-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonarr_redownloader.py
121 lines (111 loc) · 5.32 KB
/
sonarr_redownloader.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
import json
from time import sleep
import requests
MAX_TIMEOUT = 3600 # Time in seconds
MAX_CONNECTION_RETRIES = 10 # Number of attempts
CONNECTION_RETRY_TIMEOUT = 10 # Time in seconds
def content_redownloader():
"""Have Sonarr perform a search in order to upgrade content."""
# Obtain Sonarr URL and API key
print("\n ** ex) http://192.168.86.20:8989")
sonarr_url = str(input("Sonarr URL: "))
api_key = str(input("Sonarr API key: "))
# Check connection to Sonarr and get a list of all series
get_series_url = sonarr_url + "/api/series?apikey=" + api_key
get_series_status = 404
connection_retries = 0
while get_series_status != 200:
try:
get_series_response = requests.get(get_series_url)
get_series_status = get_series_response.status_code
if get_series_status == 200:
break
except:
pass
print("Failed communication with Sonarr! Retrying in " + CONNECTION_RETRY_TIMEOUT + " seconds...")
connection_retries = connection_retries + 1
sleep(CONNECTION_RETRY_TIMEOUT)
if connection_retries > MAX_CONNECTION_RETRIES:
return False
series_list = json.loads(get_series_response.content)
# Obtain user preferences
print("\n ** ex) /media/TV or /media")
root_dir = str(input("Root directory to upgrade (optional): "))
max_episodes = input("Skip shows with more than _____ episodes (optional): ")
try:
if int(max_episodes) <= 0:
max_episodes = 1000000
except:
max_episodes = 1000000
starting_series = input("Show name to start at (optional): ")
rapid_mode = False
if str(input("Rapid mode [Y/N] (optional): ")).lower() == "y":
print("\nWARNING: Rapid immediately queues all search queries. This can overwhelm Sonarr, and is difficult to stop once started.")
if str(input("Are you sure? [Y/N]: ")).lower() == "y":
rapid_mode = True
# Search for file upgrades
counter = -1
for series in series_list:
if series['path'][:len(root_dir)] == root_dir:
counter = counter + 1
print(str(counter) + ": Processing " + series['title'])
# Check if current series should be skipped
if starting_series.lower() != series['title'].lower()[:len(starting_series)]:
print("This is not the requested starting show. Skipping...")
continue
starting_series = ""
if series['episodeCount'] > int(max_episodes):
print("Show has more episodes than the limit. Skipping...")
continue
# Command Sonarr to perform a series search
command_search_url = sonarr_url + "/api/command?apikey=" + api_key
command_search_parameters = {"name":"SeriesSearch", "seriesId":int(series['id'])}
command_search_status = 404
connection_retries = 0
while command_search_status != 201:
try:
command_search_response = requests.post(command_search_url, json.dumps(command_search_parameters))
command_search_status = command_search_response.status_code
if command_search_status == 201:
break
except:
pass
print("Search command failed! Retrying in " + CONNECTION_RETRY_TIMEOUT + " seconds...")
connection_retries = connection_retries + 1
sleep(CONNECTION_RETRY_TIMEOUT)
if connection_retries > MAX_CONNECTION_RETRIES:
return False
command_search_id = json.loads(command_search_response.content)['id']
# Wait for the search to complete
if not rapid_mode:
completion_url = sonarr_url + "/api/command/" + str(command_search_id) + "?apikey=" + api_key
timeout_counter = 0
while True:
sleep(5)
timeout_counter = timeout_counter + 5
completion_status = 404
connection_retries = 0
while completion_status != 200:
try:
completion_response = requests.get(completion_url)
completion_status = completion_response.status_code
if completion_status == 200:
break
except:
pass
print("Completion check failed! Retrying in " + CONNECTION_RETRY_TIMEOUT + " seconds...")
connection_retries = connection_retries + 1
sleep(CONNECTION_RETRY_TIMEOUT)
if connection_retries > MAX_CONNECTION_RETRIES:
return False
if json.loads(completion_response.content)['state'] == "completed":
break
if timeout_counter > MAX_TIMEOUT:
print("Show is still processing after " + str(MAX_TIMEOUT) + " seconds. Starting the next show.")
break
return True
if __name__ == "__main__":
if content_redownloader():
print("Script successfully completed.")
else:
print("Script failed to complete.")