From b516177d7cff17fe7a07c3cdb49f12fbf91ba3da Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Thu, 4 Aug 2022 00:05:19 +0200 Subject: [PATCH] initial commit --- go.mod | 5 ++++ go.sum | 2 ++ lang_parser_funcs.go | 11 ++++++++ parsers.go | 25 ++++++++++++++++++ torrent_parser_funcs.go | 58 +++++++++++++++++++++++++++++++++++++++++ utils.go | 11 ++++++++ 6 files changed, 112 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 lang_parser_funcs.go create mode 100644 parsers.go create mode 100644 torrent_parser_funcs.go create mode 100644 utils.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b899b6f --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.milar.in/animan/parsers + +go 1.18 + +require git.milar.in/animan/model v0.0.0-20220803215306-905563410463 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..021bc6f --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +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= diff --git a/lang_parser_funcs.go b/lang_parser_funcs.go new file mode 100644 index 0000000..90fbe69 --- /dev/null +++ b/lang_parser_funcs.go @@ -0,0 +1,11 @@ +package parsers + +import "regexp" + +type LanguageParserFunc func(str string) []string + +var SquareBracketsLanguageParserRegex = regexp.MustCompile(`\[.+?\]`) + +func SquareBracketsLanguageParser(str string) []string { + return SquareBracketsLanguageParserRegex.FindAllString(str, -1) +} diff --git a/parsers.go b/parsers.go new file mode 100644 index 0000000..d701806 --- /dev/null +++ b/parsers.go @@ -0,0 +1,25 @@ +package parsers + +import "git.milar.in/animan/model" + +/* +how to get all torrent names on a nyaa page: + +let s = ""; +document.querySelectorAll("tr > td:nth-child(2) > a:not(.comments)").forEach((element, index) => { + s += element.textContent + "\n"; +}) +console.log(s); + +*/ + +var Parsers = []model.Parser{ + { + Identity: "Erai-Raws", + TorrentParser: torrentNameEpLangsRegex( + `^\[Erai-raws\] (.*) - (.*?) \[1080p\](?:\[HEVC\])?(?:\[Multiple Subtitle\])? (\[.*?\]+)?$`, + SquareBracketsLanguageParser, + ), + FileParser: nil, + }, +} diff --git a/torrent_parser_funcs.go b/torrent_parser_funcs.go new file mode 100644 index 0000000..92f63bc --- /dev/null +++ b/torrent_parser_funcs.go @@ -0,0 +1,58 @@ +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/utils.go b/utils.go new file mode 100644 index 0000000..807ff76 --- /dev/null +++ b/utils.go @@ -0,0 +1,11 @@ +package parsers + +import "strconv" + +func atoi(s string) (int, bool) { + v, err := strconv.Atoi(s) + if err != nil { + return 0, false + } + return v, true +}