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}
program.append(entry)
elif settings['source'] == 'tmdbtrailer':
TMDB = tmdb.Tmdb(apikey = settings['apikey'])
TMDB = tmdb.Tmdb(apikey = settings['apikey'], language = settings['language'])
tmdbid = int(feature['tmdbid'])
imdbid = feature['imdbid']
language = settings['language']
choice = settings['choice']
trailertype = settings['type']
count = settings['count']
if not tmdbid:
tmdbid = TMDB.get_tmdbid(imdbid)
if tmdbid:
movies = TMDB.get_recommendations(tmdbid, language, choice)
movies = TMDB.get_recommendations(tmdbid, choice)
random.shuffle(movies)
trailers = TMDB.get_trailers(movies, language, trailertype, count)
trailers = TMDB.get_trailers(movies, trailertype, count)
else:
print("TODO: this feature has no tmdb id, find someting else to play")
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 random
import urllib
class Tmdb:
def __init__(self, apikey):
def __init__(self, apikey, language):
self.baseurl = 'https://api.themoviedb.org/3/'
self.apikey = apikey
self.language = language
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 = self.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())
@ -22,11 +25,10 @@ class Tmdb:
print("tmdbid could not be found")
return tmdbid
def get_recommendations(self, movieid, language, choice):
def get_recommendations(self, movieid, 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)
url = self.baseurl + 'movie/%d/%s?api_key=%s&language=%s'
url = url % (movieid, choice, self.apikey, self.language)
json_url = urllib.urlopen(url)
data = json.loads(json_url.read())
results = []
@ -34,11 +36,10 @@ class Tmdb:
results.append({'title': result['title'], 'movieid': result['id']})
return results
def get_movie_trailers(self, movieid, language):
def get_movie_trailers(self, movieid):
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)
url = self.baseurl + 'movie/%d?api_key=%s&language=%s&append_to_response=videos'
url = url % (movieid, self.apikey, self.language)
json_url = urllib.urlopen(url)
data = json.loads(json_url.read())
results = []
@ -50,10 +51,10 @@ class Tmdb:
results.append({'title': result['name'], 'type': result['type'], 'location': location})
return results
def get_trailers(self, recommendations, language, cliptype, count):
def get_trailers(self, recommendations, cliptype, count):
results = []
for recommendation in recommendations[:10]:
all_trailers = self.get_movie_trailers(recommendation['movieid'], language)
all_trailers = self.get_movie_trailers(recommendation['movieid'])
trailers = []
for trailer in all_trailers:
if trailer['type'] == cliptype: