parsers/parse_torrent.go

94 lines
2.6 KiB
Go
Raw Permalink Normal View History

2022-08-04 18:13:41 +02:00
package parsers
import (
"regexp"
2022-08-15 16:43:27 +02:00
"git.milar.in/nyaanime/model"
2022-08-04 18:13:41 +02:00
)
// 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 {
2022-08-15 13:06:19 +02:00
// regex group references
2022-08-04 18:13:41 +02:00
Name int
Episode int
Languages int
Subtitles int
Resolution int
2022-08-15 13:06:19 +02:00
// language parsers
2022-08-04 18:13:41 +02:00
LanguageParser LanguageParserFunc
2022-08-15 13:06:19 +02:00
SubtitleParser LanguageParserFunc
// default values used when group reference is 0
DefaultLanguages []string
DefaultSubtitles []string
DefaultResolution model.Resolution
2022-08-04 18:13:41 +02:00
}
2022-12-07 19:57:40 +01:00
func RegexTorrentParser(regex string, options TorrentParseOptions) model.TorrentParserFunc {
2022-08-04 18:13:41 +02:00
pattern := regexp.MustCompile(regex)
2022-08-15 13:06:19 +02:00
// handle faulty regexes
2022-08-04 18:13:41 +02:00
if options.Name == 0 || options.Episode == 0 {
panic(ErrTorrentParserInsufficientData.New(regex))
2022-08-15 13:06:19 +02:00
} 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))
2022-08-04 18:13:41 +02:00
}
2022-08-15 13:06:19 +02:00
// handle faulty group references
2022-08-04 18:13:41 +02:00
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) {
2022-08-15 13:06:19 +02:00
var err error
2022-08-04 18:13:41 +02:00
matches := pattern.FindStringSubmatch(torrent.Title)
if matches == nil {
return nil, false
}
episode, ok := atoi(matches[options.Episode])
if !ok {
return nil, false
}
2022-08-15 13:06:19 +02:00
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])
2022-08-04 18:13:41 +02:00
}
return &model.ParsedTorrent{
2022-08-15 13:06:19 +02:00
OriginalAnimeTitle: matches[options.Name],
Episode: episode,
Resolution: resolution,
Parser: parser,
2022-08-15 13:40:13 +02:00
Languages: ParseLanguages(languages),
Subtitles: ParseLanguages(subtitles),
2022-08-04 18:13:41 +02:00
Torrent: torrent,
}, true
}
}