refactored

This commit is contained in:
Ronald Schaten 2021-03-19 13:54:04 +01:00
parent d9e185ba4f
commit 5e238ba9b0
2 changed files with 17 additions and 17 deletions

View File

@ -54,19 +54,18 @@ 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':
TMDB = tmdb.Tmdb(apikey = settings['apikey']) TMDB = tmdb.Tmdb(apikey = settings['apikey'], language = settings['language'])
tmdbid = int(feature['tmdbid']) tmdbid = int(feature['tmdbid'])
imdbid = feature['imdbid'] imdbid = feature['imdbid']
language = settings['language']
choice = settings['choice'] choice = settings['choice']
trailertype = settings['type'] trailertype = settings['type']
count = settings['count'] count = settings['count']
if not tmdbid: if not tmdbid:
tmdbid = TMDB.get_tmdbid(imdbid) tmdbid = TMDB.get_tmdbid(imdbid)
if tmdbid: if tmdbid:
movies = TMDB.get_recommendations(tmdbid, language, choice) movies = TMDB.get_recommendations(tmdbid, choice)
random.shuffle(movies) random.shuffle(movies)
trailers = TMDB.get_trailers(movies, language, trailertype, count) trailers = TMDB.get_trailers(movies, 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 = []

27
tmdb.py
View File

@ -1,15 +1,18 @@
# this is a small and single-purpose wrapper around the TMDB API
import json import json
import random import random
import urllib import urllib
class Tmdb: class Tmdb:
def __init__(self, apikey): def __init__(self, apikey, language):
self.baseurl = 'https://api.themoviedb.org/3/'
self.apikey = apikey self.apikey = apikey
self.language = language
def get_tmdbid(self, imdbid): def get_tmdbid(self, imdbid):
print("getting tmdbid for imdbid %s" % imdbid) print("getting tmdbid for imdbid %s" % imdbid)
baseurl = 'https://api.themoviedb.org/3/' url = self.baseurl + 'find/%s?api_key=%s&external_source=imdb_id'
url = baseurl + 'find/%s?api_key=%s&external_source=imdb_id'
url = url % (imdbid, self.apikey) url = url % (imdbid, self.apikey)
json_url = urllib.urlopen(url) json_url = urllib.urlopen(url)
data = json.loads(json_url.read()) data = json.loads(json_url.read())
@ -22,11 +25,10 @@ class Tmdb:
print("tmdbid could not be found") print("tmdbid could not be found")
return tmdbid return tmdbid
def get_recommendations(self, movieid, language, choice): def get_recommendations(self, movieid, choice):
print("getting %s for %d" % (choice, movieid)) print("getting %s for %d" % (choice, movieid))
baseurl = 'https://api.themoviedb.org/3/' url = self.baseurl + 'movie/%d/%s?api_key=%s&language=%s'
url = baseurl + 'movie/%d/%s?api_key=%s&language=%s' url = url % (movieid, choice, self.apikey, self.language)
url = url % (movieid, choice, self.apikey, language)
json_url = urllib.urlopen(url) json_url = urllib.urlopen(url)
data = json.loads(json_url.read()) data = json.loads(json_url.read())
results = [] results = []
@ -34,11 +36,10 @@ class Tmdb:
results.append({'title': result['title'], 'movieid': result['id']}) results.append({'title': result['title'], 'movieid': result['id']})
return results return results
def get_movie_trailers(self, movieid, language): def get_movie_trailers(self, movieid):
print("getting trailers for %d" % movieid) print("getting trailers for %d" % movieid)
baseurl = 'https://api.themoviedb.org/3/' url = self.baseurl + 'movie/%d?api_key=%s&language=%s&append_to_response=videos'
url = baseurl + 'movie/%d?api_key=%s&language=%s&append_to_response=videos' url = url % (movieid, self.apikey, self.language)
url = url % (movieid, self.apikey, language)
json_url = urllib.urlopen(url) json_url = urllib.urlopen(url)
data = json.loads(json_url.read()) data = json.loads(json_url.read())
results = [] results = []
@ -50,10 +51,10 @@ class Tmdb:
results.append({'title': result['name'], 'type': result['type'], 'location': location}) results.append({'title': result['name'], 'type': result['type'], 'location': location})
return results return results
def get_trailers(self, recommendations, language, cliptype, count): def get_trailers(self, recommendations, cliptype, count):
results = [] results = []
for recommendation in recommendations[:10]: for recommendation in recommendations[:10]:
all_trailers = self.get_movie_trailers(recommendation['movieid'], language) all_trailers = self.get_movie_trailers(recommendation['movieid'])
trailers = [] trailers = []
for trailer in all_trailers: for trailer in all_trailers:
if trailer['type'] == cliptype: if trailer['type'] == cliptype: