From aa6f35b4de71827f0983b4aa83d1badca7e62981 Mon Sep 17 00:00:00 2001 From: Ronald Schaten Date: Tue, 16 Mar 2021 18:13:55 +0100 Subject: [PATCH] implement getting trailers from themoviedb.org --- addon.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/addon.py b/addon.py index 95fc44e..6911eea 100644 --- a/addon.py +++ b/addon.py @@ -5,6 +5,7 @@ import xbmcvfs import json import random import sys +import urllib ADDON = xbmcaddon.Addon() CWD = ADDON.getAddonInfo('path').decode('utf-8') @@ -36,11 +37,49 @@ def files_from_dir(count, location): files[i] = location + files[i] return files -def trailers_from_net(count, genre, rating): - files = [] - for i in range(count): - files.append("TODO: find trailer %d" % i) - return files +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) @@ -58,10 +97,17 @@ def conduct_program(program_file, feature): entry = {'type': 'video', 'data': location} program.append(entry) elif settings['source'] == 'tmdbtrailer': - genre = 'TODO: find feature genre' - rating = 'TODO: find feature rating' - for location in trailers_from_net(settings['count'], genre, rating): - entry = {'type': 'video', 'data': location} + apikey = settings['apikey'] + tmdbid = int(feature['tmdbid']) + language = settings['language'] + choice = settings['choice'] + trailertype = settings['type'] + count = settings['count'] + movies = get_recommendations(apikey, tmdbid, language, choice) + random.shuffle(movies) + trailers = get_trailers(apikey, movies, language, trailertype, count) + for trailer in trailers: + entry = {'type': 'video', 'data': trailer['location']} program.append(entry) elif settings['source'] == 'feature': entry = {'type': 'video', 'data': feature['file']} @@ -69,14 +115,14 @@ def conduct_program(program_file, feature): return program def get_feature(movieid): - query = '{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": {"movieid": %s, "properties": ["file", "mpaa", "genre"]}, "id": "1"}' % movieid + query = '{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovieDetails", "params": {"movieid": %s, "properties": ["file", "mpaa", "uniqueid"]}, "id": "1"}' % movieid json_response = xbmc.executeJSONRPC(query) response = json.loads(json_response) feature = { 'label': response['result']['moviedetails']['label'], 'file': response['result']['moviedetails']['file'], 'mpaa': response['result']['moviedetails']['mpaa'], - 'genre': response['result']['moviedetails']['genre'] + 'tmdbid': response['result']['moviedetails']['uniqueid']['tmdb'] } return feature