downloader/utils.go
2022-08-21 21:14:44 +02:00

60 lines
1.6 KiB
Go

package main
import (
"fmt"
"strings"
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
)
var AllMediaListStatuses = []anilist.MediaListStatus{
anilist.MediaListStatusCurrent,
anilist.MediaListStatusPlanning,
anilist.MediaListStatusCompleted,
anilist.MediaListStatusDropped,
anilist.MediaListStatusPaused,
anilist.MediaListStatusRepeating,
}
func ParseMediaListStatus(str string) (anilist.MediaListStatus, error) {
s := anilist.MediaListStatus(str)
allStatusesStr := slices.Map(AllMediaListStatuses, func(status anilist.MediaListStatus) string {
return string(status)
})
if !slices.Contains(AllMediaListStatuses, s) {
return s, ErrInvalidAnimeStatus.New(s, strings.Join(allStatusesStr, ","))
}
return s, nil
}
func Map2Str[K comparable, T any](m map[K]T) string {
b := new(strings.Builder)
for k, v := range m {
b.WriteString(fmt.Sprintf("%v:%v ", k, v))
}
str := b.String()
return str[:len(str)-1]
}
func PrintTorrentPriority(torrentPriority *TorrentPriority) {
fmt.Printf("id: %s | resolution: %d | languages: %s | subtitles: %s | seeders: %d | leechers: %d, | downloads: %d | trusted: %t | preferred properties: %s | priority: %d\n",
torrentPriority.ParsedTorrent.Torrent.ID,
torrentPriority.ParsedTorrent.Resolution,
strings.Join(torrentPriority.ParsedTorrent.Languages, ","),
strings.Join(torrentPriority.ParsedTorrent.Subtitles, ","),
torrentPriority.ParsedTorrent.Torrent.Seeders,
torrentPriority.ParsedTorrent.Torrent.Leechers,
torrentPriority.ParsedTorrent.Torrent.Downloads,
torrentPriority.ParsedTorrent.Torrent.Trusted,
Map2Str(torrentPriority.PreferredProperties),
torrentPriority.Priority,
)
}