implement getting trailers from themoviedb.org
This commit is contained in:
parent
cdc6fa4e2b
commit
aa6f35b4de
68
addon.py
68
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user