file parsers implemented

This commit is contained in:
Timon Ringwald 2022-12-07 20:25:13 +01:00
parent 9ef0c1a7d1
commit e7bce7c418
4 changed files with 78 additions and 9 deletions

View File

@ -8,12 +8,6 @@ import (
"gopkg.in/vansante/go-ffprobe.v2" "gopkg.in/vansante/go-ffprobe.v2"
) )
func ParseFile(path string) (*model.ParsedFile, error) {
parsedFile := &model.ParsedFile{File: path}
return parsedFile, nil
}
// TODO cache // TODO cache
func AnalyzeFile(path string) (*model.ParsedFile, error) { func AnalyzeFile(path string) (*model.ParsedFile, error) {
props := &model.ParsedFile{File: path} props := &model.ParsedFile{File: path}

57
parse_file.go Normal file
View File

@ -0,0 +1,57 @@
package parsers
import (
"path/filepath"
"regexp"
"git.milar.in/nyaanime/model"
)
// FileParseOptions holds the subgroup index in which information can be found in a given regex
type FileParseOptions struct {
// regex group references
Name int
Episode int
}
func RegexFileParser(regex string, options FileParseOptions) model.FileParserFunc {
pattern := regexp.MustCompile(regex)
// handle faulty regexes
if options.Name == 0 || options.Episode == 0 {
panic(ErrTorrentParserInsufficientData.New(regex))
}
// handle faulty group references
for _, g := range []int{options.Name, options.Episode} {
if g > pattern.NumSubexp() {
panic(ErrTorrentParserInvalidGroupReference.New(g, pattern.NumSubexp()))
}
}
return func(parser *model.Parser, path string) (file *model.ParsedFile, ok bool) {
filename := filepath.Base(path)
matches := pattern.FindStringSubmatch(filename)
if matches == nil {
return nil, false
}
episode, ok := atoi(matches[options.Episode])
if !ok {
return nil, false
}
parsedFile, err := AnalyzeFile(path)
if err != nil {
return nil, false
}
parsedFile.OriginalAnimeTitle = matches[options.Name]
parsedFile.Episode = episode
parsedFile.Parser = parser
//parsedFile.File = filename // TODO necessary?
return parsedFile, true
}
}

View File

@ -27,7 +27,13 @@ var Parsers = []model.Parser{
DefaultLanguages: []string{"ja"}, DefaultLanguages: []string{"ja"},
}, },
), ),
FileParser: nil, FileParser: RegexFileParser(
`^\[Erai-raws\] (.*?) - (\d+?) .*?\.mkv$`,
FileParseOptions{
Name: 1,
Episode: 2,
},
),
}, },
{ {
@ -42,7 +48,13 @@ var Parsers = []model.Parser{
DefaultSubtitles: []string{"en"}, DefaultSubtitles: []string{"en"},
}, },
), ),
FileParser: nil, FileParser: RegexFileParser(
`^\[SubsPlease\] (.*?) - (\d+?) .*?\.mkv$`,
FileParseOptions{
Name: 1,
Episode: 2,
},
),
}, },
{ {
@ -57,6 +69,12 @@ var Parsers = []model.Parser{
DefaultSubtitles: []string{"en"}, DefaultSubtitles: []string{"en"},
}, },
), ),
FileParser: nil, FileParser: RegexFileParser(
`^\[PuyaSubs!\] (.*?) - (\d+?) .*?\.mkv$`,
FileParseOptions{
Name: 1,
Episode: 2,
},
),
}, },
} }