Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Trakt2Letterboxd.py #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Trakt2Letterboxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.")
Expand All @@ -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):
Expand Down