-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmdb.go
138 lines (124 loc) · 3.23 KB
/
tmdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package letswatch
import (
"context"
"errors"
"fmt"
"time"
"github.com/apex/log"
tmdb "github.com/cyruzin/golang-tmdb"
"github.com/go-redis/cache/v8"
)
type TMDBService interface {
GetWithIMDBID(context.Context, string) (*tmdb.MovieDetails, error)
GetStreamingChannels(id int) ([]string, error)
}
type TMDBServiceOp struct {
client *Client
tmdbClient *tmdb.Client
}
type TMDBMovie struct {
Title string `json:"title,omitempty"`
ReleaseYear int `json:"release_year,omitempty"`
}
func NewTMDBClient(key string) (*tmdb.Client, error) {
if key == "" {
return nil, errors.New("ErrNoTMDBKey")
}
return tmdb.Init(key)
}
type TMDBClientWithCache struct {
client *tmdb.Client
Cache *cache.Cache
}
/*
func (svc *TMDBServiceOp) NewTMDBClientWithCache() (*TMDBClientWithCache, error) {
tc, err := NewTMDBClient(svc.client.Config.TMDBKey)
if err != nil {
return nil, err
}
t := &TMDBClientWithCache{
client: tc,
}
var redisH string
if os.Getenv("REDIS_HOST") != "" {
redisH = os.Getenv("REDIS_HOST")
}
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%v:6379", redisH),
Password: "", // no password set
DB: 0, // use default DB
})
t.Cache = cache.New(&cache.Options{
Redis: rdb,
LocalCache: cache.NewTinyLFU(1000, time.Minute),
})
return t, nil
}
*/
func (t *TMDBServiceOp) GetWithIMDBID(ctx context.Context, imdbID string) (*tmdb.MovieDetails, error) {
key := fmt.Sprintf("/letswatch/tmdb/by-imdb-id/%s", imdbID)
if ctx == nil {
ctx = context.Background()
}
var movie *tmdb.MovieDetails
var inCache bool
if t.client.Cache != nil {
log.WithFields(log.Fields{
"key": key,
"ctx": ctx,
}).Debug("Using cache for lookup")
if err := t.client.Cache.Get(ctx, key, &movie); err == nil {
log.WithField("key", key).Debug("Found page in cache")
inCache = true
} else {
log.WithError(err).WithField("key", key).Debug("TMDB Entry NOT in cache")
}
}
if !inCache {
options := map[string]string{}
options["external_source"] = "imdb_id"
search, err := t.tmdbClient.GetFindByID(imdbID, options)
if err != nil {
return nil, err
}
if len(search.MovieResults) == 0 {
return nil, errors.New("ErrNoMovieFound")
} else if len(search.MovieResults) > 1 {
log.WithFields(log.Fields{
"imdb_id": imdbID,
"count": len(search.MovieResults),
}).Warn("Found more than one movie, using the first one")
}
thing := search.MovieResults[0]
options = map[string]string{}
options["append_to_response"] = "credits"
movie, err = t.tmdbClient.GetMovieDetails(int(thing.ID), options)
if err != nil {
return nil, err
}
if t.client.Cache != nil {
if err := t.client.Cache.Set(&cache.Item{
Ctx: ctx,
Key: key,
Value: movie,
TTL: time.Hour * 24,
}); err != nil {
log.WithError(err).Warn("Error Writing TMDB Film to Cache")
}
}
}
return movie, nil
}
func (svc *TMDBServiceOp) GetStreamingChannels(id int) ([]string, error) {
watchP, err := svc.tmdbClient.GetMovieWatchProviders(id, nil)
if err != nil {
return nil, err
}
providers := []string{}
if val, ok := watchP.MovieWatchProvidersResults.Results["US"]; ok {
for _, item := range val.Flatrate {
providers = append(providers, item.ProviderName)
}
}
return providers, nil
}