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 15:07:55 +01:00
|
|
|
# fetches data from TMDB API
|
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
|
|
|
|
|
2021-03-19 15:07:55 +01:00
|
|
|
# finds TMDB-id for a known IMDB-id, return 0 if nothing is found
|
2021-03-19 14:34:14 +01:00
|
|
|
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 15:07:55 +01:00
|
|
|
# get recommendations for a movie id
|
|
|
|
# choice is either "recommendations" or "similar"
|
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']:
|
2021-03-19 15:07:55 +01:00
|
|
|
results.append({
|
|
|
|
'title': result['title'],
|
|
|
|
'movieid': result['id']
|
|
|
|
})
|
2021-03-19 01:21:09 +01:00
|
|
|
return results
|
|
|
|
|
2021-03-19 15:07:55 +01:00
|
|
|
# get trailers, teasers, clips... for a movie id
|
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:
|
2021-03-19 15:07:55 +01:00
|
|
|
# skip other sites
|
2021-03-19 01:21:09 +01:00
|
|
|
next
|
2021-03-19 15:07:55 +01:00
|
|
|
results.append({
|
|
|
|
'title': result['name'],
|
|
|
|
'type': result['type'],
|
|
|
|
'location': location
|
|
|
|
})
|
2021-03-19 01:21:09 +01:00
|
|
|
return results
|
|
|
|
|
2021-03-19 15:07:55 +01:00
|
|
|
# get a number of trailers of a certain kind for a list of recommended movies
|
2021-03-19 13:54:04 +01:00
|
|
|
def get_trailers(self, recommendations, cliptype, count):
|
2021-03-19 01:21:09 +01:00
|
|
|
results = []
|
2021-03-19 15:10:37 +01:00
|
|
|
# loop over ten out of the list of recommendations
|
|
|
|
random.shuffle(recommendations)
|
2021-03-19 01:21:09 +01:00
|
|
|
for recommendation in recommendations[:10]:
|
2021-03-19 13:54:04 +01:00
|
|
|
all_trailers = self.get_movie_trailers(recommendation['movieid'])
|
2021-03-19 15:07:55 +01:00
|
|
|
# filter to get only a certain kind of trailer
|
2021-03-19 01:21:09 +01:00
|
|
|
trailers = []
|
|
|
|
for trailer in all_trailers:
|
|
|
|
if trailer['type'] == cliptype:
|
|
|
|
trailers.append(trailer)
|
2021-03-19 15:07:55 +01:00
|
|
|
# get a random clip for this recommendation
|
2021-03-19 01:21:09 +01:00
|
|
|
if len(trailers) > 0:
|
|
|
|
random.shuffle(trailers)
|
|
|
|
trailer = trailers[0]
|
|
|
|
results.append(trailer)
|
2021-03-19 15:07:55 +01:00
|
|
|
# quit if enough clips have been collected
|
2021-03-19 01:21:09 +01:00
|
|
|
if len(results) == count:
|
|
|
|
break
|
|
|
|
return results
|