refactored

This commit is contained in:
Ronald Schaten 2021-03-19 14:34:14 +01:00
parent 5e238ba9b0
commit 89c1c1d7c5

23
tmdb.py
View File

@ -10,13 +10,16 @@ class Tmdb:
self.apikey = apikey self.apikey = apikey
self.language = language self.language = language
def get_tmdbid(self, imdbid): def fetch_from_tmdb(self, path, parameters, data):
print("getting tmdbid for imdbid %s" % imdbid) apikey = "?api_key=%s" % self.apikey
url = self.baseurl + 'find/%s?api_key=%s&external_source=imdb_id' url = (self.baseurl + path + apikey + parameters) % data
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())
print(data) return data
def get_tmdbid(self, imdbid):
print("getting tmdbid for imdbid %s" % imdbid)
data = self.fetch_from_tmdb('find/%s', '&external_source=imdb_id', imdbid)
try: try:
tmdbid = data['movie_results'][0]['id'] tmdbid = data['movie_results'][0]['id']
print("tmdbid is %d" % tmdbid) print("tmdbid is %d" % tmdbid)
@ -27,10 +30,7 @@ class Tmdb:
def get_recommendations(self, movieid, choice): def get_recommendations(self, movieid, choice):
print("getting %s for %d" % (choice, movieid)) print("getting %s for %d" % (choice, movieid))
url = self.baseurl + 'movie/%d/%s?api_key=%s&language=%s' data = self.fetch_from_tmdb('movie/%d/%s', '&language=%s', (movieid, choice, self.language))
url = url % (movieid, choice, self.apikey, self.language)
json_url = urllib.urlopen(url)
data = json.loads(json_url.read())
results = [] results = []
for result in data['results']: for result in data['results']:
results.append({'title': result['title'], 'movieid': result['id']}) results.append({'title': result['title'], 'movieid': result['id']})
@ -38,10 +38,7 @@ class Tmdb:
def get_movie_trailers(self, movieid): def get_movie_trailers(self, movieid):
print("getting trailers for %d" % movieid) print("getting trailers for %d" % movieid)
url = self.baseurl + 'movie/%d?api_key=%s&language=%s&append_to_response=videos' data = self.fetch_from_tmdb('movie/%d', '&language=%s&append_to_response=videos', (movieid, self.language))
url = url % (movieid, self.apikey, self.language)
json_url = urllib.urlopen(url)
data = json.loads(json_url.read())
results = [] results = []
for result in data['videos']['results']: for result in data['videos']['results']:
if result['site'] == 'YouTube': if result['site'] == 'YouTube':