-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbetaseries-to-trakt.py
261 lines (198 loc) · 7.29 KB
/
betaseries-to-trakt.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import csv
import re
import json
import requests
bs_api = 'https://api.betaseries.com'
bs_api_key = os.environ.get('BS_API_KEY')
bs_movie_route = '%s/movies/movie' % (bs_api)
bs_show_route = '%s/shows/display' % (bs_api)
trakt_api = 'https://api.trakt.tv'
auth_get_token_route = '%s/oauth/token' % (trakt_api)
sync_history_route = '%s/sync/history' % (trakt_api)
sync_watchlist_route = '%s/sync/watchlist' % (trakt_api)
bs_session = requests.Session()
session = requests.Session()
bs_session.headers.update({
'Accept': 'application/json',
'User-Agent': 'Betaseries to Trakt',
'Connection': 'Keep-Alive',
'X-BetaSeries-Key' : bs_api_key
})
def login_to_trakt():
print('')
print('Open the link in a browser and paste the pin')
print('https://trakt.tv/oauth/authorize?response_type=code&client_id=%s&redirect_uri=urn:ietf:wg:oauth:2.0:oob' % (os.environ.get('CLIENT_ID')))
print('')
pin = str(input('Pin: '))
session.headers.update({
'Accept': 'application/json',
'User-Agent': 'Betaseries to Trakt',
'Connection': 'Keep-Alive'
})
post_data = {
'code': pin,
'client_id': os.environ.get('CLIENT_ID'),
'client_secret': os.environ.get('CLIENT_SECRET'),
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
'grant_type': 'authorization_code'
}
request = session.post(auth_get_token_route, data=post_data)
response = request.json()
session.headers.update({
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': os.environ.get('CLIENT_ID'),
'Authorization': 'Bearer ' + response["access_token"]
})
def create_show_history(obj):
id = obj[0]
name = obj[1]
status_pct = obj[4]
output = re.search('S([0-9]+)E([0-9]+)', obj[3])
last_seen_season = output.group(1)
last_seen_episode = output.group(2)
request = bs_session.get(bs_show_route + '?id=' + id)
response = request.json()
show = {
'name': name,
'ids': {
'tvdb': response['show']['thetvdb_id']
},
'watched_at': 'released'
}
if status_pct != '100':
show['seasons'] = []
for s in range(0, int(last_seen_season)):
show['seasons'].append({
'number': s + 1,
'watched_at': 'released'
})
if (s + 1) == int(last_seen_season):
show['seasons'][s]['episodes'] = []
for e in range(0, int(last_seen_episode)):
show['seasons'][s]['episodes'].append({
'number': e + 1,
'watched_at': 'released'
})
return show
def create_show_watchlist(obj):
id = obj[0]
name = obj[1]
request = bs_session.get(bs_show_route + '?id=' + id)
response = request.json()
show = {
'name': name,
'ids': {
'tvdb': response['show']['thetvdb_id']
}
}
return show
def create_movie_history(obj):
id = obj[0]
name = obj[1]
request = bs_session.get(bs_movie_route + '?id=' + id)
response = request.json()
movie = {
'title': name,
'ids': {
'tmdb': response['movie']['imdb_id']
},
'watched_at': 'released'
}
return movie
def create_movie_watchlist(obj):
id = obj[0]
name = obj[1]
request = bs_session.get(bs_movie_route + '?id=' + id)
response = request.json()
movie = {
'title': name,
'ids': {
'tmdb': response['movie']['imdb_id']
}
}
return movie
def main():
login_to_trakt()
post_history_data = {
'shows': [],
'movies': []
}
post_watchlist_data = {
'shows': [],
'movies': []
}
for file_path in sys.argv[1::]:
file = open(file_path, encoding="utf8")
file_name = os.path.basename(file_path)
if "series-" in file_name:
type = 'show'
else:
type = 'movie'
for row in csv.reader(file):
# we move to next line (first is header)
if row[0] == 'id':
continue
if type == 'show':
status_pct = row[5]
if status_pct == '0':
post_watchlist_data['shows'].append(create_show_watchlist(row))
else:
post_history_data['shows'].append(create_show_history(row))
if type == 'movie':
status = row[2] # '2' = je ne veux pas voir, '1' = j'ai vu, '0' = je veux voir
if status == '0':
post_watchlist_data['movies'].append(create_movie_watchlist(row))
elif status == '1':
post_history_data['movies'].append(create_movie_history(row))
file.close()
# Post 'history' data
request_history = session.post(sync_history_route, data=json.dumps(post_history_data))
response_history = request_history.json()
# Post 'watchlist' data
request_watchlist = session.post(sync_watchlist_route, data=json.dumps(post_watchlist_data))
response_watchlist = request_watchlist.json()
# Print summary
print('\r\nMigration from Betaseries CSV files Done!')
print('\r\n-----------------------------------------')
print('HISTORY')
print('\r\nAdded:')
# print('\t %s shows' % response_history['added']['shows'])
print('\t %s episodes' % response_history['added']['episodes'])
print('\t %s movies' % response_history['added']['movies'])
if response_history['not_found']['shows'] or response_history['not_found']['episodes'] or response_history['not_found']['movies']:
print('Not found:')
if len(response_history['not_found']['shows']):
print('\t shows:')
print(response_history['not_found']['shows'])
if len(response_history['not_found']['episodes']):
print('\t episodes:')
print(response_history['not_found']['episodes'])
if len(response_history['not_found']['movies']):
print('\t movies:')
print(response_history['not_found']['movies'])
print('\r\n-----------------------------------------')
print('WATCHLIST')
print('\r\nAdded:')
print('\t %s shows' % response_watchlist['added']['shows'])
print('\t %s movies' % response_watchlist['added']['movies'])
if response_watchlist['existing']['shows'] or response_watchlist['existing']['movies']:
print('Existing:')
if response_watchlist['existing']['shows']:
print('\t %s shows' % response_watchlist['existing']['shows'])
if response_watchlist['existing']['movies']:
print('\t %s movies (the play count was incremented)' % response_watchlist['existing']['movies'])
if response_watchlist['not_found']['shows'] or response_watchlist['not_found']['movies']:
print('Not found:')
if len(response_watchlist['not_found']['shows']):
print('\t shows:')
print(response_watchlist['not_found']['shows'])
if len(response_watchlist['not_found']['movies']):
print('\t movies:')
print(response_watchlist['not_found']['movies'])
if __name__ == "__main__":
main()