refactored tmdb stuff into a class

This commit is contained in:
Ronald Schaten 2021-03-19 01:21:09 +01:00
parent 1dc369bc3b
commit d9e185ba4f
3 changed files with 74 additions and 65 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.pyo

View File

@ -5,7 +5,8 @@ import xbmcvfs
import json import json
import random import random
import sys import sys
import urllib
import tmdb
ADDON = xbmcaddon.Addon() ADDON = xbmcaddon.Addon()
CWD = ADDON.getAddonInfo('path').decode('utf-8') CWD = ADDON.getAddonInfo('path').decode('utf-8')
@ -37,66 +38,6 @@ def files_from_dir(count, location):
files[i] = location + files[i] files[i] = location + files[i]
return files 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): def conduct_program(program_file, feature):
filehandle = xbmcvfs.File(program_file) filehandle = xbmcvfs.File(program_file)
program_json = filehandle.read() program_json = filehandle.read()
@ -113,7 +54,7 @@ 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':
apikey = settings['apikey'] TMDB = tmdb.Tmdb(apikey = settings['apikey'])
tmdbid = int(feature['tmdbid']) tmdbid = int(feature['tmdbid'])
imdbid = feature['imdbid'] imdbid = feature['imdbid']
language = settings['language'] language = settings['language']
@ -121,11 +62,11 @@ def conduct_program(program_file, feature):
trailertype = settings['type'] trailertype = settings['type']
count = settings['count'] count = settings['count']
if not tmdbid: if not tmdbid:
tmdbid = get_tmdbid(apikey, imdbid) tmdbid = TMDB.get_tmdbid(imdbid)
if tmdbid: if tmdbid:
movies = get_recommendations(apikey, tmdbid, language, choice) movies = TMDB.get_recommendations(tmdbid, language, choice)
random.shuffle(movies) random.shuffle(movies)
trailers = get_trailers(apikey, movies, language, trailertype, count) trailers = TMDB.get_trailers(movies, language, trailertype, count)
else: else:
print("TODO: this feature has no tmdb id, find someting else to play") print("TODO: this feature has no tmdb id, find someting else to play")
trailers = [] trailers = []

67
tmdb.py Normal file
View File

@ -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