From e7bce7c418d8a217bec5f342b893abc2e69659bc Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 7 Dec 2022 20:25:13 +0100 Subject: [PATCH] file parsers implemented --- file_parse.go => analyze_file.go | 6 --- parse_file.go | 57 ++++++++++++++++++++++++++ torrent_parsing.go => parse_torrent.go | 0 parsers.go | 24 +++++++++-- 4 files changed, 78 insertions(+), 9 deletions(-) rename file_parse.go => analyze_file.go (90%) create mode 100644 parse_file.go rename torrent_parsing.go => parse_torrent.go (100%) 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, + }, + ), }, }