This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trakt-to-couchpotato.py
678 lines (592 loc) · 26.3 KB
/
trakt-to-couchpotato.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#!/usr/bin/env python
# Check needed software dependencies
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
if sys.version_info < (2, 7):
print "Sorry, requires Python 2.7."
sys.exit(1)
import json
import urllib2
# import base64
import hashlib
import logging
import os.path
import argparse
import ConfigParser
# argument parser
parser = argparse.ArgumentParser(description='Import trakt.tv movies into Couch Potato.')
parser.add_argument('-log', help='log file path')
args = parser.parse_args()
# declare variables
trakt_username = ''
trakt_clientid = ''
trakt_clientsecret = ''
trakt_accesstoken = ''
trakt_redirect_uri = ''
cp_apikey = ''
pb_token = ''
movies_added = []
err_count = 0
pb_url = 'https://api.pushbullet.com/v2/pushes'
pb_msg = ''
cp_urls = {
'base': 'http://localhost:5050/api/',
'app_available': '{0}/app.available',
'category_list': '{0}/category.list/',
'movie_add': '{0}/movie.add/?identifier={1}&category_id={2}'
}
trakt_urls = {
'base': 'https://api-v2launch.trakt.tv/',
'authorization': 'https://trakt.tv/oauth/authorize',
'refresh_token': 'oauth/token',
'users_list': 'users/{0}/lists/{1}',
'users_lists': 'users/{0}/lists',
'users_list_items': 'users/{0}/lists/{1}/items',
'remove_users_list_items': 'users/{0}/lists/{1}/items/remove',
'users_settings': 'users/settings'
}
# def md5(text):
# return hashlib.md5(text).hexdigest()
def sha1(text):
return hashlib.sha1(text).hexdigest()
def readconfigfile(f):
logger.info('Parsing configuration file.')
config_object = ConfigParser.ConfigParser()
try:
config_object.readfp(f)
except ConfigParser.Error, err:
logger.exception('Cannot parse configuration file, error: {0}!'.format(err))
return
else:
logger.info('Successfully parsed configuration file.')
return config_object
def iscpavailable(url_path):
url = None
logger.info('Check to see if Couch Potato is available')
logger.info('HTTP request: {0}'.format(url_path))
try:
request = urllib2.Request(url_path)
url = urllib2.urlopen(request)
except urllib2.HTTPError, err:
if err.code == 404:
pass
else:
logger.exception('urllib2 http error code : {0}'.format(err.code))
logger.exception('urllib2 http error reason : {0}'.format(err.reason))
return {"success": False}
except urllib2.URLError, err:
logger.exception('urllib2 url error reason : {0}'.format(err.reason))
return {"success": False}
try:
return json.load(url)
except ValueError, e:
logger.exception('Error in Couch Potato API: {0}'.format(e))
return {"success": False}
def getsetcpdata(url_path, param1=None, param2=None):
url = None
logger.info('Calling Couch Potato API')
if param1 is None and param2 is None:
request = (cp_urls['base'] + url_path).format(cp_apikey)
else:
request = (cp_urls['base'] + url_path).format(cp_apikey, param1, param2)
logger.info('HTTP request: {0}'.format(request))
try:
request = urllib2.Request(request)
url = urllib2.urlopen(request)
except urllib2.HTTPError, err:
if err.code == 404:
pass
else:
logger.exception('urllib2 http error code : {0}'.format(err.code))
logger.exception('urllib2 http error reason : {0}'.format(err.reason))
return {"success": False}
except urllib2.URLError, err:
logger.exception('urllib2 url error reason : {0}'.format(err.reason))
return {"success": False}
try:
return json.load(url)
except ValueError, e:
logger.exception('Error in Couch Potato API: {0}'.format(e))
return {"success": False}
def istrakttokenexpired(url_path):
logger.info('Checking Trakt.TV token expiration')
logger.info('HTTP request: {0}'.format(url_path))
request = urllib2.Request(url_path)
request.add_header("Content-type", "application/json")
request.add_header("trakt-api-version", 2)
request.add_header("trakt-api-key", trakt_clientid)
# need to provide this using auth response
request.add_header("Authorization", "Bearer {0}".format(trakt_accesstoken))
logger.debug('Request headers: {0}'.format(request.headers))
try:
urllib2.urlopen(request)
except urllib2.HTTPError, err:
if err.code == 401:
logger.warning('Token is expired or not valid')
return 1
else:
logger.exception('urllib2 http error code : {0}'.format(err.code))
logger.exception('urllib2 http error reason : {0}'.format(err.reason))
return
except urllib2.URLError, err:
logger.exception('urllib2 url error reason : {0}'.format(err.reason))
return
finally:
logger.debug('Request headers: {0}'.format(request.headers))
def refreshtrakttoken(url_path, post_data):
url = None
logger.info('Refreshing Trakt.TV access token')
logger.info('HTTP request: {0}'.format(url_path))
logger.debug('Post data: {0}'.format(post_data))
try:
request = urllib2.Request(url_path)
request.add_header("Content-type", "application/json")
url = urllib2.urlopen(request, json.dumps(post_data))
except urllib2.HTTPError, err:
logger.exception('urllib2 http error code : {0}'.format(err.code))
logger.exception('urllib2 http error reason : {0}'.format(err.reason))
return "HTTP Error {0}: {1}".format(err.code, err.reason)
except urllib2.URLError, err:
logger.exception('urllib2 url error reason : {0}'.format(err.reason))
return
try:
trakt_return = json.load(url)
if len(trakt_return) > 0:
return trakt_return
else:
return url.getcode()
except ValueError, e:
logger.exception('Error in Trakt API: {0}'.format(e))
return
def getsettraktdata(url_path, post_data=None):
url = None
logger.info('Calling Trakt.TV API')
logger.info('HTTP request: {0}'.format(url_path))
request = urllib2.Request(url_path)
request.add_header("Content-type", "application/json")
request.add_header("trakt-api-version", 2)
request.add_header("trakt-api-key", trakt_clientid)
# need to provide this using auth response
request.add_header("Authorization", "Bearer {0}".format(trakt_accesstoken))
logger.debug('Request headers: {0}'.format(request.headers))
try:
if post_data is None:
url = urllib2.urlopen(request)
else:
logger.debug('Post data: {0}'.format(post_data))
url = urllib2.urlopen(request, json.dumps(post_data))
except urllib2.HTTPError, err:
logger.exception('urllib2 http error code : {0}'.format(err.code))
logger.exception('urllib2 http error reason : {0}'.format(err.reason))
return "HTTP Error {0}: {1}".format(err.code, err.reason)
except urllib2.URLError, err:
logger.exception('urllib2 url error reason : {0}'.format(err.reason))
return
try:
trakt_return = json.load(url)
if len(trakt_return) > 0:
return trakt_return
else:
return url.getcode()
except ValueError, e:
logger.exception('Error in Trakt API: {0}'.format(e))
return
def gettraktusername(url):
logger.info('Attempting to retrieve Trakt username.')
trakt_result = getsettraktdata(url)
if isinstance(trakt_result, (dict, list)):
logger.info('Success, username: {0}'.format(trakt_result['user']['username']))
return trakt_result['user']['username']
else:
logger.exception('Error retrieving Trakt username, return: {0}'.format(trakt_result))
return
def createtraktlist(url, data):
logger.info('Attempting to create the "{0}" list'.format(data['name']))
trakt_result = getsettraktdata(url, data)
if trakt_result == 201:
logger.info('Successfully created the list')
return 1
elif isinstance(trakt_result, (dict, list)):
if trakt_result['name'] == data['name']:
logger.info('Successfully created the list')
return 1
else:
logger.exception('Error creating the list, return: {0}'.format(trakt_result))
return
def istraktlist(url, listname):
logger.info('Checking to see if a list with the name "{0}" already exists'.format(listname))
trakt_result = getsettraktdata(url)
if isinstance(trakt_result, (dict, list)):
for rs in trakt_result:
if rs['name'] == listname:
logger.info('The list already exists')
return 1
elif trakt_result == 200:
logger.info('The list does not exist')
return
else:
_Exit(1, 'An unknown error occurred determining if the "{0}" Trakt.tv list exists --- Error: {1}'
.format(listname, trakt_result))
def gettraktslug(url, listname):
logger.info('Retrieving Trakt.TV slug for the "{0}" list'.format(listname))
trakt_result = getsettraktdata(url)
if isinstance(trakt_result, (dict, list)):
for rs in trakt_result:
if rs['name'] == listname:
logger.info('Slug found: {0}'.format(rs['ids']['slug']))
return rs['ids']['slug']
elif trakt_result == 200:
logger.info('Unable to find slug')
return ''
else:
_Exit(1, 'An unknown error occurred determining the Trakt.tv list slug for {0} list --- Error: {1}'
.format(listname, trakt_result))
def pushbullet_push(message=''):
if pb_token is None:
return
url = None
logger.info('Sending Pushbullet notification.')
logger.info('Message body: {0}'.format(message))
request = pb_url
logger.info('HTTP request: {0}'.format(request))
post_data = {'type': 'note',
'title': os.path.splitext(os.path.basename(__file__))[0],
'body': message
}
try:
request = urllib2.Request(request)
request.add_header("Content-Type", "application/json")
request.add_header("Authorization", "Bearer {0}".format(pb_token))
logger.debug('Request headers: {0}\nPost data: {1}'.format(request.headers, post_data))
url = urllib2.urlopen(request, json.dumps(post_data))
except urllib2.HTTPError, err:
if err.code == 404:
pass
else:
logger.exception('urllib2 http error code : {0}'.format(err.code))
logger.exception('urllib2 http error reason : {0}'.format(err.reason))
return
except urllib2.URLError, err:
logger.exception('urllib2 url error reason : {0}'.format(err.reason))
return
try:
pb_return = json.load(url)
if len(pb_return) > 0:
logger.info('Successfully sent Pushbullet notification.')
else:
logger.exception('Error sending Pushbullet notification.')
return
except ValueError, e:
logger.exception('Error in Pushbullet API: {0}'.format(e))
return
def _Exit(err=0, message=''):
if len(message) == 0:
if err > 0:
message = 'Error {0} occured, please check the log file:\n{1}'.format(err, logfile)
else:
message = 'The script has successfully completed.'
if len(movies_added) > 0:
message = 'The following movie(s) were added to Couch Potato:\n\n- {0}' \
.format('\n- '.join(map(str, movies_added)))
else:
if err > 0:
logger.error('Error {0} --- {1}'.format(err, message))
elif err_count > 0:
message = '{0} However, {1} warning(s) were recorded.'.format(message, err_count)
logger.warning(message)
else:
logger.info(message)
if len(pb_msg) > 0:
message = '{0}\n\nMore information:{1}'.format(message, pb_msg)
# Pushbullet push
if err >= 0:
pushbullet_push(message)
else: # suppress pushbullet for normal exits
logger.info('Normal exit, Pushbullet notification suppressed.')
logger.info('Exiting the script.')
logger.info('-' * 80)
sys.exit(err)
# enter main method
if __name__ == "__main__":
# create logging object
logger = logging.getLogger(__name__)
if __debug__:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
# create a file handler
if args.log is not None and len(args.log) > 0:
logfile = args.log
else:
# logfile = os.path.splitext(os.path.basename(__file__))[0] + '.log'
logfile = '{0}//{1}.log'.format(os.path.dirname(os.path.realpath(__file__)),
os.path.splitext(os.path.basename(__file__))[0])
handler = logging.FileHandler(logfile)
if __debug__:
handler.setLevel(logging.DEBUG)
else:
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
if __debug__:
logger.info('Debug logging enabled')
else:
logger.info('Debug logging disabled')
# read config file
filepath = '{0}//config.ini'.format(os.path.dirname(os.path.realpath(__file__)))
try:
configfile = open(filepath, 'r')
except IOError as e:
_Exit(1, 'I/O error({0}): {1}'.format(e.errno, e.strerror))
else:
logger.info('Configuration file exists at the following path: {0}'.format(filepath))
logger.info('Attempting to parse config file contents.')
# configfile = os.path.join(os.path.abspath(os.path.dirname(os.path.abspath(__file__))), 'config.ini')
config = readconfigfile(configfile)
configfile.close()
if isinstance(config, ConfigParser.ConfigParser):
# trakt config settings
if config.has_section('trakt'):
if config.has_option('trakt', 'clientid'):
trakt_clientid = config.get('trakt', 'clientid')
if config.has_option('trakt', 'clientsecret'):
trakt_clientsecret = config.get('trakt', 'clientsecret')
if config.has_option('trakt', 'accesstoken'):
trakt_accesstoken = config.get('trakt', 'accesstoken')
if config.has_option('trakt', 'refreshtoken'):
trakt_refreshtoken = config.get('trakt', 'refreshtoken')
if config.has_option('trakt', 'redirecturi'):
trakt_redirect_uri = config.get('trakt', 'redirecturi')
# couch potato settings
if config.has_section('couchpotato'):
if config.has_option('couchpotato', 'apikey'):
cp_apikey = config.get('couchpotato', 'apikey')
if config.has_option('couchpotato', 'apiurl'):
cp_urls['base'] = config.get('couchpotato', 'apiurl') # hard code the url of CP server api
# pushbullet settings
if config.has_section('pushbullet'):
if config.has_option('pushbullet', 'token'):
pb_token = config.get('pushbullet', 'token')
if config.has_option('pushbullet', 'apiurl'):
pb_url = config.get('pushbullet', 'apiurl')
else:
_Exit(1, 'Unable to parse config file!')
# check for Couch Potato availability
cp_available = iscpavailable(cp_urls['base'] + cp_urls['app_available'].format(cp_apikey))
if cp_available['success']:
logger.info('Success, Couch Potato is available.')
else:
_Exit(2, 'Couch Potato does not appear to be available.')
# get category data from Couch Potato
logger.info('Attempting to retrieve category information from Couch Potato.')
cp_categories = getsetcpdata(cp_urls['category_list'])
category_list = None
if cp_categories['success']:
logger.info('At least one or more categories was found, querying for name and id.')
category_list = []
for rs in cp_categories['categories']: # changed from 'list' to 'categories' (CPS API change)
category_list.append({
'name': rs['label'],
'id': rs['_id'] # changed from 'id' to '_id' (CPS API change)
})
# changed from 'id' to '_id' and %d to %s for unicode _id value (CPS API change)
logger.info('name:{0}, id:{1}'.format(rs['label'], rs['_id']))
if len(category_list) > 0:
logger.info('Success, found {0} categories.'.format(len(category_list)))
else:
_Exit(2, 'Unable to build category list from Couch Potato.')
else:
_Exit(2, 'Error retrieving Couch Potato category information. Error: {0}'.format(cp_categories['error']))
# verify Trakt authentication
# check for 401 - unauthorized
# if unauthorized perform a refresh token process
if istrakttokenexpired(trakt_urls['base'] + trakt_urls['users_settings']):
logger.warning("Trakt.TV access token is expired, attempting to refresh the token")
data = {
'refresh_token': trakt_refreshtoken,
'client_id': trakt_clientid,
'client_secret': trakt_clientsecret,
'redirect_uri': trakt_redirect_uri,
'grant_type': 'refresh_token'
}
trakt_result = refreshtrakttoken(trakt_urls['base'] + trakt_urls['refresh_token'], data)
if isinstance(trakt_result, (dict, list)):
# write access token and refresh token into config file
trakt_accesstoken = trakt_result['access_token']
trakt_refreshtoken = trakt_result['refresh_token']
config.set('trakt', 'accesstoken', trakt_accesstoken)
config.set('trakt', 'refreshtoken', trakt_refreshtoken)
with open(filepath, 'w') as myconfig:
config.write(myconfig)
else:
_Exit(3, "Unable to refresh access token")
else:
logger.info("Trakt.TV access token is valid")
# get Trakt username
trakt_username = gettraktusername(trakt_urls['base'] + trakt_urls['users_settings'])
if len(trakt_username) == 0:
_Exit(3, 'Unable to retrieve Trakt username.')
# create Trakt lists (that don't already exist) for Couch Potato categories
logger.info('Attempting to create Trakt lists (that don\'t already exist) for Couch Potato categories.')
for category in category_list:
cat_name = str(category['name'])
data = {
'name': cat_name,
'description': '',
'privacy': 'private'
}
# if not istraktlist((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username), category['name']):
if not istraktlist((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username), cat_name):
if not createtraktlist((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username), data):
_Exit(3, 'Unable to create Trakt.TV list "{0}"'.format(cat_name))
# create archive Trakt list (that don't already exist)
# logger.info('Attempting to create Trakt archive lists (that don\'t already exist).')
data['name'] += '_archive'
if not istraktlist((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username), data['name']):
if not createtraktlist((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username), data):
_Exit(3, 'Unable to create Trakt.TV list "{0}"'.format(data['name']))
# query trakt.tv for matching list slugs
logger.info('Attempting to retrieve slug information for custom Trakt.tv lists.')
i = 0
for category in category_list:
list_slug = gettraktslug((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username),
category['name'])
if len(list_slug) > 0:
i += 1
category['slug'] = list_slug
else:
logger.warning('Unable to obtain slug information for {0}'.format(category['name']))
list_slug = gettraktslug((trakt_urls['base'] + trakt_urls['users_lists']).format(trakt_username),
category['name'] + '_archive')
if len(list_slug) > 0:
i += 1
category['slug_archive'] = list_slug
else:
logger.warning('Unable to obtain slug information for {0}'.format(category['name']))
if i > 0:
logger.info('Success, retrieved {0} slug(s).'.format(i))
else:
_Exit(4, 'Error occured or no slug information was retrieved from Trakt.tv.')
# query trakt.tv for list details using slug
logger.info('Attempting to retrieve list items from Trakt.tv.')
movies = []
for category in category_list:
logger.info('Retrieving items from the "{0}" list.'.format(category['name']))
trakt_list_items = getsettraktdata((trakt_urls['base'] + trakt_urls['users_list_items'])
.format(trakt_username, category['slug']))
if isinstance(trakt_list_items, (dict, list)):
i = 0
for item in trakt_list_items:
if item['type'] == 'movie':
i += 1
movies.append({
'title': item['movie']['title'],
'imdb_id': item['movie']['ids']['imdb'],
'cp_cat_id': category['id'],
'trakt_name': category['name'],
'trakt_slug': category['slug'],
'trakt_slug_archive': category['slug_archive']
})
logger.info('Success, retrieved {0} movie(s) from the list.'.format(i))
elif trakt_list_items == 200:
pass
else:
_Exit(5, 'Error occured or no list information was retrieved from Trakt.tv.')
if isinstance(movies, (dict, list)) and len(movies) > 0:
logger.info('Total movie(s) retrieved: {0}.'.format(len(movies)))
else:
# no movies waiting to be added to Couch Potato (success)
logger.warning('No movies found in any Trakt list(s), exiting.')
_Exit(-1) # Exit without pushbullet notification (this is a normal exit)
# add movies into Couch Potato wanted list
logger.info('Attempting to add movie(s) into Couch Potato wanted list.')
for movie in list(movies):
if len(movie['imdb_id']) > 0:
cp_result = getsetcpdata(cp_urls['movie_add'], movie['imdb_id'], movie['cp_cat_id'])
if cp_result['success']:
movies_added.append(movie['title'])
logger.info('Success, {0} has been added.'.format(movie['title']))
else:
err_count += 1
logger.error('{0} was not added. Error: {1}'.format(movie['title'], cp_result['error']))
del movies[movies.index(movie)]
else:
err_count += 1
logger.error('{0} was not added. No IMDB ID was found for the movie'.format(movie['title']))
pb_msg = '{0}\n{1} was not added. No IMDB ID was found for the movie'.format(pb_msg, movie['title'])
del movies[movies.index(movie)]
if len(movies) > 0:
logger.info('Total movie(s) added to Couch Potato: {0}.'.format(len(movies)))
else:
logger.warning('No movies were added to Couch Potato, exiting.')
_Exit()
# remove movie from Trakt list
logger.info('Attempting to remove movie(s) from Trakt lists.')
for movie in list(movies):
logger.info('Removing {0} from the Trakt list (slug) "{1}".'.format(movie['title'], movie['trakt_slug']))
data = {
'movies': [
{
'ids': {
'imdb': str(movie['imdb_id'])
}
}
]
}
trakt_result = getsettraktdata(
(trakt_urls['base'] + trakt_urls['remove_users_list_items']).format(trakt_username, movie['trakt_slug']),
data)
if isinstance(trakt_result, (dict, list)):
if trakt_result['deleted']['movies'] > 0:
logger.info('Success, the movie has been removed')
else:
err_count += 1
logger.debug('Error, {0}'.format(trakt_result))
logger.error('Error, unable to remove the movie.')
del movies[movies.index(movie)]
pb_msg = '{0}\nUnable to remove {1} from the Trakt list (slug) "{2}"\n'.format(pb_msg, movie['title'],
movie['trakt_slug'])
else:
_Exit(6, 'Error occured removing the item from the Trakt list.')
if len(movies) > 0:
logger.info('Total movie(s) removed from Trakt lists: {0}.'.format(len(movies)))
else:
logger.warning('No movies were removed from Trakt lists, exiting.')
_Exit()
# now we can add the movie into the archive list
logger.info('Attempting to add movie(s) into Trakt archive lists.')
for movie in movies:
if movie['trakt_slug_archive'] is not None:
logger.info(
'Adding {0} to the archive Trakt list (slug) "{1}".'.format(movie['title'],
movie['trakt_slug_archive']))
data = {
"movies": [
{
"ids": {
"imdb": movie['imdb_id']
}
}
]
}
trakt_result = getsettraktdata(
(trakt_urls['base'] + trakt_urls['users_list_items']).format(trakt_username,
movie['trakt_slug_archive']), data)
if isinstance(trakt_result, (dict, list)):
if trakt_result['added']['movies'] > 0:
logger.info('Success, the movie has been added to the archive list')
elif trakt_result['existing']['movies'] > 0:
logger.info('Success, the movie was already in the archive list')
else:
err_count += 1
logger.error('Error, unable to add the movie to the archive list.')
pb_msg = '{0}\nError, unable to add {1} to the archive list (slug) "{2}"\n'\
.format(pb_msg, movie['title'], movie['trakt_slug_archive'])
else:
_Exit(7, 'Error occurred adding the movies to the Trakt archive list.')
# Exit script
_Exit()