2021-03-19 13:54:04 +01:00
|
|
|
# this is a small and single-purpose wrapper around the TMDB API
|
|
|
|
|
2021-03-19 01:21:09 +01:00
|
|
|
import json
|
|
|
|
import random
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
class Tmdb:
|
2021-03-19 13:54:04 +01:00
|
|
|
def __init__(self, apikey, language):
|
|
|
|
self.baseurl = 'https://api.themoviedb.org/3/'
|
2021-03-19 01:21:09 +01:00
|
|
|
self.apikey = apikey
|
2021-03-19 13:54:04 +01:00
|
|
|
self.language = language
|
2021-03-19 01:21:09 +01:00
|
|
|
|
2021-03-19 14:46:48 +01:00
|
|
|
def fetch_from_tmdb(self, path, parameters):
|
2021-03-19 14:34:14 +01:00
|
|
|
apikey = "?api_key=%s" % self.apikey
|
2021-03-19 14:46:48 +01:00
|
|
|
url = self.baseurl + path + apikey + parameters
|
2021-03-19 01:21:09 +01:00
|
|
|
json_url = urllib.urlopen(url)
|
|
|
|
data = json.loads(json_url.read())
|
2021-03-19 14:34:14 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
def get_tmdbid(self, imdbid):
|
|
|
|
print("getting tmdbid for imdbid %s" % imdbid)
|
2021-03-19 14:46:48 +01:00
|
|
|
data = self.fetch_from_tmdb(
|
|
|
|
path = 'find/%s' % imdbid,
|
|
|
|
parameters = '&external_source=imdb_id'
|
|
|
|
)
|
2021-03-19 01:21:09 +01:00
|
|
|
try:
|
|
|
|
tmdbid = data['movie_results'][0]['id']
|
|
|
|
print("tmdbid is %d" % tmdbid)
|
|
|
|
except:
|
|
|
|
tmdbid = 0
|
|
|
|
print("tmdbid could not be found")
|
|
|
|
return tmdbid
|
|
|
|
|
2021-03-19 13:54:04 +01:00
|
|
|
def get_recommendations(self, movieid, choice):
|
2021-03-19 01:21:09 +01:00
|
|
|
print("getting %s for %d" % (choice, movieid))
|
2021-03-19 14:46:48 +01:00
|
|
|
data = self.fetch_from_tmdb(
|
|
|
|
path = 'movie/%d/%s' % (movieid, choice),
|
|
|
|
parameters = '&language=%s' % self.language
|
|
|
|
)
|
2021-03-19 01:21:09 +01:00
|
|
|
results = []
|
|
|
|
for result in data['results']:
|
|
|
|
results.append({'title': result['title'], 'movieid': result['id']})
|
|
|
|
return results
|
|
|
|
|
2021-03-19 13:54:04 +01:00
|
|
|
def get_movie_trailers(self, movieid):
|
2021-03-19 01:21:09 +01:00
|
|
|
print("getting trailers for %d" % movieid)
|
2021-03-19 14:46:48 +01:00
|
|
|
data = self.fetch_from_tmdb(
|
|
|
|
path = 'movie/%d' % movieid,
|
|
|
|
parameters = '&language=%s&append_to_response=videos' % self.language
|
|
|
|
)
|
2021-03-19 01:21:09 +01:00
|
|
|
results = []
|
|
|
|
for result in data['videos']['results']:
|
|
|
|
if result['site'] == 'YouTube':
|
|
|
|
location = 'plugin://plugin.video.youtube/play/?video_id=%s' % result['key']
|
|
|
|
else:
|
|
|
|
next
|
|
|
|
results.append({'title': result['name'], 'type': result['type'], 'location': location})
|
|
|
|
return results
|
|
|
|
|
2021-03-19 13:54:04 +01:00
|
|
|
def get_trailers(self, recommendations, cliptype, count):
|
2021-03-19 01:21:09 +01:00
|
|
|
results = []
|
|
|
|
for recommendation in recommendations[:10]:
|
2021-03-19 13:54:04 +01:00
|
|
|
all_trailers = self.get_movie_trailers(recommendation['movieid'])
|
2021-03-19 01:21:09 +01:00
|
|
|
trailers = []
|
|
|
|
for trailer in all_trailers:
|
|
|
|
if trailer['type'] == cliptype:
|
|
|
|
trailers.append(trailer)
|
|
|
|
if len(trailers) > 0:
|
|
|
|
random.shuffle(trailers)
|
|
|
|
trailer = trailers[0]
|
|
|
|
results.append(trailer)
|
|
|
|
if len(results) == count:
|
|
|
|
break
|
|
|
|
return results
|