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