Repository navigation
Expand file tree
/
Copy pathwantedMissingSonarr.py
More file actions
102 lines (80 loc) · 2.97 KB
/
Copy pathwantedMissingSonarr.py
File metadata and controls
102 lines (80 loc) · 2.97 KB
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
#!/usr/bin/env python
#
################################
# Simple script to get the wanted missing from sonarr and force a
# search on them one by one with a delay timer in between
################################
import logging
import requests
import time
################################
# Config
################################
host = "https://sonarr.doamin.com/" # Sonarr URL
api_key = "xxxxxxxxxxxxxxxxxxx" # Sonarr API Key
page_size = "150" # Number of episodes to go back and search
refresh = True # Refresh series before performing a search
delay = 10 # timer in seconds between requesting search
refresh_delay = 5 # timer in seconds to delay between refreshing and searching
################################
# Logging
################################
logger = logging.getLogger(__name__)
################################
# Init
################################
headers = {'X-Api-Key': api_key,
'Content-Type': 'application/json'}
page = "1" # Do not change
sortDirection = "descending" # Do not change
monitored = "true" # Do not change
sortKey = "airDateUtc" # Do not change
################################
# Main
################################
def get_missing_list():
missing = []
data = "page={}&pageSize={}&sortDirection={}&sortKey={}&monitored={}".format(
page,
page_size,
sortDirection,
sortKey,
monitored
)
r = requests.get(host + 'api/wanted/missing?{}'.format(data), headers=headers, timeout=60, json=data)
try:
if r.status_code == 401:
logger.warning("Error when connecting to sonarr, unauthorised. check api/url")
except requests.ConnectionError:
logger.warning("Can not connect to sonarr check if sonarr is up, or URL is right")
return r.json()
def search_missing_ep(ep_id):
data = {"name": "EpisodeSearch",
"episodeIds": [ep_id]}
r = requests.post(host + 'api/v3/command', headers=headers, timeout=60, json=data)
def refresh_series(s_id):
data = {"name": "RefreshSeries",
"seriesId": s_id}
r = requests.post(host + 'api/v3/command', headers=headers, timeout=60, json=data)
if r.status_code == 201:
return True
else:
return False
missing_episodes = get_missing_list()
for episode in missing_episodes['records']:
refreshed = []
if refresh and episode['series']['id'] not in refreshed:
print("Refreshing series: {}".format(episode['series']['title']))
refresh_series(episode['series']['id'])
refreshed.append(episode['series']['id'])
print("Waiting {} seconds for refresh".format(refresh_delay))
time.sleep(refresh_delay)
try:
print("Forcing scan: {} - {}".format(episode['series']['title'], episode['title']))
except:
pass
search_missing_ep(episode['id'])
print("Waiting {} seconds until moving to next episode".format(delay))
print("#################")
time.sleep(delay)
print("#################\n### Finished ###\n#################")