refactored tmdb stuff into a class
This commit is contained in:
67
tmdb.py
Normal file
67
tmdb.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
import random
|
||||
import urllib
|
||||
|
||||
class Tmdb:
|
||||
def __init__(self, apikey):
|
||||
self.apikey = apikey
|
||||
|
||||
def get_tmdbid(self, imdbid):
|
||||
print("getting tmdbid for imdbid %s" % imdbid)
|
||||
baseurl = 'https://api.themoviedb.org/3/'
|
||||
url = baseurl + 'find/%s?api_key=%s&external_source=imdb_id'
|
||||
url = url % (imdbid, self.apikey)
|
||||
json_url = urllib.urlopen(url)
|
||||
data = json.loads(json_url.read())
|
||||
print(data)
|
||||
try:
|
||||
tmdbid = data['movie_results'][0]['id']
|
||||
print("tmdbid is %d" % tmdbid)
|
||||
except:
|
||||
tmdbid = 0
|
||||
print("tmdbid could not be found")
|
||||
return tmdbid
|
||||
|
||||
def get_recommendations(self, movieid, language, choice):
|
||||
print("getting %s for %d" % (choice, movieid))
|
||||
baseurl = 'https://api.themoviedb.org/3/'
|
||||
url = baseurl + 'movie/%d/%s?api_key=%s&language=%s'
|
||||
url = url % (movieid, choice, self.apikey, language)
|
||||
json_url = urllib.urlopen(url)
|
||||
data = json.loads(json_url.read())
|
||||
results = []
|
||||
for result in data['results']:
|
||||
results.append({'title': result['title'], 'movieid': result['id']})
|
||||
return results
|
||||
|
||||
def get_movie_trailers(self, movieid, language):
|
||||
print("getting trailers for %d" % movieid)
|
||||
baseurl = 'https://api.themoviedb.org/3/'
|
||||
url = baseurl + 'movie/%d?api_key=%s&language=%s&append_to_response=videos'
|
||||
url = url % (movieid, self.apikey, language)
|
||||
json_url = urllib.urlopen(url)
|
||||
data = json.loads(json_url.read())
|
||||
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
|
||||
|
||||
def get_trailers(self, recommendations, language, cliptype, count):
|
||||
results = []
|
||||
for recommendation in recommendations[:10]:
|
||||
all_trailers = self.get_movie_trailers(recommendation['movieid'], language)
|
||||
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
|
||||
Reference in New Issue
Block a user