This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plex-trakt-sync.py
executable file
·390 lines (296 loc) · 12.6 KB
/
plex-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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python
from optparse import OptionParser
from pprint import pformat
from xml.dom.minidom import parseString
import hashlib
import json
import logging
import os
import sys
import urllib
import urllib2
VERSION = '1.0'
DESCRIPTION = '''This script connects to a Plex media center server and
reports the watched movies to a trakt.tv user profile. Optionally it also
flags the movies at the trakt profile with "love" or "hate" according to
ratings in Plex.'''
EPILOG = '''
** Rating ** The plex rating allows to give up to 5 stars for
a movie, but you can also give half stars, so there are 10 steps for the
rating. The configurable --min-hate and --max-love options take a value
between 1 and 10. Movies which are not yet rated in plex are not flagged
at all.
'''
LOG_FILE = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'syncer.log')
logging.basicConfig(
filename=LOG_FILE,
datefmt='%Y-%m-%dT%H:%M:%S',
format='%(asctime)s %(levelname)s [%(name)s] %(message)s')
LOG = logging.getLogger('plex-trakt-syncer')
LOG.addHandler(logging.StreamHandler())
LOG.setLevel(logging.INFO)
RATE_LOVE = 'love'
RATE_HATE = 'hate'
class Syncer(object):
def __call__(self, args=None):
if args is None:
args = sys.argv[1:]
self.parse_arguments(args)
if self.options.sync_movies:
self.sync_movies()
if self.options.sync_shows:
self.sync_shows()
def quit_with_error(self, message):
LOG.error(message)
sys.exit(1)
def parse_arguments(self, args):
"""Parses the passed arguments.
"""
parser = OptionParser(version=VERSION, description=DESCRIPTION,
epilog=EPILOG)
parser.add_option(
'-H', '--host', dest='plex_host', default='localhost',
metavar='HOST',
help='Hostname or IP of plex server (default: localhost)')
parser.add_option(
'-P', '--port', dest='plex_port', default=32400,
metavar='PORT',
help='Port of the plex server (default: 32400)')
parser.add_option(
'-u', '--username', dest='trakt_username',
metavar='USERNAME',
help='trakt.tv username')
parser.add_option(
'-p', '--password', dest='trakt_password',
metavar='PASSWORD',
help='trakt.tv password')
parser.add_option(
'--no-movies', dest='sync_movies', action='store_false',
default=True,
help='Do not sync watched movies.')
parser.add_option(
'--no-shows', dest='sync_shows', action='store_false',
default=True,
help='Do not sync watched shows.')
parser.add_option(
'-k', '--key', dest='trakt_key',
metavar='API-KEY',
help='trakt.tv API key')
parser.add_option(
'-r', '--rate', dest='rate', action='store_true',
help='Submit plex movie ratings to trakt.')
parser.add_option(
'--max-hate', dest='max_hate', type='int', metavar='1-10',
default=3,
help='Maxmimum plex rating for flagging a movie with "hate" '
'(In combination with -r option, defaults to 3).')
parser.add_option(
'--min-love', dest='min_love', type='int', metavar='1-10',
default=8,
help='Minimum plex rating for flagging a movie with "love" '
'(In combination with -r option, defaults to 8).')
parser.add_option(
'-v', '--verbose', dest='verbose', action='store_true',
help='Print more verbose debugging informations.')
self.options, self.arguments = parser.parse_args(args)
if self.options.verbose:
LOG.setLevel(logging.DEBUG)
# validate options
if not self.options.trakt_username:
self.quit_with_error('Please define a trakt username (-u).')
if not self.options.trakt_key:
self.quit_with_error('Please define a trakt API key (-k).')
if not self.options.trakt_password:
self.quit_with_error('Please define a trakt password (-p).')
if self.options.max_hate > 10 or self.options.max_hate < 0:
self.quit_with_error('--max-hate should be between 1 and 10')
if self.options.min_love > 10 or self.options.min_love < 0:
self.quit_with_error('--min-love should be between 1 and 10')
def sync_movies(self):
movie_nodes = tuple(self.plex_get_watched_movies())
if movie_nodes:
self.trakt_report_movies(movie_nodes)
if self.options.rate:
self.trakt_rate_movies(movie_nodes)
else:
LOG.warning('No watched movies could be found in your '
'plex server.')
def sync_shows(self):
episode_data = self.plex_get_watched_episodes()
if episode_data:
self.trakt_report_episodes(episode_data)
else:
LOG.warning('No watched show episodes could be found on your '
'plex server.')
def plex_get_watched_movies(self):
for section_path in self._get_plex_section_paths('movie'):
for node in self._plex_request(section_path + 'all'):
if node.getAttribute('viewCount'):
yield node
def plex_get_shows(self, section_path):
return self._plex_request('%sall' % section_path,
nodename='Directory')
def plex_get_seasons(self):
for section_path in self._get_plex_section_paths('show'):
for show in self.plex_get_shows(section_path):
seasons = []
show_key = show.getAttribute('key')
for season in self._plex_request(show_key,
nodename='Directory'):
seasons.append(season)
yield show, seasons
def plex_get_watched_episodes(self):
shows = []
for show, seasons in self.plex_get_seasons():
episodes = []
for season in seasons:
season_key = season.getAttribute('key')
for episode in self._plex_request(season_key):
if episode.getAttribute('viewCount'):
episodes.append((season, episode))
if len(episodes) > 0:
shows.append((show, episodes))
return shows
def get_movie_data(self, node):
"""Returns movie data from a XML node, prepared to post to trakt.
"""
return {'title': node.getAttribute('title'),
'year': node.getAttribute('year'),
'plays': node.getAttribute('viewCount'),
'last_played': node.getAttribute('updatedAt')}
def get_show_data(self, show):
return {'title': show.getAttribute('title'),
'year': show.getAttribute('year')}
def get_movie_rating(self, node):
rating = node.getAttribute('userRating')
if not rating:
return None
rating = int(rating)
if rating >= self.options.min_love:
return RATE_LOVE
elif rating <= self.options.max_hate:
return RATE_HATE
return None
def trakt_report_movies(self, nodes):
movies = []
for node in nodes:
movie = self.get_movie_data(node)
LOG.debug('Mark "%s (%s)" as seen' % (
movie['title'], movie['year']))
movies.append(movie)
LOG.info('Mark %s movies as seen in trakt.tv' % len(movies))
self._trakt_post('movie/seen', {'movies': movies})
def trakt_report_episodes(self, episode_data):
for show, episodes in episode_data:
show_data = self.get_show_data(show)
data = show_data.copy()
data['episodes'] = []
for season, episode in episodes:
data['episodes'].append({
'season': season.getAttribute('index'),
'episode': episode.getAttribute('index')})
LOG.debug(('Mark episode "%s", season %s, episode'
' %s (%s) as seen') % (
data['title'],
season.getAttribute('index'),
episode.getAttribute('index'),
episode.getAttribute('title')))
if self.options.rate:
rating = self.get_movie_rating(episode)
if rating:
episode_data = show_data.copy()
episode_data.update({
'season': season.getAttribute('index'),
'episode': episode.getAttribute('index'),
'rating': rating})
LOG.info(('Rate episode "%s", season %s, episode'
' %s (%s) with "%s"') % (
data['title'],
season.getAttribute('index'),
episode.getAttribute('index'),
episode.getAttribute('title'),
rating))
self._trakt_post('rate/episode', episode_data)
LOG.info(('Mark "%s" episodes of the show %s as '
'seen in trakt.tv') % (
len(data['episodes']), data['title']))
self._trakt_post('show/episode/seen', data)
def trakt_rate_movies(self, nodes):
rated = 0
total = 0
for node in nodes:
total += 1
movie = self.get_movie_data(node)
rating = self.get_movie_rating(node)
if not rating:
continue
movie.update({'rating': rating})
LOG.info('Rate "%s (%s)" with "%s"' % (
movie['title'], movie['year'], rating))
self._trakt_post('rate/movie', movie)
rated += 1
LOG.info('Rated %s of %s movies in trakt.tv' % (
rated, total))
def _get_plex_section_paths(self, type_):
"""Returns all paths to sections of a particular type.
_get_plex_section_paths('movie') => ['/library/sections/1/']
"""
sections_path = '/library/sections'
paths = []
for node in self._plex_request(sections_path,
nodename='Directory'):
if node.getAttribute('type') == type_:
paths.append('%s/%s/' % (
sections_path,
node.getAttribute('key')))
return paths
def _plex_request(self, path, nodename='Video'):
"""Makes a request to plex and parses the XML with minidom.
"""
url = 'http://%s:%s%s' % (
self.options.plex_host,
self.options.plex_port,
path)
LOG.info('Plex request to %s' % url)
response = urllib.urlopen(url)
data = response.read()
doc = parseString(data)
LOG.info('Plex request success')
return doc.getElementsByTagName(nodename)
def _trakt_post(self, path, data):
"""Posts informations to trakt. Data should be a dict which will
be updated with user credentials.
"""
url = 'http://api.trakt.tv/%s/%s' % (path, self.options.trakt_key)
passwd = hashlib.sha1(self.options.trakt_password).hexdigest()
postdata = {'username': self.options.trakt_username,
'password': passwd}
postdata.update(data)
LOG.debug('POST to %s ...' % url)
LOG.debug(pformat(data))
try:
# data = urllib.urlencode(postdata)
request = urllib2.Request(url, json.dumps(postdata))
response = urllib2.urlopen(request)
except urllib2.URLError, e:
LOG.error(e)
raise
resp_data = response.read()
resp_json = json.loads(resp_data)
if resp_json.get('status') == 'success':
if LOG.isEnabledFor(logging.DEBUG):
LOG.debug('Trakt request success: %s' % pformat(resp_json))
else:
filtered_data = dict([(key, value) for (key, value) in resp_json.items()
if not key.endswith('_movies')])
LOG.info('Trakt request success: %s' % pformat(filtered_data))
return True
else:
self.quit_with_error('Trakt request failed with %s' % resp_data)
if __name__ == '__main__':
try:
Syncer()()
except Exception, e:
LOG.error(str(e))
raise