parser refactored
This commit is contained in:
parent
b516177d7c
commit
2a3194c37e
8
errors.go
Normal file
8
errors.go
Normal file
@ -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")
|
||||||
|
)
|
7
go.mod
7
go.mod
@ -2,4 +2,9 @@ module git.milar.in/animan/parsers
|
|||||||
|
|
||||||
go 1.18
|
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
|
||||||
|
8
go.sum
8
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-20220804154959-f8b66e8c9eda h1:9XG7Dn+UtyLwwNcuol10EdHcBQjYD+1WzmnJbRUJQfs=
|
||||||
git.milar.in/animan/model v0.0.0-20220803215306-905563410463/go.mod h1:FmgisUVGJd8EEvGP4uk9pEv6Ic9aiEhAy5eZF8zZsgU=
|
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=
|
||||||
|
12
parsers.go
12
parsers.go
@ -16,9 +16,15 @@ console.log(s);
|
|||||||
var Parsers = []model.Parser{
|
var Parsers = []model.Parser{
|
||||||
{
|
{
|
||||||
Identity: "Erai-Raws",
|
Identity: "Erai-Raws",
|
||||||
TorrentParser: torrentNameEpLangsRegex(
|
TorrentParser: regexTorrentParser(
|
||||||
`^\[Erai-raws\] (.*) - (.*?) \[1080p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`,
|
`^\[Erai-raws\] (.*) - (.*?) \[(.*?)p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`,
|
||||||
SquareBracketsLanguageParser,
|
TorrentParseOptions{
|
||||||
|
Name: 1,
|
||||||
|
Episode: 2,
|
||||||
|
Resolution: 3,
|
||||||
|
Languages: 4,
|
||||||
|
LanguageParser: SquareBracketsLanguageParser,
|
||||||
|
},
|
||||||
),
|
),
|
||||||
FileParser: nil,
|
FileParser: nil,
|
||||||
},
|
},
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
61
torrent_parsing.go
Normal file
61
torrent_parsing.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user