-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsonarr_utils.py
executable file
·101 lines (82 loc) · 4.16 KB
/
sonarr_utils.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
import os
import requests
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Configuration settings from environment variables
SONARR_URL = os.getenv('SONARR_URL')
SONARR_API_KEY = os.getenv('SONARR_API_KEY')
def load_preferences():
"""
Load preferences for Sonarr configuration.
Returns a dictionary containing Sonarr URL and API key.
"""
return {'SONARR_URL': SONARR_URL, 'SONARR_API_KEY': SONARR_API_KEY}
def get_series_list(preferences):
url = f"{preferences['SONARR_URL']}/api/v3/series"
headers = {'X-Api-Key': preferences['SONARR_API_KEY']}
response = requests.get(url, headers=headers)
if response.ok:
series_list = response.json()
# Sort the series list alphabetically by title
sorted_series_list = sorted(series_list, key=lambda x: x['title'].lower())
return sorted_series_list
else:
return []
def fetch_episode_file_details(episode_file_id):
episode_file_url = f"{SONARR_URL}/api/v3/episodefile/{episode_file_id}"
headers = {'X-Api-Key': SONARR_API_KEY}
response = requests.get(episode_file_url, headers=headers)
return response.json() if response.ok else None
def fetch_series_and_episodes(preferences):
SONARR_URL = preferences['SONARR_URL']
SONARR_API_KEY = preferences['SONARR_API_KEY']
series_url = f"{SONARR_URL}/api/v3/series"
headers = {'X-Api-Key': SONARR_API_KEY}
active_series = []
series_response = requests.get(series_url, headers=headers)
series_list = series_response.json() if series_response.ok else []
for series in series_list:
episodes_url = f"{SONARR_URL}/api/v3/episode"
params = {'seriesId': series['id']}
episodes_response = requests.get(episodes_url, headers=headers, params=params)
episodes = episodes_response.json() if episodes_response.ok else []
for episode in episodes:
if episode.get('monitored') and episode.get('hasFile'):
episode_file_details = fetch_episode_file_details(episode['episodeFileId'])
if episode_file_details and 'dateAdded' in episode_file_details:
# Parse and make dateAdded offset-aware
date_added = datetime.fromisoformat(episode_file_details['dateAdded'].replace('Z', '+00:00'))
active_series.append({
'name': series['title'],
'latest_monitored_episode': f"S{episode['seasonNumber']}E{episode['episodeNumber']} - {episode['title']}",
'artwork_url': f"{SONARR_URL}/api/v3/mediacover/{series['id']}/poster.jpg?apikey={SONARR_API_KEY}",
'sonarr_series_url': f"{SONARR_URL}/series/{series['titleSlug']}",
'dateAdded': date_added
})
break # Since we're only interested in the latest episode per series that meets the criteria
# Sort series and return only the top
active_series.sort(key=lambda series: series['dateAdded'], reverse=True)
return active_series[:12]
def fetch_upcoming_premieres(preferences):
SONARR_URL = preferences['SONARR_URL']
SONARR_API_KEY = preferences['SONARR_API_KEY']
series_url = f"{SONARR_URL}/api/v3/series"
headers = {'X-Api-Key': SONARR_API_KEY}
upcoming_premieres = []
series_response = requests.get(series_url, headers=headers)
if series_response.ok:
series_list = series_response.json()
for series in series_list:
if 'nextAiring' in series:
next_airing_dt = datetime.fromisoformat(series['nextAiring'].replace('Z', '+00:00'))
formatted_date = next_airing_dt.strftime('%Y-%m-%d at %H:%M')
upcoming_premieres.append({
'name': series['title'],
'nextAiring': formatted_date,
'artwork_url': f"{SONARR_URL}/api/v3/mediacover/{series['id']}/poster.jpg?apikey={SONARR_API_KEY}",
'sonarr_series_url': f"{SONARR_URL}/series/{series['titleSlug']}"
})
upcoming_premieres.sort(key=lambda x: x['nextAiring'])
return upcoming_premieres