forked from JvSomeren/tautulli-watched-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrakt_sync.py
executable file
·340 lines (269 loc) · 10.9 KB
/
trakt_sync.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Description: Sync viewing history with Trakt.tv
Author: Joost van Someren
Important!
Make sure `./sync-settings.ini` is writable
Settings:
./sync-settings.ini
[Plex]
user_ids: a comma separated list of user ids, only entries for these users will be synced
The user id for a user can be found in your url in Tautulli when you click on a user.
[Trakt]:
Update `client_id` with the `client_id` of your registered application, see here:
https://trakt.tv/oauth/applications > Choose your application
To set the access code use `urn:ietf:wg:oauth:2.0:oob` as a redirect URI on your application.
Then execute the script:
python ./trakt_sync.py --contentType trakt_authenticate --userId -1
And follow the instructions shown.
Adding the script to Tautulli:
Tautulli > Settings > Notification Agents > Add a new notification agent > Script
Configuration:
Tautulli > Settings > Notification Agents > New Script > Configuration:
Script Folder: /path/to/your/scripts
Script File: ./trakt_sync.py (Should be selectable in a dropdown list)
Script Timeout: {timeout}
Description: Trakt.tv sync
Save
Triggers:
Tautulli > Settings > Notification Agents > New Script > Triggers:
Check: Watched
Save
Conditions:
Tautulli > Settings > Notification Agents > New Script > Conditions:
Set Conditions: [{condition} | {operator} | {value} ]
Save
Script Arguments:
Tautulli > Settings > Notification Agents > New Script > Script Arguments:
Select: Watched
Arguments: --userId {user_id} --contentType {media_type}
<movie>--imdbId {imdb_id}</movie>
<episode>--tvdbId {thetvdb_id} --season {season_num} --episode {episode_num}</episode>
Save
Close
"""
import os
import sys
import requests
import argparse
import datetime
from configparser import ConfigParser, NoOptionError, NoSectionError
TAUTULLI_ENCODING = os.getenv('TAUTULLI_ENCODING', 'UTF-8')
credential_path = os.path.dirname(os.path.realpath(__file__))
credential_file = 'sync_settings.ini'
config = ConfigParser()
try:
with open('%s/%s' % (credential_path, credential_file)) as f:
config.read_file(f)
except IOError:
print('ERROR: %s/%s not found' % (credential_path, credential_file))
sys.exit(1)
def arg_decoding(arg):
"""Decode args, encode UTF-8"""
return arg.decode(TAUTULLI_ENCODING).encode('UTF-8')
def write_settings():
"""Write config back to settings file"""
try:
with open('%s/%s' % (credential_path, credential_file), 'w') as f:
config.write(f)
except IOError:
print('ERROR: unable to write to %s/%s' % (credential_path, credential_file))
sys.exit(1)
def sync_for_user(user_id):
"""Returns wheter or not to sync for the passed user_id"""
try:
user_ids = config.get('Plex', 'user_ids')
except (NoSectionError, NoOptionError):
print('ERROR: %s not setup - missing user_ids' % credential_file)
sys.exit(1)
return str(user_id) in user_ids.split(',')
class Trakt:
def __init__(self, type, id, season_num='', episode_num=''):
if type == 'movie':
self.type = 'movie'
self.imdb_id = id
elif type == 'episode':
self.type = 'episode'
self.tvdb_id = id
self.season_num = season_num
self.episode_num = episode_num
try:
self.client_id = config.get('Trakt', 'client_id')
except (NoSectionError, NoOptionError):
print('ERROR: %s not setup - missing client_id' % credential_file)
sys.exit(1)
try:
self.client_secret = config.get('Trakt', 'client_secret')
except (NoSectionError, NoOptionError):
print('ERROR: %s not setup - missing client_secret' % credential_file)
sys.exit(1)
def get_access_token(self):
try:
return config.get('Trakt', 'access_token')
except (NoSectionError, NoOptionError):
print('ERROR: %s not setup - missing access_token' % credential_file)
sys.exit(1)
def get_refresh_token(self):
try:
return config.get('Trakt', 'refresh_token')
except (NoSectionError, NoOptionError):
print('ERROR: %s not setup - missing refresh_token' % credential_file)
sys.exit(1)
def authenticate(self):
headers = {
'Content-Type': 'application/json'
}
device_code = self.generate_device_code(headers)
self.poll_access_token(headers, device_code)
def generate_device_code(self, headers):
payload = {
'client_id': self.client_id
}
r = requests.post('https://api.trakt.tv/oauth/device/code', json=payload, headers=headers)
response = r.json()
print('Please go to %s and insert the following code: "%s"' % (
response['verification_url'], response['user_code']))
i = input('I have authorized the application! Press ENTER to continue:')
return response['device_code']
def poll_access_token(self, headers, device_code):
payload = {
'code': device_code,
'client_id': self.client_id,
'client_secret': self.client_secret
}
r = requests.post('https://api.trakt.tv/oauth/device/token', json=payload, headers=headers)
if r.status_code == 400:
i = input('The device hasn\'t been authorized yet, please do so. Press ENTER to continue:')
return self.poll_access_token(self, headers, device_code)
elif r.status_code != 200:
print('Something went wrong, please try again.')
sys.exit(1)
response = r.json()
config.set('Trakt', 'access_token', response['access_token'])
config.set('Trakt', 'refresh_token', response['refresh_token'])
write_settings()
print('Succesfully configured your Trakt.tv sync!')
def refresh_access_token(self):
headers = {
'Content-Type': 'application/json'
}
payload = {
'refresh_token': self.get_refresh_token(),
'client_id': self.client_id,
'client_secret': self.client_secret,
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob',
'grant_type': 'refresh_token'
}
r = requests.post('https://api.trakt.tv/oauth/token', json=payload, headers=headers)
response = r.json()
config.set('Trakt', 'access_token', response['access_token'])
config.set('Trakt', 'refresh_token', response['refresh_token'])
write_settings()
print('Refreshed access token succesfully!')
def get_movie(self):
headers = {
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': self.client_id
}
r = requests.get('https://api.trakt.tv/search/imdb/' + str(self.imdb_id) + '?type=movie', headers=headers)
response = r.json()
return response[0]['movie']
def get_show(self):
headers = {
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': self.client_id
}
r = requests.get('https://api.trakt.tv/search/tvdb/' + str(self.tvdb_id) + '?type=show', headers=headers)
response = r.json()
return response[0]['show']
def get_episode(self, show):
headers = {
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': self.client_id
}
r = requests.get('https://api.trakt.tv/shows/' + str(show['ids']['slug']) + '/seasons/' + str(
self.season_num) + '/episodes/' + str(self.episode_num), headers=headers)
response = r.json()
return response
def sync_history(self):
access_token = self.get_access_token()
watched_at = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.000Z')
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token,
'trakt-api-version': '2',
'trakt-api-key': self.client_id
}
if self.type == 'movie':
movie = self.get_movie()
payload = {
'movies': [
{
'watched_at': watched_at,
'title': movie['title'],
'year': movie['year'],
'ids': {
'trakt': movie['ids']['trakt'],
'slug': movie['ids']['slug'],
'imdb': movie['ids']['imdb'],
'tmdb': movie['ids']['tmdb']
}
}
]
}
elif self.type == 'episode':
show = self.get_show()
episode = self.get_episode(show)
payload = {
'episodes': [
{
'watched_at': watched_at,
'ids': {
'trakt': episode['ids']['trakt'],
'tvdb': episode['ids']['tvdb'],
'imdb': episode['ids']['imdb'],
'tmdb': episode['ids']['tmdb']
}
}
]
}
r = requests.post('https://api.trakt.tv/sync/history', json=payload, headers=headers)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Syncing viewing activity to Trakt.tv.")
parser.add_argument('--userId', required=True, type=int,
help='The user_id of the current user.')
parser.add_argument('--contentType', required=True, type=str,
help='The type of content, movie or episode.')
parser.add_argument('--tvdbId', type=int,
help='TVDB ID.')
parser.add_argument('--season', type=int,
help='Season number.')
parser.add_argument('--episode', type=int,
help='Episode number.')
parser.add_argument('--imdbId', type=str,
help='IMDB ID.')
opts = parser.parse_args()
if not sync_for_user(opts.userId) and not opts.userId == -1:
print('We will not sync for this user')
sys.exit(0)
if opts.contentType == 'trakt_authenticate':
trakt = Trakt(None, None, None)
trakt.authenticate()
elif opts.contentType == 'trakt_refresh':
trakt = Trakt(None, None, None)
trakt.refresh_access_token()
elif opts.contentType == 'movie':
trakt = Trakt('movie', opts.imdbId)
trakt.refresh_access_token()
trakt.sync_history()
elif opts.contentType == 'episode':
trakt = Trakt('episode', opts.tvdbId, opts.season, opts.episode)
trakt.refresh_access_token()
trakt.sync_history()
else:
print('ERROR: %s not found - invalid contentType' % opts.contentType)