default torrent values introduced

This commit is contained in:
Timon Ringwald 2022-08-15 13:06:19 +02:00
parent 2a3194c37e
commit 77db2c6683
6 changed files with 54 additions and 16 deletions

View File

@ -4,5 +4,8 @@ import "git.milar.in/milarin/adverr"
var ( var (
ErrTorrentParserInsufficientData = adverr.NewErrTmpl("ErrTorrentParserInsufficientData", "regex '%s' must at least provide title and episode") 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") ErrTorrentParserInvalidGroupReference = adverr.NewErrTmpl("ErrTorrentParserInvalidGroupReference", "options references group %d but regex only has %d groups")
) )

2
go.mod
View File

@ -3,7 +3,7 @@ module git.milar.in/animan/parsers
go 1.18 go 1.18
require ( 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 git.milar.in/milarin/adverr v0.2.1
) )

2
go.sum
View File

@ -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 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-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 h1:eyXFGC+Ui/kcNt2+NqP3HiAplwxzqeNr9DfitsUb3c4=
git.milar.in/milarin/adverr v0.2.1/go.mod h1:wwfglcey4R3vqjNL/d8mbnvFJGzETRXzAEolIHZY32w= 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 h1:fSiAXY/topNk4ISEp2QtcG9HHKLJfMc8w05iqc+Paf0=

View File

@ -22,8 +22,9 @@ var Parsers = []model.Parser{
Name: 1, Name: 1,
Episode: 2, Episode: 2,
Resolution: 3, Resolution: 3,
Languages: 4, Subtitles: 4,
LanguageParser: SquareBracketsLanguageParser, SubtitleParser: SquareBracketsLanguageParser,
DefaultLanguages: []string{"ja"},
}, },
), ),
FileParser: nil, FileParser: nil,

View File

@ -9,22 +9,38 @@ import (
// TorrentParseOptions holds the subgroup index in which information can be found in a given regex // TorrentParseOptions holds the subgroup index in which information can be found in a given regex
// as well as some parser specific functions // as well as some parser specific functions
type TorrentParseOptions struct { type TorrentParseOptions struct {
// regex group references
Name int Name int
Episode int Episode int
Languages int Languages int
Subtitles int Subtitles int
Resolution int Resolution int
// language parsers
LanguageParser LanguageParserFunc 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 { func regexTorrentParser(regex string, options TorrentParseOptions) model.TorrentParserFunc {
pattern := regexp.MustCompile(regex) pattern := regexp.MustCompile(regex)
// handle faulty regexes
if options.Name == 0 || options.Episode == 0 { if options.Name == 0 || options.Episode == 0 {
panic(ErrTorrentParserInsufficientData.New(regex)) 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} { for _, g := range []int{options.Name, options.Episode, options.Languages, options.Subtitles, options.Resolution} {
if g > pattern.NumSubexp() { if g > pattern.NumSubexp() {
panic(ErrTorrentParserInvalidGroupReference.New(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) { return func(parser *model.Parser, torrent *model.Torrent) (ParsedTorrent *model.ParsedTorrent, ok bool) {
var err error
matches := pattern.FindStringSubmatch(torrent.Title) matches := pattern.FindStringSubmatch(torrent.Title)
if matches == nil { if matches == nil {
@ -43,17 +61,31 @@ func regexTorrentParser(regex string, options TorrentParseOptions) model.Torrent
return nil, false return nil, false
} }
resolution, err := model.ParseResolution(matches[options.Resolution]) resolution := options.DefaultResolution
if options.Resolution != 0 {
resolution, err = model.ParseResolution(matches[options.Resolution])
if err != nil { if err != nil {
return nil, false 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{ return &model.ParsedTorrent{
Title: matches[options.Name], OriginalAnimeTitle: matches[options.Name],
Episode: episode, Episode: episode,
Resolution: resolution, Resolution: resolution,
Parser: parser, Parser: parser,
Languages: options.LanguageParser(matches[options.Languages]), Languages: languages,
Subtitles: subtitles,
Torrent: torrent, Torrent: torrent,
}, true }, true