diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2bc03a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyo diff --git a/addon.py b/addon.py index fe6e3b2..27ec44a 100644 --- a/addon.py +++ b/addon.py @@ -5,7 +5,8 @@ import xbmcvfs import json import random import sys -import urllib + +import tmdb ADDON = xbmcaddon.Addon() CWD = ADDON.getAddonInfo('path').decode('utf-8') @@ -37,66 +38,6 @@ def files_from_dir(count, location): files[i] = location + files[i] return files -def get_tmdbid(apikey, 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, 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(apikey, 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, 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(apikey, 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, 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(apikey, recommendations, language, cliptype, count): - results = [] - for recommendation in recommendations[:10]: - all_trailers = get_movie_trailers(apikey, 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 - def conduct_program(program_file, feature): filehandle = xbmcvfs.File(program_file) program_json = filehandle.read() @@ -113,7 +54,7 @@ def conduct_program(program_file, feature): entry = {'type': 'video', 'data': location} program.append(entry) elif settings['source'] == 'tmdbtrailer': - apikey = settings['apikey'] + TMDB = tmdb.Tmdb(apikey = settings['apikey']) tmdbid = int(feature['tmdbid']) imdbid = feature['imdbid'] language = settings['language'] @@ -121,11 +62,11 @@ def conduct_program(program_file, feature): trailertype = settings['type'] count = settings['count'] if not tmdbid: - tmdbid = get_tmdbid(apikey, imdbid) + tmdbid = TMDB.get_tmdbid(imdbid) if tmdbid: - movies = get_recommendations(apikey, tmdbid, language, choice) + movies = TMDB.get_recommendations(tmdbid, language, choice) random.shuffle(movies) - trailers = get_trailers(apikey, movies, language, trailertype, count) + trailers = TMDB.get_trailers(movies, language, 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 new file mode 100644 index 0000000..d23671f --- /dev/null +++ b/tmdb.py @@ -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