From 77db2c668303b325e3636f484754a8df0e10b3f2 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 15 Aug 2022 13:06:19 +0200 Subject: [PATCH] default torrent values introduced --- errors.go | 7 ++- go.mod | 2 +- go.sum | 2 + lang_parser_funcs.go => language_parsing.go | 0 parsers.go | 11 ++--- torrent_parsing.go | 48 +++++++++++++++++---- 6 files changed, 54 insertions(+), 16 deletions(-) rename lang_parser_funcs.go => language_parsing.go (100%) diff --git a/errors.go b/errors.go index d9a1481..1e65fb9 100644 --- a/errors.go +++ b/errors.go @@ -3,6 +3,9 @@ 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") + ErrTorrentParserInsufficientData = adverr.NewErrTmpl("ErrTorrentParserInsufficientData", "regex '%s' must at least provide title and episode") + ErrTorrentParserInsufficientLanguageData = adverr.NewErrTmpl("ErrTorrentParserInsufficientLanguageData", "no language reference in regex and no default language set") + ErrTorrentParserInsufficientSubtitleData = adverr.NewErrTmpl("ErrTorrentParserInsufficientSubtitleData", "no subtitle reference in regex and no default subtitle set") + ErrTorrentParserInsufficientResolutionData = adverr.NewErrTmpl("ErrTorrentParserInsufficientResolutionData", "no resolution reference in regex and no default resolution set") + ErrTorrentParserInvalidGroupReference = adverr.NewErrTmpl("ErrTorrentParserInvalidGroupReference", "options references group %d but regex only has %d groups") ) diff --git a/go.mod b/go.mod index 3081a66..91f9375 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.milar.in/animan/parsers go 1.18 require ( - git.milar.in/animan/model v0.0.0-20220804154959-f8b66e8c9eda + git.milar.in/animan/model v0.0.0-20220815093549-fe9b565a9dd2 git.milar.in/milarin/adverr v0.2.1 ) diff --git a/go.sum b/go.sum index 6b733a6..15adf7e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ 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/animan/model v0.0.0-20220815093549-fe9b565a9dd2 h1:vNbYNp8VJQ6FBENgZMHA1IFOfPkQEA+TIaN8PFF8m48= +git.milar.in/animan/model v0.0.0-20220815093549-fe9b565a9dd2/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= diff --git a/lang_parser_funcs.go b/language_parsing.go similarity index 100% rename from lang_parser_funcs.go rename to language_parsing.go diff --git a/parsers.go b/parsers.go index dbf6d84..40938b8 100644 --- a/parsers.go +++ b/parsers.go @@ -19,11 +19,12 @@ var Parsers = []model.Parser{ TorrentParser: regexTorrentParser( `^\[Erai-raws\] (.*) - (.*?) \[(.*?)p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`, TorrentParseOptions{ - Name: 1, - Episode: 2, - Resolution: 3, - Languages: 4, - LanguageParser: SquareBracketsLanguageParser, + Name: 1, + Episode: 2, + Resolution: 3, + Subtitles: 4, + SubtitleParser: SquareBracketsLanguageParser, + DefaultLanguages: []string{"ja"}, }, ), FileParser: nil, diff --git a/torrent_parsing.go b/torrent_parsing.go index 759c2f6..b954149 100644 --- a/torrent_parsing.go +++ b/torrent_parsing.go @@ -9,22 +9,38 @@ import ( // 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 { + // regex group references Name int Episode int Languages int Subtitles int Resolution int + // language parsers LanguageParser LanguageParserFunc + SubtitleParser LanguageParserFunc + + // default values used when group reference is 0 + DefaultLanguages []string + DefaultSubtitles []string + DefaultResolution model.Resolution } func regexTorrentParser(regex string, options TorrentParseOptions) model.TorrentParserFunc { pattern := regexp.MustCompile(regex) + // handle faulty regexes if options.Name == 0 || options.Episode == 0 { panic(ErrTorrentParserInsufficientData.New(regex)) + } else if options.Languages == 0 && options.DefaultLanguages == nil { + panic(ErrTorrentParserInsufficientLanguageData.New(regex)) + } else if options.Subtitles == 0 && options.DefaultSubtitles == nil { + panic(ErrTorrentParserInsufficientSubtitleData.New(regex)) + } else if options.Resolution == 0 && options.DefaultResolution == 0 { + panic(ErrTorrentParserInsufficientResolutionData.New(regex)) } + // handle faulty group references for _, g := range []int{options.Name, options.Episode, options.Languages, options.Subtitles, options.Resolution} { if g > pattern.NumSubexp() { panic(ErrTorrentParserInvalidGroupReference.New(g, pattern.NumSubexp())) @@ -32,6 +48,8 @@ func regexTorrentParser(regex string, options TorrentParseOptions) model.Torrent } return func(parser *model.Parser, torrent *model.Torrent) (ParsedTorrent *model.ParsedTorrent, ok bool) { + var err error + matches := pattern.FindStringSubmatch(torrent.Title) if matches == nil { @@ -43,17 +61,31 @@ func regexTorrentParser(regex string, options TorrentParseOptions) model.Torrent return nil, false } - resolution, err := model.ParseResolution(matches[options.Resolution]) - if err != nil { - return nil, false + resolution := options.DefaultResolution + if options.Resolution != 0 { + resolution, err = model.ParseResolution(matches[options.Resolution]) + if err != nil { + return nil, false + } + } + + languages := options.DefaultLanguages + if options.Languages != 0 { + languages = options.LanguageParser(matches[options.Languages]) + } + + subtitles := options.DefaultSubtitles + if options.Subtitles != 0 { + subtitles = options.SubtitleParser(matches[options.Subtitles]) } return &model.ParsedTorrent{ - Title: matches[options.Name], - Episode: episode, - Resolution: resolution, - Parser: parser, - Languages: options.LanguageParser(matches[options.Languages]), + OriginalAnimeTitle: matches[options.Name], + Episode: episode, + Resolution: resolution, + Parser: parser, + Languages: languages, + Subtitles: subtitles, Torrent: torrent, }, true