diff --git a/addon.py b/addon.py index 27ec44a..f1e0e0a 100644 --- a/addon.py +++ b/addon.py @@ -54,19 +54,18 @@ def conduct_program(program_file, feature): entry = {'type': 'video', 'data': location} program.append(entry) elif settings['source'] == 'tmdbtrailer': - TMDB = tmdb.Tmdb(apikey = settings['apikey']) + TMDB = tmdb.Tmdb(apikey = settings['apikey'], language = settings['language']) tmdbid = int(feature['tmdbid']) imdbid = feature['imdbid'] - language = settings['language'] choice = settings['choice'] trailertype = settings['type'] count = settings['count'] if not tmdbid: tmdbid = TMDB.get_tmdbid(imdbid) if tmdbid: - movies = TMDB.get_recommendations(tmdbid, language, choice) + movies = TMDB.get_recommendations(tmdbid, choice) random.shuffle(movies) - trailers = TMDB.get_trailers(movies, language, trailertype, count) + trailers = TMDB.get_trailers(movies, trailertype, count) else: print("TODO: this feature has no tmdb id, find someting else to play") trailers = [] diff --git a/tmdb.py b/tmdb.py index d23671f..55dad91 100644 --- a/tmdb.py +++ b/tmdb.py @@ -1,15 +1,18 @@ +# this is a small and single-purpose wrapper around the TMDB API + import json import random import urllib class Tmdb: - def __init__(self, apikey): + def __init__(self, apikey, language): + self.baseurl = 'https://api.themoviedb.org/3/' self.apikey = apikey + self.language = language 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 = self.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()) @@ -22,11 +25,10 @@ class Tmdb: print("tmdbid could not be found") return tmdbid - def get_recommendations(self, movieid, language, choice): + def get_recommendations(self, movieid, 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) + url = self.baseurl + 'movie/%d/%s?api_key=%s&language=%s' + url = url % (movieid, choice, self.apikey, self.language) json_url = urllib.urlopen(url) data = json.loads(json_url.read()) results = [] @@ -34,11 +36,10 @@ class Tmdb: results.append({'title': result['title'], 'movieid': result['id']}) return results - def get_movie_trailers(self, movieid, language): + def get_movie_trailers(self, movieid): 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) + url = self.baseurl + 'movie/%d?api_key=%s&language=%s&append_to_response=videos' + url = url % (movieid, self.apikey, self.language) json_url = urllib.urlopen(url) data = json.loads(json_url.read()) results = [] @@ -50,10 +51,10 @@ class Tmdb: results.append({'title': result['name'], 'type': result['type'], 'location': location}) return results - def get_trailers(self, recommendations, language, cliptype, count): + def get_trailers(self, recommendations, cliptype, count): results = [] for recommendation in recommendations[:10]: - all_trailers = self.get_movie_trailers(recommendation['movieid'], language) + all_trailers = self.get_movie_trailers(recommendation['movieid']) trailers = [] for trailer in all_trailers: if trailer['type'] == cliptype: