-
Notifications
You must be signed in to change notification settings - Fork 7
/
pingrr.py
445 lines (346 loc) · 16.3 KB
/
pingrr.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
import pingrr.config as config
import json
import logging
from logging.handlers import RotatingFileHandler
import sys
import requests
from time import sleep
#from imdb import IMDb
#i = IMDb()
################################
# Logging
################################
# Logging format
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# root logger
logger = logging.getLogger()
# Set initial level to INFO
logger.setLevel(logging.DEBUG)
# Console handler, log to stdout
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
# Other modules logging levels
logging.getLogger("requests").setLevel(logging.WARNING)
################################
# Load config
################################
# Load initial config
configuration = config.Config()
# Set configured log level
logger.setLevel(configuration.settings['loglevel'])
# Load config file
configuration.load()
conf = configuration.config
# Print config file to log on run
logger.info(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': ')))
# Log file handler
fileHandler = RotatingFileHandler(configuration.settings['logfile'], maxBytes=1024 * 1024 * 2, backupCount=1)
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
################################
# Init
################################
import pingrr.trakt as trakt
import pingrr.sonarr as sonarr
import pingrr.justWatch as justWatch
import pingrr.radarr as radarr
from pingrr.notifications import Notifications
new = []
delay_time = conf['pingrr']['timer'] * 3600
options = {"ignoreEpisodesWithFiles": False, "ignoreEpisodesWithoutFiles": False,
"searchForMissingEpisodes": conf['sonarr']['search_missing_episodes']}
notify = Notifications()
if conf['pushover']['enabled']:
notify.load(service="pushover", app_token=conf['pushover']['app_token'], user_token=conf['pushover']['user_token'])
if conf['slack']['enabled']:
notify.load(service="slack", webhook_url=conf['slack']['webhook_url'], sender_name=conf['slack']['sender_name'],
sender_icon=conf['slack']['sender_icon'], channel=conf['slack']['channel'])
################################
# Main
################################
def create_path(genres, program):
"""Create path based on genre for sonarr/radarr"""
# Set root folder for path creation
root_folder = conf[program]['path_root']
# Check if any of the genres match up, only if genre paths are in config
if 'paths' in conf[program]:
for key in conf[program]['paths']:
for genre in conf[program]['paths'][key]:
if genre in genres:
return root_folder + key + '/'
# If no match, return default path
return conf[program]['folder_path']
def send_to_sonarr(a, b, genres):
"""Send found tv program to sonarr"""
logger.info("Attempting to send to sonarr")
path = create_path(genres, "sonarr")
payload = {"tvdbId": a, "title": b, "qualityProfileId": conf['sonarr']['quality_profile'], "images": [],
"seasons": [], "seasonFolder": True, "monitored": conf['sonarr']['monitored'], "rootFolderPath": path,
"addOptions": options, }
if conf['pingrr']['dry_run']:
logger.info("dry run is on, not sending to sonarr")
return True
r = requests.post(sonarr.url + '/api/series', headers=sonarr.headers, data=json.dumps(payload), timeout=30)
if r.status_code == 201:
logger.debug("sent to sonarr successfully")
return True
else:
logger.debug("failed to send to sonarr, code return: %r", r.status_code)
return False
def send_to_radarr(a, b, genres, year):
"""Send found tv program to radarr"""
logger.info("Attempting to send to radarr")
path = create_path(genres, "radarr")
payload = {"tmdbId": a,
"title": b,
"qualityProfileId": conf['radarr']['quality_profile'],
"images": [],
"monitored": conf['radarr']['monitored'],
"titleSlug": b,
"rootFolderPath": path,
"minimumAvailability": "preDB",
"year": year
}
if conf['pingrr']['dry_run']:
logger.info("dry run is on, not sending to radarr")
return True
r = requests.post(radarr.url + '/api/movie', headers=radarr.headers, data=json.dumps(payload), timeout=30)
response = r.json()
if r.status_code == 201:
radarr.search_movie(response['id'])
logger.debug("sent to radarr successfully")
return True
else:
logger.debug("failed to send to radarr, code return: %r", r.status_code)
return False
def add_media(program):
added_list = []
n = 0
limit = conf['pingrr']['limit'][program]
for media in new:
title = media['title']
if program == "radarr":
media_id = media['tmdb']
elif program == "sonarr":
media_id = media['tvdb']
try:
logger.debug('Sending media to {}: {}'.format(program, media['title'].encode('utf8')))
if program == "sonarr":
if send_to_sonarr(media_id, title, media['genres']):
logger.info('{} has been added to Sonarr'.format(title.encode('utf8')))
added_list.append(media['title'])
if media['aired'] >= conf['pingrr']['aired']:
n += 1
else:
logger.info(
"{} only has {} episodes, does not count towards add limit".format(media['title'].encode('utf8'),
media['aired']))
if 0 < limit == n:
logger.info('{} shows added limit reached'.format(str(n)))
break
elif limit > 0 and not n == limit:
logger.debug('limit not yet reached: {}'.format(str(n)))
else:
configuration.blacklist.add(str(media["tvdb"]))
logger.warning('{} failed to be added to Sonarr! Adding to blacklist'.format(title.encode('utf8')))
if program == "radarr":
if send_to_radarr(media_id, title, media['genres'], media['year']):
logger.info('{} has been added to Radarr'.format(title.encode('utf8')))
added_list.append(media['title'])
n += 1
if 0 < limit == n:
logger.info('{} shows added limit reached'.format(str(n)))
break
elif limit > 0 and not n == limit:
logger.debug('limit not yet reached: {}'.format(str(n)))
else:
configuration.blacklist.add(str(media["tmdb"]))
logger.warning('{} failed to be added to Radarr! Adding to blacklist'.format(title.encode('utf8')))
except IOError:
logger.warning('error sending media: {} id: {}'.format(title.encode('utf8'), str(media_id)))
if conf['pushover']['enabled'] or conf['slack']['enabled'] and n != 0:
message = "The following {} item(s) out of {} added to {}:\n{}".format(str(len(added_list)), str(len(new)), program,
"\n".join(added_list))
logger.warning(message)
notify.send(message=message)
def new_check(item_type):
"""Check for new trakt items in list"""
if item_type == "movies":
library = radarr.get_library()
program = "radarr"
else:
library = sonarr.get_library()
program = "sonarr"
global new
new = filter_list(item_type)
logger.info('checking for new {} in lists'.format(item_type))
if item_type == "movies":
item_id = "imdb"
else:
item_id = "tvdb"
for x in new:
logger.debug('checking {} from list: {}'.format(item_type, x['title'].encode('utf8')))
if x[item_id] not in library and conf['filters']['allow_ended']:
logger.info('new media found, adding {} {} now'.format(len(new), item_type))
add_media(program)
break
if item_type == "shows":
if x[item_id] not in library and not x['status'] == 'ended':
logger.info('new continuing show(s) found, adding shows now')
add_media(program)
break
def check_lists(arg, arg2):
for filters in conf['filters'][arg]:
for data in arg2:
if filters == data:
return True
return False
def filter_check(title, item_type):
if item_type == "shows":
if len(title['country']):
country = title['country'].lower()
else:
country = False
type_id = "tvdb"
library = sonarr_library
elif item_type == "movies":
type_id = "tmdb"
library = radarr_library
country = False
else:
return False
lang = title['language']
if title[type_id] not in library:
if str(title['imdb']) in configuration.blacklist or str(title[type_id]) in configuration.blacklist:
logger.info("{} was rejected as it was found in the blacklist".format(title['title'].encode('utf8')))
return False
logger.debug("Checking year: {}".format(title['year']))
if conf['filters']['year'][item_type] > title['year']:
logger.info("{} was rejected as it was outside allowed year range: {}".format(title['title'].encode('utf8'),
str(title['year'])))
return False
logger.debug("Checking runtime: {}".format(title['runtime']))
if conf['filters']['runtime'] > title['runtime']:
logger.info("{} was rejected as it was outside allowed runtime: {}".format(title['title'].encode('utf8'),
str(title['runtime'])))
return False
if item_type == "shows":
if len(conf['filters']['network']) > 0:
if title['network'] is None or conf['filters']['network'] in title['network']:
logger.info("{} was rejected as it was by a disallowed network: {}"
.format(title['title'].encode('utf8'), str(title['network']).encode('utf8')))
return False
logger.debug("Checking votes: {}".format(title['votes']))
if conf['filters']['votes'] > title['votes']:
logger.info(
"{} was rejected as it did not meet vote requirement: {}".format(title['title'].encode('utf8'),
str(title['votes'])))
return False
if conf['filters']['allow_ended'] is False and 'ended' in title['status']:
logger.info("{} was rejected as it is an ended tv series".format(title['title'].encode('utf8')))
return False
if item_type == "shows":
if conf['filters']['allow_canceled'] is False and 'canceled' in title['status']:
logger.info("{} was rejected as it an canceled tv show".format(title['title'].encode('utf8')))
return False
logger.debug("Checking rating: {}".format(title['rating']))
if float(title['rating']) < float(conf['filters']['rating']):
logger.info(
"{} was rejected as it was outside the allowed ratings: {}".format(title['title'].encode('utf8'),
str(title['rating'])))
return False
logger.debug("Checking genres: {}".format(title['genres']))
if isinstance(conf['filters']['genre'], list):
if check_lists('genre', title['genres']):
logger.info("{} was rejected as it wasn't a wanted genre: {}".format(title['title'].encode('utf8'),
str(title['genres'])))
return False
elif conf['filters']['genre'] == title['genres']:
logger.info("{} was rejected as it wasn't a wanted genre: {}".format(title['title'].encode('utf8'),
str(title['genres'])))
return False
logger.debug("Checking country: {}".format(country))
if country and country not in conf['filters']['country']:
logger.info("{} was rejected as it wasn't a wanted country: {}".format(title['title'].encode('utf8'),
str(title['country'])))
return False
logger.debug("Checking language: {}".format(lang))
if lang not in conf['filters']['language']:
logger.info("{} was rejected as it wasn't a wanted language: {}".format(title['title'].encode('utf8'), lang))
return False
return True
else:
logger.info("{} was rejected as it is already in {} library".format(title['title'].encode('utf8'), item_type))
def filter_list(list_type):
# Create the lists ready to be filtered down
if list_type == 'shows':
raw_list = []
item_id = "tvdb"
for trakt_list in conf['trakt']['tv_list']:
if conf['trakt']['tv_list'][trakt_list]:
raw_list = trakt.get_info('tv')
break
# if conf['allflicks']['enabled']['shows']:
# raw_list += allflicks.create_list()
if conf['just_watch']['enabled']['shows']:
raw_list += justWatch.create_list("shows")
if list_type == 'movies':
item_id = "tmdb"
raw_list = []
for trakt_list in conf['trakt']['movie_list']:
if conf['trakt']['movie_list'][trakt_list]:
raw_list = trakt.get_info('movie')
break
if conf['just_watch']['enabled']['movies']:
raw_list += justWatch.create_list("movies")
fixed_raw = []
for raw in raw_list:
try:
fixed_raw.append(raw[0])
except KeyError:
fixed_raw.append(raw)
raw_list = fixed_raw
filtered = []
for title in raw_list:
try:
# If not already in the list, check against filters
if filter_check(title, list_type) and title[item_id] not in filtered:
logger.info('adding {} to potential add list'.format(title['title'].encode('utf8')))
filtered.append(title)
except TypeError:
logger.debug('{} failed to check against filters'.format(title['title'].encode('utf8')))
logger.debug("Filtered list successfully")
return filtered
if __name__ == "__main__":
while True:
logger.info("\n\n###### Checking if TV lists are wanted ######\n")
if conf['sonarr']['api']:
try:
sonarr_library = sonarr.get_library()
new_check('shows')
except requests.exceptions.ReadTimeout:
logger.warning("Sonarr library timed out, skipping for now")
except requests.exceptions.ConnectionError:
logger.warning("Can not connect to Sonarr, check sonarr is running or host is correct")
logger.info("\n\n###### Checking if Movie lists are wanted ######\n")
if conf['radarr']['api']:
try:
radarr_library = radarr.get_library()
new_check('movies')
except requests.exceptions.ReadTimeout:
logger.warning("Radarr library timed out, skipping for now")
except requests.exceptions.ConnectionError:
logger.warning("Can not connect to Radarr, check Radarr is running or host is correct")
# Save updated blacklist
configuration.save_blacklist()
if conf['pingrr']['timer'] == 0:
logger.info('Scan finished, shutting down')
sys.exit()
if conf['pingrr']['timer'] > 1:
hours = "s"
else:
hours = ""
logger.info("check finish, sleeping for {} hour{}".format(conf['pingrr']['timer'], hours))
sleep(float(delay_time))
logger.debug('sleep over, checking again')