2021-02-14 16:18:26 +00:00
|
|
|
package media
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-03-03 16:53:54 +00:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2022-04-01 20:42:05 +01:00
|
|
|
|
2022-04-14 16:18:06 +01:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
2021-02-14 16:18:26 +00:00
|
|
|
"github.com/l3uddz/nabarr/logger"
|
|
|
|
|
"github.com/l3uddz/nabarr/media/omdb"
|
2026-03-03 16:53:54 +00:00
|
|
|
"github.com/l3uddz/nabarr/media/tmdb"
|
2021-02-14 16:18:26 +00:00
|
|
|
"github.com/l3uddz/nabarr/media/trakt"
|
2021-02-19 23:31:23 +00:00
|
|
|
"github.com/l3uddz/nabarr/media/tvdb"
|
2026-03-03 16:53:54 +00:00
|
|
|
"github.com/l3uddz/nabarr/util"
|
2021-02-14 16:18:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
trakt *trakt.Client
|
|
|
|
|
omdb *omdb.Client
|
2021-02-19 23:31:23 +00:00
|
|
|
tvdb *tvdb.Client
|
2026-03-03 16:53:54 +00:00
|
|
|
tmdb *tmdb.Client
|
2021-02-14 16:18:26 +00:00
|
|
|
|
|
|
|
|
log zerolog.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 16:53:54 +00:00
|
|
|
func (c *Client) EnrichFeedItemWithTmdbId(item *FeedItem) {
|
|
|
|
|
if c.tmdb == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// skip if a valid ID is already present
|
|
|
|
|
hasId := (item.TvdbId != "" && !util.StringSliceContains([]string{"0", "1"}, item.TvdbId)) ||
|
|
|
|
|
(item.ImdbId != "" && strings.HasPrefix(item.ImdbId, "tt")) ||
|
|
|
|
|
(item.TmdbId != "" && !util.StringSliceContains([]string{"0", "1"}, item.TmdbId))
|
|
|
|
|
if hasId {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
title, year := tmdb.ExtractTitleAndYear(item.Title)
|
|
|
|
|
if title == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tmdbId int
|
|
|
|
|
switch {
|
|
|
|
|
case util.ContainsMovieCategory(item.Categories):
|
|
|
|
|
if id, err := c.tmdb.SearchMovies(title, year); err == nil && id > 0 {
|
|
|
|
|
tmdbId = id
|
|
|
|
|
}
|
|
|
|
|
case util.ContainsTvCategory(item.Categories):
|
|
|
|
|
if id, err := c.tmdb.SearchShows(title, year); err == nil && id > 0 {
|
|
|
|
|
tmdbId = id
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tmdbId > 0 {
|
|
|
|
|
item.TmdbId = strconv.Itoa(tmdbId)
|
|
|
|
|
c.log.Info().
|
|
|
|
|
Str("release", item.Title).
|
|
|
|
|
Str("clean_title", title).
|
|
|
|
|
Int("year", year).
|
|
|
|
|
Int("tmdb_id", tmdbId).
|
|
|
|
|
Msg("Enriched item with TMDB ID via title search")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:18:26 +00:00
|
|
|
func New(cfg *Config) (*Client, error) {
|
2021-02-19 23:31:23 +00:00
|
|
|
// validate trakt configured (it is mandatory)
|
2021-02-14 16:18:26 +00:00
|
|
|
if cfg.Trakt.ClientId == "" {
|
|
|
|
|
return nil, fmt.Errorf("trakt: no client_id specified")
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 16:53:54 +00:00
|
|
|
var tmdbClient *tmdb.Client
|
|
|
|
|
if cfg.Tmdb.ApiKey != "" {
|
|
|
|
|
tmdbClient = tmdb.New(&cfg.Tmdb)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:18:26 +00:00
|
|
|
return &Client{
|
2021-02-19 23:31:23 +00:00
|
|
|
trakt: trakt.New(&cfg.Trakt),
|
|
|
|
|
omdb: omdb.New(&cfg.Omdb),
|
|
|
|
|
tvdb: tvdb.New(&cfg.Tvdb),
|
2026-03-03 16:53:54 +00:00
|
|
|
tmdb: tmdbClient,
|
2021-02-14 16:18:26 +00:00
|
|
|
|
2022-04-14 16:18:06 +01:00
|
|
|
log: logger.Child(logger.WithLevel(cfg.Verbosity)),
|
2021-02-14 16:18:26 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|