Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Nov 24, 2024
1 parent ae9ec4b commit 4742e86
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can set your own config path with the env `CONFIG_PATH`.

```yaml
# config.yaml
logLevel: info # (debug,info,error).
animeList:
type: myanimelist # (myanimelist|anilist).
username: YOUR_USERNAME # Replace with your username.
Expand Down
1 change: 1 addition & 0 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
if err != nil {
log.Fatal().Msgf("config is not valid: %s", err)
}
zerolog.SetGlobalLevel(config.LogLevel.Convert())
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
c := discovery.New(discovery.Dependencies{
NYAA: nyaa.New(nyaa.Config{
Expand Down
21 changes: 21 additions & 0 deletions internal/configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sonalys/animeman/internal/utils"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -104,10 +105,30 @@ func (c TorrentConfig) Validate() error {
return nil
}

type LogLevel string

const (
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
LogLevelError LogLevel = "error"
)

type Config struct {
AnimeListConfig `yaml:"animeList"`
RSSConfig `yaml:"rssConfig"`
TorrentConfig `yaml:"torrentConfig"`
LogLevel LogLevel `yaml:"logLevel"`
}

func (l LogLevel) Convert() zerolog.Level {
switch l {
case LogLevelDebug:
return zerolog.DebugLevel
case LogLevelError:
return zerolog.ErrorLevel
default:
return zerolog.InfoLevel
}
}

func (c Config) Validate() error {
Expand Down
6 changes: 5 additions & 1 deletion internal/discovery/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func getDownloadableEntries(
useBatch := latestTag == "" && animeStatus == animelist.AiringStatusAired
parsedEntries := parseAndSort(animeListEntry, entries)
if useBatch {
log.Debug().Msg("anime is already aired, no downloaded entries. activating batch search")
return utils.Filter(parsedEntries, filterBatchEntries)
}
return episodeFilterNew(parsedEntries, latestTag, !useBatch)
Expand All @@ -154,7 +155,7 @@ func (c *Controller) NyaaSearch(ctx context.Context, entry animelist.Entry) ([]n
sourceQuery := nyaa.QueryOr(c.dep.Config.Sources)
qualityQuery := nyaa.QueryOr(c.dep.Config.Qualitites)
entries, err := c.dep.NYAA.List(ctx, titleQuery, sourceQuery, qualityQuery)
log.Debug().Str("entry", entry.Titles[0]).Msgf("found %d torrents", len(entries))
log.Debug().Str("entry", entry.Titles[0]).Msgf("found %d torrent candidates", len(entries))
if err != nil {
return nil, fmt.Errorf("getting nyaa list: %w", err)
}
Expand All @@ -170,17 +171,20 @@ func (c *Controller) DigestAnimeListEntry(ctx context.Context, entry animelist.E
nyaaEntries, err := c.NyaaSearch(ctx, entry)
// There should always be torrents for entries, if there aren't we can just exit the routine.
if len(nyaaEntries) == 0 {
log.Debug().Any("title", entry.Titles).Msg("no nyaa entries found")
return
}
latestTag, err := c.TorrentGetLatestEpisodes(ctx, entry)
if err != nil {
return fmt.Errorf("getting latest tag: %w", err)
}
log.Debug().Str("latestTag", latestTag).Msg("looking for torrent candidates")
for _, nyaaEntry := range getDownloadableEntries(entry, nyaaEntries, latestTag, entry.AiringStatus) {
if err := c.TorrentDigestNyaa(ctx, entry, nyaaEntry); err != nil {
log.Error().Msgf("failed to digest nyaa entry: %s", err)
continue
}
}
log.Debug().Msg("discovery finished")
return
}
8 changes: 5 additions & 3 deletions internal/discovery/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func filterPublishedAfterDate(t time.Time) func(e nyaa.Entry) bool {

// Guarantees that the main title matches for the anime list entry and the nyaa entry.
func filterTitleMatch(alEntry animelist.Entry) func(e nyaa.Entry) bool {
parsedTitles := utils.Map(alEntry.Titles, func(title string) string {
return parser.Parse(title).Title
})
return func(e nyaa.Entry) bool {
meta := parser.Parse(e.Title)

for _, title := range alEntry.Titles {
alMeta := parser.Parse(title)
if strings.EqualFold(meta.Title, alMeta.Title) {
for _, parsedTitle := range parsedTitles {
if strings.EqualFold(meta.Title, parsedTitle) {
return true
}
}
Expand Down
9 changes: 0 additions & 9 deletions internal/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,3 @@ outer:
}
return out
}

func Find[T any](in []T, f func(T) bool) (*T, bool) {
for i := range in {
if f(in[i]) {
return &in[i], true
}
}
return nil, false
}
34 changes: 34 additions & 0 deletions internal/utils/slices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils_test

import (
"strconv"
"testing"

"github.com/sonalys/animeman/internal/utils"
"github.com/stretchr/testify/require"
)

func Test_Map(t *testing.T) {
t.Run("should run and return new slice of type with value of func", func(t *testing.T) {
from := []int{1, 2, 3}

var to []string = utils.Map(from, func(cur int) string {
return strconv.Itoa(cur)
})

require.Equal(t, []string{"1", "2", "3"}, to)
})
}

func Test_Filter(t *testing.T) {
t.Run("should filter with and condition for all filters", func(t *testing.T) {
from := []int{1, 2, 3}

got := utils.Filter(from,
func(cur int) bool { return cur == 2 },
func(cur int) bool { return true },
)

require.Equal(t, []int{2}, got)
})
}

0 comments on commit 4742e86

Please sign in to comment.