From db636cd1ada51a58355ffce7cc32d88ccb8d586f Mon Sep 17 00:00:00 2001 From: Benny Salewski Date: Fri, 6 Aug 2021 22:52:22 +0200 Subject: [PATCH] Update Trakt2Letterboxd.py Added export for the ratings. --- Trakt2Letterboxd.py | 46 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/Trakt2Letterboxd.py b/Trakt2Letterboxd.py index cc011ca..6212e08 100644 --- a/Trakt2Letterboxd.py +++ b/Trakt2Letterboxd.py @@ -130,6 +130,8 @@ def get_movie_list(self, list_name): page_limit = 1 page = 1 + ratings = self.get_ratings() + while page <= page_limit: request = Request(self.api_root + '/sync/' + list_name + '/movies?page={0}&limit=10'.format(page), headers=headers) @@ -142,7 +144,7 @@ def get_movie_list(self, list_name): response_body = response.read() if response_body: - extracted_movies.extend(self.__extract_fields(json.loads(response_body))) + extracted_movies.extend(self.__extract_fields(json.loads(response_body), ratings)) except HTTPError as err: if err.code == 401 or err.code == 403: print("Auth Token has expired.") @@ -152,14 +154,54 @@ def get_movie_list(self, list_name): return extracted_movies + def get_ratings(self): + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + self.api_token, + 'trakt-api-version': '2', + 'trakt-api-key': self.api_clid + } + + request = Request(self.api_root + '/sync/ratings/movies', headers=headers) + + try: + response = urlopen(request) + response_body = response.read() + + if response_body: + return [ + { + 'rating': rating['rating'], + 'imdb': rating['movie']['ids']['imdb'], + 'trakt': rating['movie']['ids']['trakt'], + 'tmdb': rating['movie']['ids']['tmdb'], + 'slug': rating['movie']['ids']['slug'], + } for rating in json.loads(response_body)] + except HTTPError as err: + if err.code == 401 or err.code == 403: + print("Auth Token has expired.") + self.__delete_token_cache() + + print("{0} An error occured. Please re-run the script".format(err.code)) + quit() + + @staticmethod + def __get_rating(ratings, ids): + for rating in ratings: + if ids['imdb'] == rating['imdb'] or ids['trakt'] == rating['trakt'] or ids['tmdb'] == rating['tmdb'] or ids['slug'] == rating['slug']: + return rating['rating'] + + return '' + @staticmethod - def __extract_fields(movies): + def __extract_fields(movies, ratings): return [{ 'WatchedDate': x['watched_at'] if ('watched_at' in x) else '', 'tmdbID': x['movie']['ids']['tmdb'], 'imdbID': x['movie']['ids']['imdb'], 'Title': x['movie']['title'], 'Year': x['movie']['year'], + 'Rating10': TraktImporter.__get_rating(ratings, x['movie']['ids']) } for x in movies] def write_csv(history, filename):