commit c98bebd8bdc474f1d34417c37b2261696246d639 Author: Tordarus Date: Fri Jun 6 16:19:36 2025 +0200 initial commit diff --git a/anime_episode.go b/anime_episode.go new file mode 100644 index 0000000..02fcbb2 --- /dev/null +++ b/anime_episode.go @@ -0,0 +1,8 @@ +package model + +import "git.tordarus.net/tordarus/anilist" + +type AnimeEpisode struct { + Anime *anilist.Media + Episode int +} diff --git a/file_parser.go b/file_parser.go new file mode 100644 index 0000000..dd73f41 --- /dev/null +++ b/file_parser.go @@ -0,0 +1,3 @@ +package model + +type FileParserFunc func(parser *Parser, str string) (file *ParsedFile, ok bool) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dc03e1b --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.tordarus.net/nyaanime/model + +go 1.18 + +require git.tordarus.net/tordarus/anilist v1.5.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8005efe --- /dev/null +++ b/go.sum @@ -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= diff --git a/pair.go b/pair.go new file mode 100644 index 0000000..6988679 --- /dev/null +++ b/pair.go @@ -0,0 +1,6 @@ +package model + +type Pair[A, B any] struct { + First A + Second B +} diff --git a/parsed_file.go b/parsed_file.go new file mode 100644 index 0000000..928d245 --- /dev/null +++ b/parsed_file.go @@ -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, + } +} diff --git a/parsed_torrent.go b/parsed_torrent.go new file mode 100644 index 0000000..ab8b7a0 --- /dev/null +++ b/parsed_torrent.go @@ -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 +} diff --git a/parser.go b/parser.go new file mode 100644 index 0000000..3ec64c5 --- /dev/null +++ b/parser.go @@ -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 +} diff --git a/property_holder.go b/property_holder.go new file mode 100644 index 0000000..9d83713 --- /dev/null +++ b/property_holder.go @@ -0,0 +1,7 @@ +package model + +type PropertyHolder interface { + GetLanguages() []string + GetSubtitles() []string + GetResolution() Resolution +} diff --git a/resolution.go b/resolution.go new file mode 100644 index 0000000..89f9876 --- /dev/null +++ b/resolution.go @@ -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) + } +} diff --git a/title_language.go b/title_language.go new file mode 100644 index 0000000..a0e1d8b --- /dev/null +++ b/title_language.go @@ -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") + } +} diff --git a/torrent.go b/torrent.go new file mode 100644 index 0000000..eda77ec --- /dev/null +++ b/torrent.go @@ -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"), + ) +} diff --git a/torrent_parser.go b/torrent_parser.go new file mode 100644 index 0000000..5763a77 --- /dev/null +++ b/torrent_parser.go @@ -0,0 +1,3 @@ +package model + +type TorrentParserFunc func(parser *Parser, torrent *Torrent) (parsedTorrent *ParsedTorrent, ok bool)