reformat and comment

This commit is contained in:
Ronald Schaten 2021-03-19 15:07:55 +01:00
parent c3bf8117a6
commit 646b62a149

22
tmdb.py
View File

@ -10,6 +10,7 @@ class Tmdb:
self.apikey = apikey self.apikey = apikey
self.language = language self.language = language
# fetches data from TMDB API
def fetch_from_tmdb(self, path, parameters): def fetch_from_tmdb(self, path, parameters):
apikey = "?api_key=%s" % self.apikey apikey = "?api_key=%s" % self.apikey
url = self.baseurl + path + apikey + parameters url = self.baseurl + path + apikey + parameters
@ -17,6 +18,7 @@ class Tmdb:
data = json.loads(json_url.read()) data = json.loads(json_url.read())
return data return data
# finds TMDB-id for a known IMDB-id, return 0 if nothing is found
def get_tmdbid(self, imdbid): def get_tmdbid(self, imdbid):
print("getting tmdbid for imdbid %s" % imdbid) print("getting tmdbid for imdbid %s" % imdbid)
data = self.fetch_from_tmdb( data = self.fetch_from_tmdb(
@ -31,6 +33,8 @@ class Tmdb:
print("tmdbid could not be found") print("tmdbid could not be found")
return tmdbid return tmdbid
# get recommendations for a movie id
# choice is either "recommendations" or "similar"
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))
data = self.fetch_from_tmdb( data = self.fetch_from_tmdb(
@ -39,9 +43,13 @@ class Tmdb:
) )
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']
})
return results return results
# get trailers, teasers, clips... for a movie id
def get_movie_trailers(self, movieid): def get_movie_trailers(self, movieid):
print("getting trailers for %d" % movieid) print("getting trailers for %d" % movieid)
data = self.fetch_from_tmdb( data = self.fetch_from_tmdb(
@ -53,22 +61,32 @@ class Tmdb:
if result['site'] == 'YouTube': if result['site'] == 'YouTube':
location = 'plugin://plugin.video.youtube/play/?video_id=%s' % result['key'] location = 'plugin://plugin.video.youtube/play/?video_id=%s' % result['key']
else: else:
# skip other sites
next next
results.append({'title': result['name'], 'type': result['type'], 'location': location}) results.append({
'title': result['name'],
'type': result['type'],
'location': location
})
return results return results
# get a number of trailers of a certain kind for a list of recommended movies
def get_trailers(self, recommendations, cliptype, count): def get_trailers(self, recommendations, cliptype, count):
results = [] results = []
# loop over list of recommendations
for recommendation in recommendations[:10]: for recommendation in recommendations[:10]:
all_trailers = self.get_movie_trailers(recommendation['movieid']) all_trailers = self.get_movie_trailers(recommendation['movieid'])
# filter to get only a certain kind of trailer
trailers = [] trailers = []
for trailer in all_trailers: for trailer in all_trailers:
if trailer['type'] == cliptype: if trailer['type'] == cliptype:
trailers.append(trailer) trailers.append(trailer)
# get a random clip for this recommendation
if len(trailers) > 0: if len(trailers) > 0:
random.shuffle(trailers) random.shuffle(trailers)
trailer = trailers[0] trailer = trailers[0]
results.append(trailer) results.append(trailer)
# quit if enough clips have been collected
if len(results) == count: if len(results) == count:
break break
return results return results