main.go refactor
This commit is contained in:
parent
37a41c2058
commit
de3a29ca41
54
check_torrents.go
Normal file
54
check_torrents.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.milar.in/milarin/adverr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkTorrents() {
|
||||||
|
fmt.Println("checking torrents")
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
torrents, err := GetTorrents()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(adverr.Wrap("retrieving torrents failed", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
animeList, err := GetAnimeListByAnimeID()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(adverr.Wrap("retrieving anime list failed", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse torrents
|
||||||
|
parsedTorrentsByAnimeEp := ParseTorrentsByAnimeEp(torrents)
|
||||||
|
|
||||||
|
// filter not on anime list
|
||||||
|
animeListTorrents := FilterTorrentsByAnimeList(parsedTorrentsByAnimeEp, animeList)
|
||||||
|
|
||||||
|
// filter essential properties
|
||||||
|
essentialTorrents := FilterEssentialTorrents(animeListTorrents)
|
||||||
|
|
||||||
|
// filter preferred properties
|
||||||
|
preferredTorrents := GetTorrentsWithMaxPrioByAnimeEp(essentialTorrents)
|
||||||
|
|
||||||
|
for animeEp, torrentPrio := range preferredTorrents {
|
||||||
|
if IsCurrentlyDownloading(animeEp) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if props, inCollection := GetAnimeEpProps(animeEp); inCollection && props.Priority > torrentPrio.Priority {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := DownloadTorrent(animeEp, torrentPrio.ParsedTorrent); err != nil {
|
||||||
|
panic(err) // TODO error handling
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
duration := time.Since(start)
|
||||||
|
fmt.Printf("\ncheck took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond))
|
||||||
|
}
|
64
envvars.go
Normal file
64
envvars.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.milar.in/animan/model"
|
||||||
|
"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", "")
|
||||||
|
|
||||||
|
TorrentPath = envvars.String("TORRENT_PATH", "")
|
||||||
|
AnimePath = envvars.String("ANIME_PATH", "")
|
||||||
|
|
||||||
|
AnimeEpFilepathPattern = envvars.Object(
|
||||||
|
"EPISODE_FILEPATH_PATTERN",
|
||||||
|
template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}}/{{.Title.UserPreferred}} Episode {{.Episode}}.{{.Ext}}`)),
|
||||||
|
template.New("anime-episode-filepath-pattern").Parse,
|
||||||
|
)
|
||||||
|
|
||||||
|
AnimeStatus = envvars.ObjectSlice("ANIME_STATUS", ",", ParseMediaListStatus)
|
||||||
|
|
||||||
|
// 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 = ParsePreferredStringProps(envvars.StringSlice("PREFERRED_LANGUAGES", "|"))
|
||||||
|
PreferredSubtitles = ParsePreferredStringProps(envvars.StringSlice("PREFERRED_SUBTITLES", "|"))
|
||||||
|
PreferredResolutions = ParsePreferredProps(envvars.StringSlice("PREFERRED_RESOLUTIONS", "|"), model.ParseResolution)
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO
|
||||||
|
|
||||||
|
PreferMoreLanguages = envvars.Bool("PREFERER_MORE_LANGUAGES", false)
|
||||||
|
PreferMoreSubtitles = envvars.Bool("PREFERER_MORE_SUBTITLES", false)
|
||||||
|
*/
|
||||||
|
)
|
105
main.go
105
main.go
@ -2,68 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"math"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.milar.in/milarin/adverr"
|
|
||||||
"git.milar.in/milarin/anilist"
|
"git.milar.in/milarin/anilist"
|
||||||
"git.milar.in/milarin/envvars"
|
|
||||||
"git.milar.in/nyaanime/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
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", "")
|
|
||||||
|
|
||||||
TorrentPath = envvars.String("TORRENT_PATH", "")
|
|
||||||
AnimePath = envvars.String("ANIME_PATH", "")
|
|
||||||
|
|
||||||
AnimeEpFilepathPattern = envvars.Object(
|
|
||||||
"EPISODE_FILEPATH_PATTERN",
|
|
||||||
template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}}/{{.Title.UserPreferred}} Episode {{.Episode}}.{{.Ext}}`)),
|
|
||||||
template.New("anime-episode-filepath-pattern").Parse,
|
|
||||||
)
|
|
||||||
|
|
||||||
AnimeStatus = envvars.ObjectSlice("ANIME_STATUS", ",", ParseMediaListStatus)
|
|
||||||
|
|
||||||
// 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 = ParsePreferredStringProps(envvars.StringSlice("PREFERRED_LANGUAGES", "|"))
|
|
||||||
PreferredSubtitles = ParsePreferredStringProps(envvars.StringSlice("PREFERRED_SUBTITLES", "|"))
|
|
||||||
PreferredResolutions = ParsePreferredProps(envvars.StringSlice("PREFERRED_RESOLUTIONS", "|"), model.ParseResolution)
|
|
||||||
|
|
||||||
/*
|
|
||||||
TODO
|
|
||||||
|
|
||||||
PreferMoreLanguages = envvars.Bool("PREFERER_MORE_LANGUAGES", false)
|
|
||||||
PreferMoreSubtitles = envvars.Bool("PREFERER_MORE_SUBTITLES", false)
|
|
||||||
*/
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -92,49 +33,3 @@ func main() {
|
|||||||
checkTorrents()
|
checkTorrents()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkTorrents() {
|
|
||||||
fmt.Println("checking torrents")
|
|
||||||
start := time.Now()
|
|
||||||
|
|
||||||
torrents, err := GetTorrents()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(adverr.Wrap("retrieving torrents failed", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
animeList, err := GetAnimeListByAnimeID()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(adverr.Wrap("retrieving anime list failed", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse torrents
|
|
||||||
parsedTorrentsByAnimeEp := ParseTorrentsByAnimeEp(torrents)
|
|
||||||
|
|
||||||
// filter not on anime list
|
|
||||||
animeListTorrents := FilterTorrentsByAnimeList(parsedTorrentsByAnimeEp, animeList)
|
|
||||||
|
|
||||||
// filter essential properties
|
|
||||||
essentialTorrents := FilterEssentialTorrents(animeListTorrents)
|
|
||||||
|
|
||||||
// filter preferred properties
|
|
||||||
preferredTorrents := GetTorrentsWithMaxPrioByAnimeEp(essentialTorrents)
|
|
||||||
|
|
||||||
for animeEp, torrentPrio := range preferredTorrents {
|
|
||||||
if IsCurrentlyDownloading(animeEp) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if props, inCollection := GetAnimeEpProps(animeEp); inCollection && props.Priority > torrentPrio.Priority {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := DownloadTorrent(animeEp, torrentPrio.ParsedTorrent); err != nil {
|
|
||||||
panic(err) // TODO error handling
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
duration := time.Since(start)
|
|
||||||
fmt.Printf("\ncheck took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond))
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user