implement getting trailers from themoviedb.org

This commit is contained in:
Ronald Schaten 2021-03-16 18:13:55 +01:00
parent cdc6fa4e2b
commit aa6f35b4de

View File

@ -5,6 +5,7 @@ import xbmcvfs
import json import json
import random import random
import sys import sys
import urllib
ADDON = xbmcaddon.Addon() ADDON = xbmcaddon.Addon()
CWD = ADDON.getAddonInfo('path').decode('utf-8') CWD = ADDON.getAddonInfo('path').decode('utf-8')
@ -36,11 +37,49 @@ def files_from_dir(count, location):
files[i] = location + files[i] files[i] = location + files[i]
return files return files
def trailers_from_net(count, genre, rating): def get_recommendations(apikey, movieid, language, choice):
files = [] print("getting %s for %d" % (choice, movieid))
for i in range(count): baseurl = 'https://api.themoviedb.org/3/'
files.append("TODO: find trailer %d" % i) url = baseurl + 'movie/%d/%s?api_key=%s&language=%s'
return files 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): def conduct_program(program_file, feature):
filehandle = xbmcvfs.File(program_file) filehandle = xbmcvfs.File(program_file)
@ -58,10 +97,17 @@ def conduct_program(program_file, feature):
entry = {'type': 'video', 'data': location} entry = {'type': 'video', 'data': location}
program.append(entry) program.append(entry)
elif settings['source'] == 'tmdbtrailer': elif settings['source'] == 'tmdbtrailer':
genre = 'TODO: find feature genre' apikey = settings['apikey']
rating = 'TODO: find feature rating' tmdbid = int(feature['tmdbid'])
for location in trailers_from_net(settings['count'], genre, rating): language = settings['language']
entry = {'type': 'video', 'data': location} 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) program.append(entry)
elif settings['source'] == 'feature': elif settings['source'] == 'feature':
entry = {'type': 'video', 'data': feature['file']} entry = {'type': 'video', 'data': feature['file']}
@ -69,14 +115,14 @@ def conduct_program(program_file, feature):
return program return program
def get_feature(movieid): 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) json_response = xbmc.executeJSONRPC(query)
response = json.loads(json_response) response = json.loads(json_response)
feature = { feature = {
'label': response['result']['moviedetails']['label'], 'label': response['result']['moviedetails']['label'],
'file': response['result']['moviedetails']['file'], 'file': response['result']['moviedetails']['file'],
'mpaa': response['result']['moviedetails']['mpaa'], 'mpaa': response['result']['moviedetails']['mpaa'],
'genre': response['result']['moviedetails']['genre'] 'tmdbid': response['result']['moviedetails']['uniqueid']['tmdb']
} }
return feature return feature