Skip to content

Commit

Permalink
fix discovery latestTag logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Feb 8, 2024
1 parent 8f01ffd commit 289f639
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
39 changes: 36 additions & 3 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"time"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -40,6 +41,28 @@ func (c *Controller) RunDiscovery(ctx context.Context) error {
return nil
}

type TaggedNyaa struct {
meta parser.ParsedTitle
seasonEpisodeTag string
entry nyaa.Entry
}

func buildTaggedNyaaList(torrents []nyaa.Entry) []TaggedNyaa {
out := make([]TaggedNyaa, 0, len(torrents))
for _, entry := range torrents {
meta := parser.ParseTitle(entry.Title)
out = append(out, TaggedNyaa{
meta: meta,
seasonEpisodeTag: meta.BuildSeasonEpisodeTag(),
entry: entry,
})
}
sort.Slice(out, func(i, j int) bool {
return compareTags(out[i].seasonEpisodeTag, out[j].seasonEpisodeTag) < 0
})
return out
}

func (c *Controller) DigestMALEntry(ctx context.Context, entry myanimelist.AnimeListEntry) (count int, err error) {
// Build search query for Nyaa.
// For title we filter for english and original titles.
Expand All @@ -57,9 +80,19 @@ func (c *Controller) DigestMALEntry(ctx context.Context, entry myanimelist.Anime
log.Error().Msgf("no torrents found for entry '%s'", entry.GetTitle())
return 0, nil
}
for _, torrent := range torrents {
log.Debug().Str("entry", entry.GetTitle()).Msgf("analyzing torrent '%s'", torrent.Title)
added, err := c.DigestNyaaTorrent(ctx, entry, torrent)
latestTag, err := c.GetLatestTag(ctx, entry)
if err != nil {
return count, fmt.Errorf("getting latest tag: %w", err)
}
taggedNyaaList := buildTaggedNyaaList(torrents)
for _, nyaaEntry := range taggedNyaaList {
// Make sure we only add episodes ahead of the current ones in the qBittorrent.
if compareTags(nyaaEntry.seasonEpisodeTag, latestTag) <= 0 {
continue
}
latestTag = nyaaEntry.seasonEpisodeTag
log.Debug().Str("entry", entry.GetTitle()).Msgf("analyzing torrent '%s'", nyaaEntry.meta.Title)
added, err := c.DigestNyaaTorrent(ctx, entry, nyaaEntry)
if err != nil {
log.Error().Msgf("failed to digest nyaa entry: %s", err)
continue
Expand Down
21 changes: 5 additions & 16 deletions internal/discovery/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/rs/zerolog/log"
"github.com/sonalys/animeman/integrations/myanimelist"
"github.com/sonalys/animeman/integrations/nyaa"
"github.com/sonalys/animeman/integrations/qbittorrent"
"github.com/sonalys/animeman/internal/parser"
"github.com/sonalys/animeman/internal/utils"
Expand Down Expand Up @@ -87,32 +86,22 @@ func (c *Controller) GetLatestTag(ctx context.Context, entry myanimelist.AnimeLi
return getLatestTag(append(torrents1, torrents2...)...), nil
}

func (c *Controller) DigestNyaaTorrent(ctx context.Context, entry myanimelist.AnimeListEntry, torrent nyaa.Entry) (bool, error) {
meta := parser.ParseTitle(torrent.Title)
if meta.IsMultiEpisode && entry.AiringStatus == myanimelist.AiringStatusAiring {
func (c *Controller) DigestNyaaTorrent(ctx context.Context, entry myanimelist.AnimeListEntry, nyaaEntry TaggedNyaa) (bool, error) {
if nyaaEntry.meta.IsMultiEpisode && entry.AiringStatus == myanimelist.AiringStatusAiring {
log.Debug().Msgf("torrent dropped: multi-episode for currently airing")
return false, nil
}
latestTag, err := c.GetLatestTag(ctx, entry)
if err != nil {
return false, fmt.Errorf("getting latest tag: %w", err)
}
// Check if qBittorrent client already has an episode after the current one.
tagCompare := compareTags(meta.BuildSeasonEpisodeTag(), latestTag)
if tagCompare <= 0 {
return false, nil
}
var savePath qbittorrent.SavePath
if c.dep.Config.CreateShowFolder {
savePath = qbittorrent.SavePath(fmt.Sprintf("%s/%s", c.dep.Config.DownloadPath, entry.GetTitle()))
} else {
savePath = qbittorrent.SavePath(c.dep.Config.DownloadPath)
}
tags := meta.BuildTorrentTags()
err = c.dep.QB.AddTorrent(ctx,
tags := nyaaEntry.meta.BuildTorrentTags()
err := c.dep.QB.AddTorrent(ctx,
tags,
savePath,
qbittorrent.TorrentURL{torrent.Link},
qbittorrent.TorrentURL{nyaaEntry.entry.Link},
qbittorrent.Category(c.dep.Config.Category),
)
if err != nil {
Expand Down

0 comments on commit 289f639

Please sign in to comment.