-
Notifications
You must be signed in to change notification settings - Fork 3
/
tmdb.go
381 lines (349 loc) · 9.15 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2014, Amahi. All rights reserved.
// Use of this source code is governed by the
// license that can be found in the LICENSE file.
// Golang library for requesting metadata from themoviedb.org
// It's used by
//
// 1) Initializing the library via Init(), with the caller's API
// key from http://www.themoviedb.org like
//
// 2) Calling MovieData() to get the actual data, like
//
// For example
//
// package main
//
// import "fmt"
// import "github.com/amahi/go-themoviedb"
//
// func main() {
// tmdb := tmdb.Init("your-api-key")
// metadata, err := tmdb.MovieData("Pulp Fiction")
// if err != nil {
// fmt.Printf("Error: %s\n", err)
// } else {
// fmt.Printf("TMDb Metadata: %s\n", metadata)
// }
// }
//
// the metadata is returned in XML format according to TMDb guidelines.
//
package tmdb
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
const base_url string = "http://api.themoviedb.org/3"
type TMDb struct {
api_key string
config *tmdbConfig
}
func Init(api_key string) *TMDb {
return &TMDb{api_key: api_key}
}
type filtered_output struct {
Title string `json:"title"`
Artwork string `json:"artwork"`
Release_date string `json:"year"`
}
// response of search/multi
type tmdbResponse struct {
Page int
Results []tmdbResult
Total_pages int
Total_results int
}
// results format from Tmdb
type tmdbResult struct {
Adult bool
Name string
Backdrop_path string
Id int
Original_name string
Original_title string
First_air_date string
Release_date string
Poster_path string
Title string
Media_type string
Profile_path string
}
// response of config
type tmdbConfig struct {
Images imageConfig
}
// Image configurtion
type imageConfig struct {
Base_url string
Secure_base_url string
//possible sizes for images
Backdrop_sizes []string
Logo_sizes []string
Poster_sizes []string
Profile_sizes []string
Still_sizes []string
}
// Movie metadata structure
type movieMetadata struct {
Id int
Media_type string
Backdrop_path string
Poster_path string
Credits tmdbCredits
Config *tmdbConfig
Imdb_id string
Overview string
Title string
Release_date string
}
type tmdbCredits struct {
Id int
Cast []tmdbCast
Crew []tmdbCrew
}
type tmdbCast struct {
Character string
Name string
Profile_path string
}
type tmdbCrew struct {
Department string
Name string
Job string
Profile_path string
}
// The main call for getting movie data media_name is the (plain) name of
// the movie information to be retrieved without year or other information
func (tmdb *TMDb) MovieData(media_name string) (string, error) {
results, err := tmdb.searchMovie(media_name)
if err != nil {
return "", err
}
if results.Total_results == 0 {
return "", errors.New("No results found at TMDb")
}
if results.Results[0].Media_type == "person" {
return "", errors.New("Metadata for persons not supported")
}
if results.Results[0].Media_type == "tv" {
return "", errors.New("Metadata for tv not supported inside a call for movie data")
}
// otherwise
movie_details, err := tmdb.getMovieDetails(strconv.Itoa(results.Results[0].Id))
if err != nil {
return "", err
}
movie_details.Credits, err = tmdb.getMovieCredits(strconv.Itoa(results.Results[0].Id))
if err != nil {
return "", err
}
movie_details.Config, err = tmdb.getConfig()
if err != nil {
return "", err
}
movie_details.Id = results.Results[0].Id
movie_details.Media_type = "movie"
metadata, err := json.Marshal(movie_details)
if err != nil {
return "", err
}
return string(metadata), nil
}
// Search on TMDb for TV, persons and Movies with a given name
func (tmdb *TMDb) searchTmdbMulti(media_name string) (tmdbResponse, error) {
var resp tmdbResponse
res, err := http.Get(base_url + "/search/multi?api_key=" + tmdb.api_key + "&query=" + url.QueryEscape(media_name))
if err != nil {
return resp, err
}
if res.StatusCode != 200 {
return resp, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return tmdbResponse{}, err
}
if err := json.Unmarshal(body, &resp); err != nil {
return tmdbResponse{}, err
}
return resp, nil
}
// Search on TMDb for Movies with a given name
func (tmdb *TMDb) searchMovie(media_name string) (tmdbResponse, error) {
var resp tmdbResponse
res, err := http.Get(base_url + "/search/movie?api_key=" + tmdb.api_key + "&query=" + url.QueryEscape(media_name))
if err != nil {
return resp, err
}
if res.StatusCode != 200 {
return resp, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return tmdbResponse{}, err
}
if err := json.Unmarshal(body, &resp); err != nil {
return tmdbResponse{}, err
}
return resp, nil
}
// Search on TMDb for Tv Shows with a given name
func (tmdb *TMDb) searchTmdbTv(media_name string) (tmdbResponse, error) {
var resp tmdbResponse
res, err := http.Get(base_url + "/search/tv?api_key=" + tmdb.api_key + "&query=" + url.QueryEscape(media_name))
if err != nil {
return resp, err
}
if res.StatusCode != 200 {
return resp, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return tmdbResponse{}, err
}
if err := json.Unmarshal(body, &resp); err != nil {
return tmdbResponse{}, err
}
return resp, nil
}
// Get configurations from TMDb
func (tmdb *TMDb) getConfig() (*tmdbConfig, error) {
if tmdb.config == nil || tmdb.config.Images.Base_url == "" {
var conf = &tmdbConfig{}
res, err := http.Get(base_url + "/configuration?api_key=" + tmdb.api_key)
if err != nil {
return conf, err
}
if res.StatusCode != 200 {
return conf, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return &tmdbConfig{}, err
}
if err := json.Unmarshal(body, &conf); err != nil {
return &tmdbConfig{}, err
}
tmdb.config = conf
}
return tmdb.config, nil
}
// Get basic information for movie
func (tmdb *TMDb) getMovieDetails(MediaId string) (movieMetadata, error) {
var met movieMetadata
res, err := http.Get(base_url + "/movie/" + MediaId + "?api_key=" + tmdb.api_key)
if err != nil {
return met, err
}
if res.StatusCode != 200 {
return met, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return movieMetadata{}, err
}
if err := json.Unmarshal(body, &met); err != nil {
return movieMetadata{}, err
}
return met, nil
}
// Get credits for movie
func (tmdb *TMDb) getMovieCredits(MediaId string) (tmdbCredits, error) {
var cred tmdbCredits
res, err := http.Get(base_url + "/movie/" + MediaId + "/credits?api_key=" + tmdb.api_key)
if err != nil {
return cred, err
}
if res.StatusCode != 200 {
return cred, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return tmdbCredits{}, err
}
if err := json.Unmarshal(body, &cred); err != nil {
return tmdbCredits{}, err
}
return cred, nil
}
// Get basic information for Tv
func (tmdb *TMDb) getTmdbTvDetails(MediaId string) (movieMetadata, error) {
var met movieMetadata
res, err := http.Get(base_url + "/tv/" + MediaId + "?api_key=" + tmdb.api_key)
if err != nil {
return met, err
}
if res.StatusCode != 200 {
return met, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return movieMetadata{}, err
}
if err := json.Unmarshal(body, &met); err != nil {
return movieMetadata{}, err
}
return met, nil
}
// Get credits for Tv
func (tmdb *TMDb) getTmdbTvCredits(MediaId string) (tmdbCredits, error) {
var cred tmdbCredits
res, err := http.Get(base_url + "/tv/" + MediaId + "/credits?api_key=" + tmdb.api_key)
if err != nil {
return cred, err
}
if res.StatusCode != 200 {
return cred, error_status(res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return tmdbCredits{}, err
}
if err := json.Unmarshal(body, &cred); err != nil {
return tmdbCredits{}, err
}
return cred, nil
}
// Transform the simplified movie metadata in JSON format
// This output is rather arbitrary to our (Amahi's) needs and could be customized a little
func (tmdb *TMDb) ToJSON(data string) (string, error) {
var f filtered_output
var det movieMetadata
if err := json.Unmarshal([]byte(data), &det); err != nil {
return "", err
}
f.Title = det.Title
f.Release_date = det.Release_date
if len(det.Release_date) > 4 {
f.Release_date = det.Release_date[0:4]
}
// default width of the poster
size := det.poster_size("w154")
f.Artwork = det.Config.Images.Base_url + size + det.Poster_path
metadata, err := json.Marshal(f)
if err != nil {
return "", err
}
return string(metadata), nil
}
// return the requested size, the original if there are none
// and the first one if the requested size does not exist
func (md *movieMetadata) poster_size(size string) string {
if len(md.Config.Images.Poster_sizes) == 0 {
return "original"
}
for i := range md.Config.Images.Poster_sizes {
if md.Config.Images.Poster_sizes[i] == size {
return size
}
}
return md.Config.Images.Poster_sizes[0]
}
func error_status(status int) error {
return errors.New(fmt.Sprintf("Status Code %d received from TMDb", status))
}