downloader/torrent_parse.go
2022-08-15 16:45:52 +02:00

54 lines
1.4 KiB
Go

package main
import (
"errors"
"fmt"
"git.milar.in/milarin/adverr"
"git.milar.in/nyaanime/model"
"git.milar.in/nyaanime/parsers"
)
func ParseTorrent(torrent *model.Torrent) (*model.ParsedTorrent, error) {
for _, parser := range parsers.Parsers {
parsedTorrent, ok := parser.TorrentParser(&parser, torrent)
if ok {
anime, err := SearchAnimeByTitle(parsedTorrent.OriginalAnimeTitle)
if err != nil {
return parsedTorrent, ErrTorrentParseFailed.Wrap(err, torrent.ID, parser.String())
}
parsedTorrent.Anime = anime
return parsedTorrent, nil
}
}
return nil, ErrNoSuitableParser.New(torrent.ID)
}
func ParseTorrentsByAnimeEp(torrents []model.Torrent) map[model.AnimeEpisode][]*model.ParsedTorrent {
torrentsByAnimeEp := map[model.AnimeEpisode][]*model.ParsedTorrent{}
for _, torrent := range torrents {
torrent := torrent
parsedTorrent, err := ParseTorrent(&torrent)
if err != nil {
if errors.Is(err, ErrAnimeNotFound) {
fmt.Printf("anime not found: \"%s\" (torrent ID: %s)\n", parsedTorrent.OriginalAnimeTitle, torrent.ID)
} else if !errors.Is(err, ErrNoSuitableParser) {
adverr.Println(err)
}
continue
}
animeEpisode := model.AnimeEpisode{
Anime: parsedTorrent.Anime.Title.Native,
Episode: parsedTorrent.Episode,
}
torrentsByAnimeEp[animeEpisode] = append(torrentsByAnimeEp[animeEpisode], parsedTorrent)
}
return torrentsByAnimeEp
}