-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTrakt2Letterboxd.py
196 lines (153 loc) · 6.58 KB
/
Trakt2Letterboxd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
""" Trakt2Letterboxd """
from urllib.request import Request, urlopen
from urllib.error import HTTPError
import json
import time
import csv
import os.path
class TraktImporter(object):
""" Trakt Importer """
def __init__(self):
self.api_root = 'https://api.trakt.tv'
self.api_clid = 'b04da548cc9df60510eac7ec1845ab98cebd8008a9978804a981bff7e73ab270'
self.api_clsc = 'a880315fba01a5e5f0ad7de12b7872e36826a9359b2f419122a24dee1b2cb600'
self.api_token = None
self.api_headers = { 'Content-Type': 'application/json' }
def authenticate(self):
""" Authenticates the user and grabs an API access token if none is available. """
if self.__decache_token():
return True
dev_code_details = self.__generate_device_code()
self.__show_auth_instructions(dev_code_details)
got_token = self.__poll_for_auth(dev_code_details['device_code'],
dev_code_details['interval'],
dev_code_details['expires_in'] + time.time())
if got_token:
self.__encache_token()
return True
return False
def __decache_token(self):
if not os.path.isfile("t_token"):
return False
token_file = open("t_token", 'r')
self.api_token = token_file.read()
token_file.close()
return True
def __encache_token(self):
token_file = open("t_token", 'w')
token_file.write(self.api_token)
token_file.close()
@staticmethod
def __delete_token_cache():
os.remove("t_token")
def __generate_device_code(self):
""" Generates a device code for authentication within Trakt. """
url = self.api_root + '/oauth/device/code'
data = """{{"client_id": "{0}"}}""".format(self.api_clid).encode('utf8')
request = Request(url, data, self.api_headers)
response_body = urlopen(request).read()
return json.loads(response_body)
@staticmethod
def __show_auth_instructions(details):
message = ("\nGo to {0} on your web browser and enter the below user code there:\n\n"
"{1}\n\nAfter you have authenticated and given permission;"
"come back here to continue.\n"
).format(details['verification_url'], details['user_code'])
print(message)
def __poll_for_auth(self, device_code, interval, expiry):
""" Polls for authorization token """
url = self.api_root + '/oauth/device/token'
data = """{{ "code": "{0}",
"client_id": "{1}",
"client_secret": "{2}" }}
""".format(device_code, self.api_clid, self.api_clsc).encode('utf8')
request = Request(url, data, self.api_headers)
response_body = ""
should_stop = False
print("Waiting for authorization.", end=' ')
while not should_stop:
time.sleep(interval)
try:
response_body = urlopen(request).read()
should_stop = True
except HTTPError as err:
if err.code == 400:
print(".", end=' ')
else:
print("\n{0} : Authorization failed, please try again. Script will now quit.".format(err.code))
should_stop = True
should_stop = should_stop or (time.time() > expiry)
if response_body:
response_dict = json.loads(response_body)
if response_dict and 'access_token' in response_dict:
print("Authenticated!")
self.api_token = response_dict['access_token']
print("Token:" + self.api_token)
return True
# Errored.
return False
def get_movie_list(self, list_name):
""" Get movie list of the user. """
print("Getting " + list_name)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + self.api_token,
'trakt-api-version': '2',
'trakt-api-key': self.api_clid
}
extracted_movies = []
page_limit = 1
page = 1
while page <= page_limit:
request = Request(self.api_root + '/sync/' + list_name + '/movies?page={0}&limit=10'.format(page),
headers=headers)
try:
response = urlopen(request)
page_limit = int(response.info()['X-Pagination-Page-Count'])
print("Completed page {0} of {1}".format(page, page_limit))
page = page + 1
response_body = response.read()
if response_body:
extracted_movies.extend(self.__extract_fields(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() # This will regenerate token on next run.
print("{0} An error occured. Please re-run the script".format(err.code))
quit()
return extracted_movies
@staticmethod
def __extract_fields(movies):
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'],
} for x in movies]
def write_csv(history, filename):
""" Write Letterboxd format CSV """
if history:
with open(filename, 'w', encoding='utf8') as fil:
writer = csv.DictWriter(fil, list(history[0].keys()))
writer.writeheader()
writer.writerows(history)
return True
return False
def run():
"""Get set go!"""
print("Initializing...")
importer = TraktImporter()
if importer.authenticate():
history = importer.get_movie_list('history')
watchlist = importer.get_movie_list('watchlist')
if write_csv(history, "trakt-exported-history.csv"):
print("\nYour history has been exported and saved to the file 'trakt-exported-history.csv'.")
else:
print("\nEmpty results, nothing to generate.")
if write_csv(watchlist, "trakt-exported-watchlist.csv"):
print("\nYour watchlist has been exported and saved to the file 'trakt-exported-watchlist.csv'.")
else:
print("\nEmpty results, nothing to generate.")
if __name__ == '__main__':
run()