initial commit
This commit is contained in:
		
							
								
								
									
										52
									
								
								torrent_parse.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								torrent_parse.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,52 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"os" | ||||
|  | ||||
| 	"git.tordarus.net/nyaanime/logic" | ||||
| 	"git.tordarus.net/nyaanime/model" | ||||
| 	"git.tordarus.net/nyaanime/parsers" | ||||
| 	"git.tordarus.net/tordarus/adverr/v2" | ||||
| 	"github.com/fatih/color" | ||||
| ) | ||||
|  | ||||
| func ParseTorrent(torrent *model.Torrent) (*model.ParsedTorrent, error) { | ||||
| 	for _, parser := range parsers.Parsers { | ||||
| 		parsedTorrent, ok := parser.TorrentParser(&parser, torrent) | ||||
| 		if ok { | ||||
| 			anime, err := logic.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, 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)) | ||||
| 			} else if !errors.Is(err, ErrNoSuitableParser) { | ||||
| 				adverr.Println(err) | ||||
| 			} | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		animeEp := GetAnimeEpisode(parsedTorrent.Anime, parsedTorrent.Episode) | ||||
| 		torrentsByAnimeEp[animeEp] = append(torrentsByAnimeEp[animeEp], parsedTorrent) | ||||
| 	} | ||||
|  | ||||
| 	return torrentsByAnimeEp | ||||
| } | ||||
		Reference in New Issue
	
	Block a user