downloader/show_debug_info.go
2023-01-15 20:59:54 +01:00

85 lines
2.5 KiB
Go

package main
import (
"fmt"
"strings"
"github.com/fatih/color"
"git.milar.in/milarin/slices"
"git.milar.in/milarin/tprint"
"git.milar.in/nyaanime/model"
)
var (
BoldText = color.New(color.Bold)
)
func ShowDebugInfo(
parsedTorrentsByAnimeEp, animeListTorrents, essentialTorrents map[model.AnimeEpisode][]*model.ParsedTorrent,
preferredTorrents map[model.AnimeEpisode]*TorrentPriority,
downloadingEpisodes map[model.AnimeEpisode]struct{},
inCollectionEpisodes map[model.AnimeEpisode]bool,
collectionPreferredTorrents map[*model.ParsedTorrent]bool,
downloadedTorrents map[model.AnimeEpisode]*TorrentPriority) {
for animeEp, parsedTorrents := range parsedTorrentsByAnimeEp {
table := tprint.NewTable("id", "resolution", "languages", "subtitles", "seeders", "leechers", "downloads", "trusted", "group", "evaluation")
for _, torrent := range parsedTorrents {
eval := getEvaluation(
slices.Contains(essentialTorrents[animeEp], torrent),
preferredTorrents[animeEp] != nil && preferredTorrents[animeEp].ParsedTorrent == torrent,
collectionPreferredTorrents[torrent],
downloadedTorrents[animeEp] != nil && downloadedTorrents[animeEp].ParsedTorrent == torrent,
)
table.AddRow(
torrent.Torrent.ID,
torrent.Resolution,
strings.Join(torrent.Languages, ", "),
strings.Join(torrent.Subtitles, ", "),
torrent.Torrent.Seeders,
torrent.Torrent.Leechers,
torrent.Torrent.Downloads,
torrent.Torrent.Trusted,
torrent.Parser.Identity,
eval,
)
}
var epState string
if _, onList := animeListTorrents[animeEp]; onList {
if _, downloading := downloadingEpisodes[animeEp]; downloading {
epState = color.BlueString("CURRENTLY DOWNLOADING")
} else if inCollectionEpisodes[animeEp] {
epState = color.GreenString("IN COLLECTION")
} else {
epState = color.YellowString("ON LIST")
}
} else {
epState = color.RedString("NOT ON LIST")
}
b := new(strings.Builder)
DebugAnimeEpisodePattern.Execute(b, animeEp)
header := BoldText.Sprintf("%s (%s)", color.MagentaString(b.String()), epState)
fmt.Println(tprint.FormatHeaderTable(header, table))
}
}
func getEvaluation(essential, preferred, collectionPreferred, downloaded bool) string {
if downloaded {
return color.GreenString("DOWNLOAD STARTED")
} else if collectionPreferred {
return color.GreenString("collection preferred")
} else if preferred {
return color.BlueString("torrent preferred")
} else if essential {
return color.YellowString("torrent considered")
} else {
return color.RedString("torrent ignored")
}
}