diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..d9a1481 --- /dev/null +++ b/errors.go @@ -0,0 +1,8 @@ +package parsers + +import "git.milar.in/milarin/adverr" + +var ( + ErrTorrentParserInsufficientData = adverr.NewErrTmpl("ErrTorrentParserInsufficientData", "regex '%s' must at least provide title and episode") + ErrTorrentParserInvalidGroupReference = adverr.NewErrTmpl("ErrTorrentParserInvalidGroupReference", "options references group %d but regex only has %d groups") +) diff --git a/go.mod b/go.mod index b899b6f..3081a66 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module git.milar.in/animan/parsers go 1.18 -require git.milar.in/animan/model v0.0.0-20220803215306-905563410463 +require ( + git.milar.in/animan/model v0.0.0-20220804154959-f8b66e8c9eda + git.milar.in/milarin/adverr v0.2.1 +) + +require git.milar.in/milarin/anilist v1.5.0 // indirect diff --git a/go.sum b/go.sum index 021bc6f..6b733a6 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ -git.milar.in/animan/model v0.0.0-20220803215306-905563410463 h1:M7Xxqbv7WQ3xwW66APwI7KQ8wD8IN/v6ZZbq1JB1T2M= -git.milar.in/animan/model v0.0.0-20220803215306-905563410463/go.mod h1:FmgisUVGJd8EEvGP4uk9pEv6Ic9aiEhAy5eZF8zZsgU= +git.milar.in/animan/model v0.0.0-20220804154959-f8b66e8c9eda h1:9XG7Dn+UtyLwwNcuol10EdHcBQjYD+1WzmnJbRUJQfs= +git.milar.in/animan/model v0.0.0-20220804154959-f8b66e8c9eda/go.mod h1:oXDr3slnzXoccIrci2wotX0cWwMVuAQ9dSvmh1buE4c= +git.milar.in/milarin/adverr v0.2.1 h1:eyXFGC+Ui/kcNt2+NqP3HiAplwxzqeNr9DfitsUb3c4= +git.milar.in/milarin/adverr v0.2.1/go.mod h1:wwfglcey4R3vqjNL/d8mbnvFJGzETRXzAEolIHZY32w= +git.milar.in/milarin/anilist v1.5.0 h1:fSiAXY/topNk4ISEp2QtcG9HHKLJfMc8w05iqc+Paf0= +git.milar.in/milarin/anilist v1.5.0/go.mod h1:8PTHXFMA45JpfRFIpcdrKwDHue8fbT/wwV1BuHFn6c0= diff --git a/parsers.go b/parsers.go index d701806..dbf6d84 100644 --- a/parsers.go +++ b/parsers.go @@ -16,9 +16,15 @@ console.log(s); var Parsers = []model.Parser{ { Identity: "Erai-Raws", - TorrentParser: torrentNameEpLangsRegex( - `^\[Erai-raws\] (.*) - (.*?) \[1080p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`, - SquareBracketsLanguageParser, + TorrentParser: regexTorrentParser( + `^\[Erai-raws\] (.*) - (.*?) \[(.*?)p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`, + TorrentParseOptions{ + Name: 1, + Episode: 2, + Resolution: 3, + Languages: 4, + LanguageParser: SquareBracketsLanguageParser, + }, ), FileParser: nil, }, diff --git a/torrent_parser_funcs.go b/torrent_parser_funcs.go deleted file mode 100644 index 92f63bc..0000000 --- a/torrent_parser_funcs.go +++ /dev/null @@ -1,58 +0,0 @@ -package parsers - -import ( - "regexp" - - "git.milar.in/animan/model" -) - -// torrentNameEpRegex returns a TorrentParserFunc with the following regex groups: -// 1: name -// 2: episode -func torrentNameEpRegex(regex string) model.TorrentParserFunc { - pattern := regexp.MustCompile(regex) - - return func(parser *model.Parser, torrent *model.Torrent) (ParsedTorrent *model.ParsedTorrent, ok bool) { - matches := pattern.FindStringSubmatch(torrent.Title) - if len(matches) <= 2 { - return nil, false - } - - name := matches[1] - episode, ok := atoi(matches[2]) - - return &model.ParsedTorrent{ - Torrent: torrent, - Name: name, - Episode: episode, - Parser: parser, - }, ok - } -} - -// torrentNameEpRegex returns a TorrentParserFunc with the following regex groups: -// 1: name -// 2: episode -// 3: languages -func torrentNameEpLangsRegex(regex string, languageParser LanguageParserFunc) model.TorrentParserFunc { - pattern := regexp.MustCompile(regex) - - return func(parser *model.Parser, torrent *model.Torrent) (ParsedTorrent *model.ParsedTorrent, ok bool) { - matches := pattern.FindStringSubmatch(torrent.Title) - if len(matches) <= 2 { - return nil, false - } - - name := matches[1] - episode, ok := atoi(matches[2]) - languages := languageParser(matches[3]) - - return &model.ParsedTorrent{ - Torrent: torrent, - Name: name, - Episode: episode, - Languages: languages, - Parser: parser, - }, ok - } -} diff --git a/torrent_parsing.go b/torrent_parsing.go new file mode 100644 index 0000000..759c2f6 --- /dev/null +++ b/torrent_parsing.go @@ -0,0 +1,61 @@ +package parsers + +import ( + "regexp" + + "git.milar.in/animan/model" +) + +// TorrentParseOptions holds the subgroup index in which information can be found in a given regex +// as well as some parser specific functions +type TorrentParseOptions struct { + Name int + Episode int + Languages int + Subtitles int + Resolution int + + LanguageParser LanguageParserFunc +} + +func regexTorrentParser(regex string, options TorrentParseOptions) model.TorrentParserFunc { + pattern := regexp.MustCompile(regex) + + if options.Name == 0 || options.Episode == 0 { + panic(ErrTorrentParserInsufficientData.New(regex)) + } + + for _, g := range []int{options.Name, options.Episode, options.Languages, options.Subtitles, options.Resolution} { + if g > pattern.NumSubexp() { + panic(ErrTorrentParserInvalidGroupReference.New(g, pattern.NumSubexp())) + } + } + + return func(parser *model.Parser, torrent *model.Torrent) (ParsedTorrent *model.ParsedTorrent, ok bool) { + matches := pattern.FindStringSubmatch(torrent.Title) + + if matches == nil { + return nil, false + } + + episode, ok := atoi(matches[options.Episode]) + if !ok { + return nil, false + } + + resolution, err := model.ParseResolution(matches[options.Resolution]) + if err != nil { + return nil, false + } + + return &model.ParsedTorrent{ + Title: matches[options.Name], + Episode: episode, + Resolution: resolution, + Parser: parser, + Languages: options.LanguageParser(matches[options.Languages]), + + Torrent: torrent, + }, true + } +}