-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathapi.py
More file actions
210 lines (182 loc) · 7.41 KB
/
api.py
File metadata and controls
210 lines (182 loc) · 7.41 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
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
#!/usr/bin/env python3
"""
Sonarr API Helper Functions
Handles all communication with the Sonarr API
"""
import requests
import time
from typing import List, Dict, Any, Optional, Union
from utils.logger import logger, debug_log
from config import API_KEY, API_URL, API_TIMEOUT, COMMAND_WAIT_DELAY, COMMAND_WAIT_ATTEMPTS
# Create a session for reuse
session = requests.Session()
def sonarr_request(endpoint: str, method: str = "GET", data: Dict = None) -> Optional[Union[Dict, List]]:
"""
Make a request to the Sonarr API (v3).
`endpoint` should be something like 'series', 'command', 'wanted/cutoff', etc.
"""
url = f"{API_URL}/api/v3/{endpoint}"
headers = {
"X-Api-Key": API_KEY,
"Content-Type": "application/json"
}
try:
if method.upper() == "GET":
response = session.get(url, headers=headers, timeout=API_TIMEOUT)
elif method.upper() == "POST":
response = session.post(url, headers=headers, json=data, timeout=API_TIMEOUT)
else:
logger.error(f"Unsupported HTTP method: {method}")
return None
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
logger.error(f"API request error: {e}")
return None
def wait_for_command(command_id: int):
logger.debug(f"Waiting for command {command_id} to complete...")
attempts = 0
while True:
try:
time.sleep(COMMAND_WAIT_DELAY)
response = sonarr_request(f"command/{command_id}")
logger.debug(f"Command {command_id} Status: {response['status']}")
except Exception as error:
logger.error(f"Error fetching command status on attempt {attempts + 1}: {error}")
return False
attempts += 1
if response['status'].lower() in ['complete', 'completed'] or attempts >= COMMAND_WAIT_ATTEMPTS:
break
if response['status'].lower() not in ['complete', 'completed']:
logger.warning(f"Command {command_id} did not complete within the allowed attempts.")
return False
time.sleep(0.5)
return response['status'].lower() in ['complete', 'completed']
def get_series() -> List[Dict]:
"""Get all series from Sonarr."""
series_list = sonarr_request("series")
if series_list:
debug_log("Raw series API response sample:", series_list[:2] if len(series_list) > 2 else series_list)
return series_list or []
def refresh_series(series_id: int) -> Optional[Dict]:
"""
POST /api/v3/command
{
"name": "RefreshSeries",
"seriesId": <series_id>
}
"""
data = {
"name": "RefreshSeries",
"seriesId": series_id
}
response = sonarr_request("command", method="POST", data=data)
return wait_for_command(response['id'])
def episode_search_episodes(episode_ids: List[int]) -> Optional[Dict]:
"""
POST /api/v3/command
{
"name": "EpisodeSearch",
"episodeIds": [...]
}
"""
data = {
"name": "EpisodeSearch",
"episodeIds": episode_ids
}
response = sonarr_request("command", method="POST", data=data)
return wait_for_command(response['id'])
def get_download_queue_size() -> Optional[int]:
"""
GET /api/v3/queue
Returns total number of items in the queue with the status 'downloading'.
"""
response = sonarr_request("queue?status=downloading")
total_records = response.get("totalRecords", 0)
if not isinstance(total_records, int):
total_records = 0
logger.debug(f"Download Queue Size: {total_records}")
return total_records
def get_cutoff_unmet(page: int = 1) -> Optional[Dict]:
"""
GET /api/v3/wanted/cutoff?sortKey=airDateUtc&sortDirection=descending&includeSeriesInformation=true
&page=<page>&pageSize=200
Returns JSON with a "records" array and "totalRecords".
"""
endpoint = (
"wanted/cutoff?"
"sortKey=airDateUtc&sortDirection=descending&includeSeriesInformation=true"
f"&page={page}&pageSize=200"
)
return sonarr_request(endpoint, method="GET")
def get_cutoff_unmet_total_pages() -> int:
"""
To find total pages, call the endpoint with page=1&pageSize=1, read totalRecords,
then compute how many pages if each pageSize=200.
"""
response = sonarr_request("wanted/cutoff?page=1&pageSize=1")
if not response or "totalRecords" not in response:
return 0
total_records = response.get("totalRecords", 0)
if not isinstance(total_records, int) or total_records < 1:
return 0
# Each page has up to 200 episodes
total_pages = (total_records + 200 - 1) // 200
return max(total_pages, 1)
def get_episodes_for_series(series_id: int) -> Optional[List[Dict]]:
"""Get all episodes for a specific series"""
return sonarr_request(f"episode?seriesId={series_id}", method="GET")
def get_missing_episodes(pageSize: int = 1000) -> Optional[Dict]:
"""
GET /api/v3/wanted/missing?pageSize=<pageSize>&includeSeriesInformation=true
Returns JSON with a "records" array of missing episodes and "totalRecords".
"""
endpoint = f"wanted/missing?pageSize={pageSize}&includeSeriesInformation=true"
return sonarr_request(endpoint, method="GET")
def get_series_with_missing_episodes() -> List[Dict]:
"""
Fetch all shows that have missing episodes using the wanted/missing endpoint.
Returns a list of series objects with an additional 'missingEpisodes' field
containing the list of missing episodes for that series.
"""
missing_data = get_missing_episodes()
if not missing_data or "records" not in missing_data:
return []
# Group missing episodes by series ID
series_with_missing = {}
for episode in missing_data.get("records", []):
series_id = episode.get("seriesId")
series_title = None
# Try to get series info from the episode record
if "series" in episode and isinstance(episode["series"], dict):
series_info = episode["series"]
series_title = series_info.get("title")
if series_id not in series_with_missing:
# Initialize the series entry if it doesn't exist
if series_title:
# We have the series info from the episode
series_with_missing[series_id] = {
"id": series_id,
"title": series_title,
"monitored": series_info.get("monitored", False),
"missingEpisodes": [episode]
}
else:
# We need to fetch the series info
series_info = sonarr_request(f"series/{series_id}", method="GET")
if series_info:
series_with_missing[series_id] = {
"id": series_id,
"title": series_info.get("title", "Unknown Show"),
"monitored": series_info.get("monitored", False),
"missingEpisodes": [episode]
}
else:
# Add the episode to the existing series entry
series_with_missing[series_id]["missingEpisodes"].append(episode)
# Convert to list and add count for convenience
result = []
for series_id, series_data in series_with_missing.items():
series_data["missingEpisodeCount"] = len(series_data["missingEpisodes"])
result.append(series_data)
return result