From 9110cf407644c47c5e91087f2a311d8346482329 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Thu, 25 Aug 2022 23:25:48 +0200 Subject: [PATCH] polishing --- check_torrents.go | 19 +++++++++++-------- download_torrent_file.go | 3 +-- envvars.go | 11 +++++++++++ go.mod | 8 +++++--- go.sum | 17 +++++++++++++++++ local_file_check.go | 6 ++++++ show_debug_info.go | 13 +++++-------- torrent_filter.go | 4 ++++ utils.go | 2 +- 9 files changed, 61 insertions(+), 22 deletions(-) diff --git a/check_torrents.go b/check_torrents.go index 2b97c94..8fcdb4d 100644 --- a/check_torrents.go +++ b/check_torrents.go @@ -44,26 +44,30 @@ func checkTorrents() { for animeEp, torrentPrio := range preferredTorrents { if IsCurrentlyDownloading(animeEp) { - downloadingEpisodes[animeEp] = struct{}{} + downloadingEpisodes[animeEp] = struct{}{} // debug output continue } props, inCollection := GetAnimeEpProps(animeEp) - lowerPrio := props.Priority > torrentPrio.Priority - inCollectionEpisodes[animeEp] = inCollection - lowerPrioTorrents[torrentPrio.ParsedTorrent] = lowerPrio - if inCollection && lowerPrio { - continue + inCollectionEpisodes[animeEp] = inCollection // debug output + if inCollection { + lowerPrio := props.Priority > torrentPrio.Priority + lowerPrioTorrents[torrentPrio.ParsedTorrent] = lowerPrio // debug output + if lowerPrio { + continue + } } if err := DownloadTorrent(animeEp, torrentPrio.ParsedTorrent); err != nil { fmt.Println(fmt.Sprintf("could not download torrent %s", torrentPrio.ParsedTorrent.Torrent.ID), err) } - downloadedTorrents[animeEp] = torrentPrio + downloadedTorrents[animeEp] = torrentPrio // debug output } + duration := time.Since(start) + ShowDebugInfo( parsedTorrentsByAnimeEp, animeListTorrents, @@ -75,6 +79,5 @@ func checkTorrents() { downloadedTorrents, ) - duration := time.Since(start) fmt.Printf("check took %s. sleeping for %s\n", duration.Truncate(time.Millisecond), (PollRate - duration).Truncate(time.Millisecond)) } diff --git a/download_torrent_file.go b/download_torrent_file.go index d0396f5..96c8eca 100644 --- a/download_torrent_file.go +++ b/download_torrent_file.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io" "net/http" "os" @@ -18,7 +17,7 @@ func DownloadTorrent(animeEp model.AnimeEpisode, torrent *model.ParsedTorrent) e torrentFilePath := filepath.Join(TorrentPath, path.Base(torrent.Torrent.Link)) - fmt.Printf("download: %s -> %s\n", torrent.Torrent.Link, torrentFilePath) + //fmt.Printf("download: %s -> %s\n", torrent.Torrent.Link, torrentFilePath) resp, err := http.Get(torrent.Torrent.Link) if err != nil { diff --git a/envvars.go b/envvars.go index 3166500..2880637 100644 --- a/envvars.go +++ b/envvars.go @@ -11,6 +11,9 @@ import ( ) var ( + Uid = envvars.Int("UID", 1000) + Gid = envvars.Int("GID", 1000) + PollRate = envvars.Object("POLL_RATE", 30*time.Minute, time.ParseDuration) AnilistUsername = envvars.String("ANILIST_USERNAME", "username") @@ -29,11 +32,19 @@ var ( template.New("anime-episode-filepath-pattern").Parse, ) + DebugAnimeEpisodePattern = envvars.Object( + "DEBUG_ANIME_LANGUAGE_PATTERN", + template.Must(template.New("anime-episode-filepath-pattern").Parse(`{{.Title.UserPreferred}} episode {{.Episode}}`)), + template.New("anime-episode-pattern").Parse, + ) + AnimeStatus = envvars.ObjectSlice("ANIME_STATUS", ",", []anilist.MediaListStatus{ anilist.MediaListStatusCurrent, anilist.MediaListStatusPlanning, }, ParseMediaListStatus) + DownloadAll = envvars.Bool("DOWNLOAD_ALL_ANIMES", false) + // essential torrent properties MaxResolution = envvars.Object("MAX_RESOLUTION", model.Resolution4K, model.ParseResolution) diff --git a/go.mod b/go.mod index 351b83d..f71505c 100644 --- a/go.mod +++ b/go.mod @@ -9,18 +9,20 @@ require ( git.milar.in/milarin/envvars/v2 v2.0.0 git.milar.in/milarin/gmath v0.0.2 git.milar.in/milarin/slices v0.0.3 - git.milar.in/nyaanime/model v0.0.0-20220822093541-87208e95e7ac - git.milar.in/nyaanime/parsers v0.0.0-20220822100125-2813a7868f6a + git.milar.in/nyaanime/model v0.0.0-20220825203652-2504dcc85f73 + git.milar.in/nyaanime/parsers v0.0.0-20220825163541-c840c68b761d github.com/PuerkitoBio/goquery v1.8.0 github.com/fatih/color v1.13.0 gopkg.in/vansante/go-ffprobe.v2 v2.1.0 ) require ( - git.milar.in/milarin/tprint v0.0.6 // indirect + git.milar.in/milarin/tprint v0.0.7 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.16 // indirect + github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/rivo/uniseg v0.3.4 // indirect golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect ) diff --git a/go.sum b/go.sum index 679c38e..91178ba 100644 --- a/go.sum +++ b/go.sum @@ -24,10 +24,22 @@ git.milar.in/milarin/tprint v0.0.5 h1:T4gszcFME+hW9bwTS/gCelFOwq7aZOX6qTngPiZor7 git.milar.in/milarin/tprint v0.0.5/go.mod h1:KVwwKRggS88ZePLssFXMdisOzJr1SU8hVEUQDzrVtAA= git.milar.in/milarin/tprint v0.0.6 h1:OSiboAnZaeXxx/utZU4nUPSSqamdHYlh89TD02WVOwQ= git.milar.in/milarin/tprint v0.0.6/go.mod h1:KVwwKRggS88ZePLssFXMdisOzJr1SU8hVEUQDzrVtAA= +git.milar.in/milarin/tprint v0.0.7 h1:dvm4l4BhXOie4vtnRMZii0WTLTz2wju8wyUPB/lNeVo= +git.milar.in/milarin/tprint v0.0.7/go.mod h1:UwW/B+0cTCbN5hi0bQBE9rIRgLkq+x4V751rrS2KVoI= git.milar.in/nyaanime/model v0.0.0-20220822093541-87208e95e7ac h1:rM5Mpo4/OJuZaBNZdylag+gi8giWVwDbqsoPjhDP9+g= git.milar.in/nyaanime/model v0.0.0-20220822093541-87208e95e7ac/go.mod h1:OzhQgj0b/Hf9fg8VXYxYt8ONQOvHm8xC44TmS9kQ150= +git.milar.in/nyaanime/model v0.0.0-20220823115053-441386b0def9 h1:5VhVM6S9NfUmjb7qbmv10RK3YE2cM5FdrfVZy2OHTXQ= +git.milar.in/nyaanime/model v0.0.0-20220823115053-441386b0def9/go.mod h1:DqRaTKOh6JRSTVLmoTQ6jrnDvGZvWrfKj3nimr6yuKs= +git.milar.in/nyaanime/model v0.0.0-20220825203105-caabe20b311b h1:u5uv/DGUaboua/GCs3zpEWZI9sI7gNg7apZNgyy0eaw= +git.milar.in/nyaanime/model v0.0.0-20220825203105-caabe20b311b/go.mod h1:DqRaTKOh6JRSTVLmoTQ6jrnDvGZvWrfKj3nimr6yuKs= +git.milar.in/nyaanime/model v0.0.0-20220825203652-2504dcc85f73 h1:fnHpasTZiZyVrXH6Yt0pARibWUveO8pLO+3upK1tboc= +git.milar.in/nyaanime/model v0.0.0-20220825203652-2504dcc85f73/go.mod h1:DqRaTKOh6JRSTVLmoTQ6jrnDvGZvWrfKj3nimr6yuKs= git.milar.in/nyaanime/parsers v0.0.0-20220822100125-2813a7868f6a h1:7vrKOL/vpqJ8YFZ9tmq9iPLoBuLnZgptHWaScyFOFFo= git.milar.in/nyaanime/parsers v0.0.0-20220822100125-2813a7868f6a/go.mod h1:qm6fIFBFs90uz7IJ8RKgDir0K8Fa8isixGLgrtC6kgU= +git.milar.in/nyaanime/parsers v0.0.0-20220825162145-d343540b0f88 h1:jaNVi6srnsozxSjGzQwlf+ouZr0RLOEvrGPrXCttWV0= +git.milar.in/nyaanime/parsers v0.0.0-20220825162145-d343540b0f88/go.mod h1:qm6fIFBFs90uz7IJ8RKgDir0K8Fa8isixGLgrtC6kgU= +git.milar.in/nyaanime/parsers v0.0.0-20220825163541-c840c68b761d h1:LskYoxsMQ3UgPF/6P7p7qm5xYCoe7GrYH1MZnwMTdEI= +git.milar.in/nyaanime/parsers v0.0.0-20220825163541-c840c68b761d/go.mod h1:qm6fIFBFs90uz7IJ8RKgDir0K8Fa8isixGLgrtC6kgU= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= @@ -43,6 +55,11 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9 github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.3.4 h1:3Z3Eu6FGHZWSfNKJTOUiPatWwfc7DzJRU04jFUqJODw= +github.com/rivo/uniseg v0.3.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E= golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= diff --git a/local_file_check.go b/local_file_check.go index 9ed5f55..fdec8fd 100644 --- a/local_file_check.go +++ b/local_file_check.go @@ -78,6 +78,12 @@ func IsCurrentlyDownloading(animeEp model.AnimeEpisode) bool { func SetCurrentlyDownloading(animeEp model.AnimeEpisode) error { animeEpPath := GetAnimeEpFilepath(animeEp, "lock") + + dir := filepath.Dir(animeEpPath) + if err := os.MkdirAll(dir, 0755); err != nil { + return err + } + file, err := os.Create(animeEpPath) if err != nil { defer file.Close() diff --git a/show_debug_info.go b/show_debug_info.go index f71be54..d2c9d6b 100644 --- a/show_debug_info.go +++ b/show_debug_info.go @@ -24,7 +24,7 @@ func ShowDebugInfo( downloadedTorrents map[model.AnimeEpisode]*TorrentPriority) { for animeEp, parsedTorrents := range parsedTorrentsByAnimeEp { - table := tprint.NewTable("id", "resolution", "languages", "subtitles", "seeders", "leechers", "downloads", "trusted", "evaluation") + table := tprint.NewTable("id", "resolution", "languages", "subtitles", "seeders", "leechers", "downloads", "trusted", "group", "evaluation") for _, torrent := range parsedTorrents { eval := getEvaluation( @@ -43,6 +43,7 @@ func ShowDebugInfo( torrent.Torrent.Leechers, torrent.Torrent.Downloads, torrent.Torrent.Trusted, + torrent.Parser.Identity, eval, ) } @@ -61,13 +62,9 @@ func ShowDebugInfo( epState = color.RedString("NOT ON LIST") } - header := BoldText.Sprintf( - "%s episode %s (found: %d) (%s)", - color.MagentaString(animeEp.Anime.Title.Romaji), - color.MagentaString("%d", animeEp.Episode), - len(parsedTorrents), epState, - ) - + b := new(strings.Builder) + DebugAnimeEpisodePattern.Execute(b, AnimePathPatternData{Title: animeEp.Anime.Title, Episode: animeEp.Episode}) + header := BoldText.Sprintf("%s (%s)", color.MagentaString(b.String()), epState) fmt.Println(tprint.FormatHeaderTable(header, table)) } } diff --git a/torrent_filter.go b/torrent_filter.go index 93933e5..14ea96b 100644 --- a/torrent_filter.go +++ b/torrent_filter.go @@ -7,6 +7,10 @@ import ( ) func FilterTorrentsByAnimeList(allTorrents map[model.AnimeEpisode][]*model.ParsedTorrent, animeList map[anilist.MediaID]*anilist.MediaList) map[model.AnimeEpisode][]*model.ParsedTorrent { + if DownloadAll { + return allTorrents + } + filtered := map[model.AnimeEpisode][]*model.ParsedTorrent{} for animeEp, torrents := range allTorrents { if _, ok := animeList[animeEp.Anime.ID]; ok { diff --git a/utils.go b/utils.go index eeac1c1..4ad4e42 100644 --- a/utils.go +++ b/utils.go @@ -19,7 +19,7 @@ var AllMediaListStatuses = []anilist.MediaListStatus{ } func ParseMediaListStatus(str string) (anilist.MediaListStatus, error) { - s := anilist.MediaListStatus(str) + s := anilist.MediaListStatus(strings.ToUpper(str)) allStatusesStr := slices.Map(AllMediaListStatuses, func(status anilist.MediaListStatus) string { return string(status)