initial commit

This commit is contained in:
2025-06-06 16:19:36 +02:00
commit c98bebd8bd
13 changed files with 265 additions and 0 deletions

8
anime_episode.go Normal file
View File

@ -0,0 +1,8 @@
package model
import "git.tordarus.net/tordarus/anilist"
type AnimeEpisode struct {
Anime *anilist.Media
Episode int
}

3
file_parser.go Normal file
View File

@ -0,0 +1,3 @@
package model
type FileParserFunc func(parser *Parser, str string) (file *ParsedFile, ok bool)

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.tordarus.net/nyaanime/model
go 1.18
require git.tordarus.net/tordarus/anilist v1.5.2

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
git.tordarus.net/tordarus/anilist v1.5.2 h1:SxlovS+e3lgL2SowQQwj8dQrIZzRFPomcGCw3V+My0Q=
git.tordarus.net/tordarus/anilist v1.5.2/go.mod h1:Mrhx/9+8HJVj5ebQ5fJuXqL220tEJhgQIqFK2WKPXgA=

6
pair.go Normal file
View File

@ -0,0 +1,6 @@
package model
type Pair[A, B any] struct {
First A
Second B
}

38
parsed_file.go Normal file
View File

@ -0,0 +1,38 @@
package model
import "git.tordarus.net/tordarus/anilist"
type ParsedFile struct {
// parsed data
Anime *anilist.Media
OriginalAnimeTitle string
Episode int
Languages []string
Subtitles []string
Resolution Resolution
// meta data
File string
Parser *Parser
}
var _ PropertyHolder = &ParsedFile{}
func (f *ParsedFile) GetLanguages() []string {
return f.Languages
}
func (f *ParsedFile) GetSubtitles() []string {
return f.Subtitles
}
func (f *ParsedFile) GetResolution() Resolution {
return f.Resolution
}
func (f *ParsedFile) AnimeEpisode() AnimeEpisode {
return AnimeEpisode{
Anime: f.Anime,
Episode: f.Episode,
}
}

47
parsed_torrent.go Normal file
View File

@ -0,0 +1,47 @@
package model
import (
"fmt"
"strings"
"git.tordarus.net/tordarus/anilist"
)
type ParsedTorrent struct {
// parsed data
Anime *anilist.Media
OriginalAnimeTitle string
Episode int
Languages []string
Subtitles []string
Resolution Resolution
// meta data
Torrent *Torrent
Parser *Parser
}
var _ PropertyHolder = &ParsedTorrent{}
func (t ParsedTorrent) String() string {
return fmt.Sprintf("title: %s | episode: %d | resolution: %s | languages: %s | subtitles: %s | %s",
t.Anime.Title.Native,
t.Episode,
t.Resolution,
strings.Join(t.Languages, ", "),
strings.Join(t.Subtitles, ", "),
t.Torrent.StringWithoutTitle(),
)
}
func (t *ParsedTorrent) GetLanguages() []string {
return t.Languages
}
func (t *ParsedTorrent) GetSubtitles() []string {
return t.Subtitles
}
func (t *ParsedTorrent) GetResolution() Resolution {
return t.Resolution
}

14
parser.go Normal file
View File

@ -0,0 +1,14 @@
package model
type Parser struct {
Identity string
TorrentParser TorrentParserFunc
FileParser FileParserFunc
FileEncoding string
}
func (p Parser) String() string {
return p.Identity
}

7
property_holder.go Normal file
View File

@ -0,0 +1,7 @@
package model
type PropertyHolder interface {
GetLanguages() []string
GetSubtitles() []string
GetResolution() Resolution
}

42
resolution.go Normal file
View File

@ -0,0 +1,42 @@
package model
import (
"fmt"
"strconv"
"strings"
)
type Resolution int
const (
Resolution4K Resolution = 2160
ResolutionQuadHD Resolution = 1440
ResolutionFullHD Resolution = 1080
ResolutionHD Resolution = 720
ResolutionSD Resolution = 480
ResolutionUnknown Resolution = 0
)
func ParseResolution(str string) (Resolution, error) {
switch strings.TrimSpace(str) {
case "4K":
return Resolution4K, nil
case "HD":
return ResolutionHD, nil
case "SD":
return ResolutionSD, nil
default:
v, err := strconv.Atoi(strings.TrimSuffix(str, "p"))
return Resolution(v), err
}
}
func (r Resolution) String() string {
switch r {
case Resolution4K:
return "4K"
default:
return fmt.Sprintf("%dp", r)
}
}

48
title_language.go Normal file
View File

@ -0,0 +1,48 @@
package model
import (
"errors"
"strings"
"git.tordarus.net/tordarus/anilist"
)
type AnimeTitleLang string
const (
AnimeTitleLangEnglish AnimeTitleLang = "english"
AnimeTitleLangNative AnimeTitleLang = "native"
AnimeTitleLangRomaji AnimeTitleLang = "romaji"
AnimeTitleUserPreferred AnimeTitleLang = "user-preferred"
)
func ParseAnimeTitleLang(str string) (AnimeTitleLang, error) {
lang := AnimeTitleLang(strings.TrimSpace(str))
switch lang {
case AnimeTitleLangEnglish:
return lang, nil
case AnimeTitleUserPreferred:
return lang, nil
case AnimeTitleLangNative:
return lang, nil
case AnimeTitleLangRomaji:
return lang, nil
default:
return "", errors.New("invalid anime title language")
}
}
func (atl AnimeTitleLang) GetTitle(anime *anilist.Media) string {
switch atl {
case AnimeTitleLangEnglish:
return anime.Title.English
case AnimeTitleUserPreferred:
return anime.Title.UserPreferred
case AnimeTitleLangNative:
return anime.Title.Native
case AnimeTitleLangRomaji:
return anime.Title.Romaji
default:
panic("invalid anime title language")
}
}

42
torrent.go Normal file
View File

@ -0,0 +1,42 @@
package model
import (
"fmt"
"time"
)
type Torrent struct {
ID TorrentID
Title string
Link string
Time time.Time
Seeders int
Leechers int
Downloads int
Trusted bool
}
type TorrentID string
func (t Torrent) String() string {
return fmt.Sprintf("id: %s | title: %s | seeders: %d | leechers: %d | downloads: %d | trusted: %t | upload time: %s",
t.ID,
t.Title,
t.Seeders,
t.Leechers,
t.Downloads,
t.Trusted,
t.Time.Format("2006-01-02 15:04:05"),
)
}
func (t Torrent) StringWithoutTitle() string {
return fmt.Sprintf("id: %s | seeders: %d | leechers: %d | downloads: %d | trusted: %t | upload time: %s",
t.ID,
t.Seeders,
t.Leechers,
t.Downloads,
t.Trusted,
t.Time.Format("2006-01-02 15:04:05"),
)
}

3
torrent_parser.go Normal file
View File

@ -0,0 +1,3 @@
package model
type TorrentParserFunc func(parser *Parser, torrent *Torrent) (parsedTorrent *ParsedTorrent, ok bool)