initial commit

This commit is contained in:
Timon Ringwald 2022-08-04 00:05:19 +02:00
commit b516177d7c
6 changed files with 112 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.milar.in/animan/parsers
go 1.18
require git.milar.in/animan/model v0.0.0-20220803215306-905563410463

2
go.sum Normal file
View File

@ -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=

11
lang_parser_funcs.go Normal file
View File

@ -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)
}

25
parsers.go Normal file
View File

@ -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,
},
}

58
torrent_parser_funcs.go Normal file
View File

@ -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
}
}

11
utils.go Normal file
View File

@ -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
}