2022-08-15 15:55:27 +02:00
package main
import (
"errors"
"fmt"
2023-01-17 17:09:55 +01:00
"os"
2022-08-15 15:55:27 +02:00
"git.milar.in/milarin/adverr"
2023-01-13 13:38:51 +01:00
"git.milar.in/nyaanime/logic"
2022-08-15 16:45:52 +02:00
"git.milar.in/nyaanime/model"
"git.milar.in/nyaanime/parsers"
2023-01-17 17:09:55 +01:00
"github.com/fatih/color"
2022-08-15 15:55:27 +02:00
)
func ParseTorrent ( torrent * model . Torrent ) ( * model . ParsedTorrent , error ) {
for _ , parser := range parsers . Parsers {
parsedTorrent , ok := parser . TorrentParser ( & parser , torrent )
if ok {
2023-01-13 13:38:51 +01:00
anime , err := logic . SearchAnimeByTitle ( parsedTorrent . OriginalAnimeTitle )
2022-08-15 15:55:27 +02:00
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 {
2023-01-17 17:09:55 +01:00
if errors . Is ( err , logic . ErrAnimeNotFound ) {
fmt . Fprintln ( os . Stderr , color . RedString ( "torrent with ID %s could not be parsed because anime was not found: \"%s\"" , torrent . ID , parsedTorrent . OriginalAnimeTitle ) )
2022-08-15 15:55:27 +02:00
} else if ! errors . Is ( err , ErrNoSuitableParser ) {
adverr . Println ( err )
}
continue
}
2023-01-17 09:25:03 +01:00
animeEp := GetAnimeEpisode ( parsedTorrent . Anime , parsedTorrent . Episode )
torrentsByAnimeEp [ animeEp ] = append ( torrentsByAnimeEp [ animeEp ] , parsedTorrent )
2022-08-15 15:55:27 +02:00
}
return torrentsByAnimeEp
}