diff --git a/file_parse.go b/analyze_file.go similarity index 90% rename from file_parse.go rename to analyze_file.go index cda1ef4..6d3d759 100644 --- a/file_parse.go +++ b/analyze_file.go @@ -8,12 +8,6 @@ import ( "gopkg.in/vansante/go-ffprobe.v2" ) -func ParseFile(path string) (*model.ParsedFile, error) { - parsedFile := &model.ParsedFile{File: path} - - return parsedFile, nil -} - // TODO cache func AnalyzeFile(path string) (*model.ParsedFile, error) { props := &model.ParsedFile{File: path} diff --git a/parse_file.go b/parse_file.go new file mode 100644 index 0000000..44824c7 --- /dev/null +++ b/parse_file.go @@ -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 + } +} diff --git a/torrent_parsing.go b/parse_torrent.go similarity index 100% rename from torrent_parsing.go rename to parse_torrent.go diff --git a/parsers.go b/parsers.go index a525363..8ef8fe4 100644 --- a/parsers.go +++ b/parsers.go @@ -27,7 +27,13 @@ var Parsers = []model.Parser{ 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"}, }, ), - FileParser: nil, + FileParser: RegexFileParser( + `^\[SubsPlease\] (.*?) - (\d+?) .*?\.mkv$`, + FileParseOptions{ + Name: 1, + Episode: 2, + }, + ), }, { @@ -57,6 +69,12 @@ var Parsers = []model.Parser{ DefaultSubtitles: []string{"en"}, }, ), - FileParser: nil, + FileParser: RegexFileParser( + `^\[PuyaSubs!\] (.*?) - (\d+?) .*?\.mkv$`, + FileParseOptions{ + Name: 1, + Episode: 2, + }, + ), }, }