downloader/utils.go
2022-08-22 00:13:50 +02:00

60 lines
1.7 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 FormatTorrentPriority(torrentPriority *TorrentPriority) string {
return fmt.Sprintf("id: %s | resolution: %d | languages: %s | subtitles: %s | seeders: %d | leechers: %d, | downloads: %d | trusted: %t | preferred properties: %s | priority: %d",
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,
)
}