downloader/main.go
2022-08-15 16:15:20 +02:00

97 lines
2.6 KiB
Go

package main
import (
"fmt"
"math"
"time"
"git.milar.in/animan/model"
"git.milar.in/milarin/adverr"
"git.milar.in/milarin/envvars"
)
var (
PollRate = envvars.Object("POLL_RATE", 30*time.Minute, time.ParseDuration)
AnilistUsername = envvars.String("ANILIST_USERNAME", "username")
AnilistAccessToken = envvars.String("ANILIST_TOKEN", "")
StoragePath = envvars.String("STORAGE_PATH", "")
StorageUser = envvars.String("STORAGE_USER", "")
StoragePass = envvars.String("STORAGE_PASS", "")
// essential torrent properties
MaxResolution = envvars.Object("MAX_RESOLUTION", model.Resolution4K, model.ParseResolution)
MinResolution = envvars.Object("MIN_RESOLUTION", model.ResolutionHD, model.ParseResolution)
EssentialLanguages = envvars.StringSlice("ESSENTIAL_LANGUAGES", ",")
EssentialSubtitles = envvars.StringSlice("ESSENTIAL_SUBTITLES", ",")
MaxSeeders = envvars.Int("MAX_SEEDERS", math.MaxInt)
MinSeeders = envvars.Int("MIN_SEEDERS", 0)
MaxLeechers = envvars.Int("MAX_LEECHERS", math.MaxInt)
MinLeechers = envvars.Int("MIN_LEECHERS", 0)
MaxDownloads = envvars.Int("MAX_DOWNLOADS", math.MaxInt)
MinDownloads = envvars.Int("MIN_DOWNLOADS", 0)
TrustedOnly = envvars.Bool("TRUSTED_ONLY", false)
// preferred torrent properties
PreferredLanguages = envvars.StringSlice("PREFERRED_LANGUAGES", ",")
PreferredSubtitles = envvars.StringSlice("PREFERRED_SUBTITLES", ",")
/*
TODO
PreferMoreLanguages = envvars.Bool("PREFERER_MORE_LANGUAGES", false)
PreferMoreSubtitles = envvars.Bool("PREFERER_MORE_SUBTITLES", false)
*/
)
func main() {
// get access token once at startup to be sure that an access token is obtainable at all
if _, err := GetAnilistAccessToken(); err != nil {
panic(err)
}
ticker := time.NewTicker(PollRate)
defer ticker.Stop()
checkTorrents()
for range ticker.C {
checkTorrents()
}
}
func checkTorrents() {
fmt.Println("checking torrents")
start := time.Now()
torrents, err := GetLatestNyaaContent()
if err != nil {
fmt.Println(adverr.Wrap("retrieving torrents failed", err))
return
}
/*watchingAnimes, err := GetCurrentlyWatchingAnimesByAnimeID()
if err != nil {
fmt.Println(adverr.Wrap("retrieving anime list failed", err))
return
}*/
parsedTorrentsByAnimeEp := FilterEssentialTorrents(ParseTorrentsByAnimeEp(torrents))
for _, parsedTorrents := range parsedTorrentsByAnimeEp {
for _, parsedTorrent := range parsedTorrents {
fmt.Println(parsedTorrent, "|", parsedTorrent.Torrent.StringWithoutTitle())
}
}
duration := time.Since(start)
fmt.Printf("check took %s. sleeping for %s\n", duration, PollRate-duration)
}