-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotifications.py
More file actions
310 lines (252 loc) · 9.9 KB
/
Copy pathnotifications.py
File metadata and controls
310 lines (252 loc) · 9.9 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
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
"""
Episeerr Notification System
Handles Discord notifications for pending searches and selection requests
"""
import requests
import logging
from sonarr_utils import get_episode
from datetime import datetime
from logging_config import main_logger as logger
# Config will be passed in from episeerr.py
NOTIFICATIONS_ENABLED = False
DISCORD_WEBHOOK_URL = ''
EPISEERR_URL = 'http://localhost:5002'
SONARR_URL = 'http://localhost:8989'
def init_notifications(notifications_enabled, discord_webhook_url, episeerr_url, sonarr_url):
"""Initialize notification config - called from episeerr.py on startup"""
global NOTIFICATIONS_ENABLED, DISCORD_WEBHOOK_URL, EPISEERR_URL, SONARR_URL
NOTIFICATIONS_ENABLED = notifications_enabled
DISCORD_WEBHOOK_URL = discord_webhook_url
EPISEERR_URL = episeerr_url
SONARR_URL = sonarr_url
def send_notification(notification_type, **data):
"""
Central notification dispatcher
Args:
notification_type: Type of notification to send
**data: Context data for the notification
Supported types:
- episode_search_pending: Search requested for episode
- selection_pending: New episeerr_select request
Returns:
Discord message ID if sent successfully, None otherwise
"""
if not NOTIFICATIONS_ENABLED:
logger.debug(f"Notifications disabled, skipping {notification_type}")
return None
if not DISCORD_WEBHOOK_URL:
logger.warning("Discord webhook URL not configured")
return None
try:
# Build message based on type
if notification_type == "episode_search_pending":
message = build_search_pending_message(
series=data['series'],
season=data['season'],
episode=data['episode'],
air_date=data.get('air_date'),
series_id=data.get('series_id')
)
elif notification_type == "selection_pending":
message = build_selection_pending_message(
series=data['series'],
series_id=data.get('series_id')
)
elif notification_type == "aired_not_downloaded":
message = build_aired_not_downloaded_message(
episodes=data['episodes']
)
else:
logger.warning(f"Unknown notification type: {notification_type}")
return None
# Send notification and get message ID
message_id = send_discord_webhook(message)
logger.info(f"Sent {notification_type} notification, message_id: {message_id}")
return message_id
except Exception as e:
logger.error(f"Failed to send notification: {e}")
return None
def build_search_pending_message(series, season, episode, air_date=None, series_id=None):
"""Build Discord embed for pending episode search"""
# Format air date
air_date_str = "Unknown"
if air_date:
try:
dt = datetime.fromisoformat(air_date.replace('Z', '+00:00'))
air_date_str = dt.strftime("%B %d, %Y")
except:
air_date_str = str(air_date)
# Build Sonarr link
sonarr_link = ""
if series_id and SONARR_URL:
series_slug = series.lower().replace(' ', '-').replace("'", "")
sonarr_link = f"{SONARR_URL}/series/{series_slug}/season-{season}"
fields = [
{
"name": "Episode",
"value": f"{series} S{season:02d}E{episode:02d}",
"inline": False
},
{
"name": "Air Date",
"value": air_date_str,
"inline": True
},
{
"name": "ℹ️ Status",
"value": "Waiting for Sonarr to find and grab this episode. This message will disappear if found.",
"inline": False
}
]
if sonarr_link:
fields.append({
"name": "🔍 Manual Search",
"value": f"[Open in Sonarr]({sonarr_link})",
"inline": False
})
return {
"embeds": [{
"title": "🔍 Episode Search Pending",
"description": "Sonarr is searching for this episode",
"color": 3447003, # Blue - informational
"fields": fields,
"timestamp": datetime.utcnow().isoformat()
}]
}
def build_selection_pending_message(series, series_id=None):
"""Build Discord embed for pending episode selection"""
episeerr_link = ""
if EPISEERR_URL:
episeerr_link = f"{EPISEERR_URL}/episeerr"
fields = [
{
"name": "Series",
"value": series,
"inline": False
}
]
if episeerr_link:
fields.append({
"name": "🔗 Select Episodes",
"value": f"[Open Episeerr]({episeerr_link})",
"inline": False
})
return {
"embeds": [{
"title": "📋 New Episode Selection Request",
"description": "A show is waiting for episode selection",
"color": 3447003, # Blue
"fields": fields,
"timestamp": datetime.utcnow().isoformat()
}]
}
def build_aired_not_downloaded_message(episodes):
"""Build a single batched Discord embed for all aired-but-not-downloaded episodes."""
from sonarr_utils import get_episode # add this import at top of file if not already there
by_series = {}
for ep in episodes:
# Try different possible locations for series title
series_title = (
ep.get('seriesTitle') or
ep.get('series', {}).get('title') or
'Unknown Series'
)
# If still unknown → fetch full episode from Sonarr (most reliable fix)
if series_title == 'Unknown Series' and 'id' in ep:
full_ep = get_episode(ep['id'])
if full_ep and 'series' in full_ep:
series_title = full_ep['series'].get('title', 'Unknown Series (fetched)')
by_series.setdefault(series_title, []).append(ep)
fields = []
for series_title in sorted(by_series):
lines = []
for ep in sorted(by_series[series_title], key=lambda e: (e.get('seasonNumber', 0), e.get('episodeNumber', 0))):
season = ep.get('seasonNumber', 0)
episode = ep.get('episodeNumber', 0)
ep_title = ep.get('title') or 'Unknown'
air_date_raw = ep.get('airDate') or ep.get('airDateUtc', '')
try:
if 'T' in air_date_raw:
dt = datetime.fromisoformat(air_date_raw.replace('Z', '+00:00'))
air_date_str = dt.strftime('%b %d, %Y')
else:
air_date_str = air_date_raw
except Exception:
air_date_str = air_date_raw or 'Unknown'
lines.append(f"S{season:02d}E{episode:02d} — {ep_title} *(aired {air_date_str})*")
field_value = '\n'.join(lines)
if len(field_value) > 1020:
field_value = field_value[:1020] + '…'
fields.append({
'name': series_title,
'value': field_value,
'inline': False
})
# Discord allows max 25 fields per embed
if len(fields) > 25:
truncated = len(fields) - 24
fields = fields[:24]
fields.append({
'name': f'… and {truncated} more series',
'value': 'Check Sonarr for the full list.',
'inline': False
})
if EPISEERR_URL:
fields.append({
'name': '🔗 Manage',
'value': f'[Open Episeerr]({EPISEERR_URL}/episeerr)',
'inline': False
})
total = sum(len(v) for v in by_series.values())
description = (
f"**{total}** episode{'s' if total != 1 else ''} have aired but not yet been downloaded.\n"
"Sonarr may still be searching — this is a heads-up for continuing series."
)
return {
'embeds': [{
'title': '📺 Aired but Not Downloaded',
'description': description,
'color': 15105570, # Orange — warning
'fields': fields,
'timestamp': datetime.utcnow().isoformat()
}]
}
def send_discord_webhook(message):
"""Send notification to Discord webhook and return message ID"""
if not DISCORD_WEBHOOK_URL:
return None
try:
# Add ?wait=true to get the message back
url = DISCORD_WEBHOOK_URL
if '?' not in url:
url += '?wait=true'
else:
url += '&wait=true'
response = requests.post(url, json=message, timeout=10)
response.raise_for_status()
# Get message ID from response
message_data = response.json()
message_id = message_data.get('id')
logger.info(f"📤 Sent Discord message ID: {message_id}")
return message_id
except requests.exceptions.RequestException as e:
logger.error(f"Discord webhook failed: {e}")
return None
def delete_discord_message(message_id):
"""Delete a Discord webhook message"""
if not DISCORD_WEBHOOK_URL or not message_id:
return False
try:
# Extract webhook ID and token from URL
# Format: https://discord.com/api/webhooks/{webhook_id}/{webhook_token}
parts = DISCORD_WEBHOOK_URL.rstrip('/').split('/')
webhook_id = parts[-2]
webhook_token = parts[-1].split('?')[0] # Remove query params if present
delete_url = f"https://discord.com/api/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}"
response = requests.delete(delete_url)
response.raise_for_status()
logger.info(f"🗑️ Deleted Discord message {message_id}")
return True
except Exception as e:
logger.error(f"Failed to delete Discord message: {e}")
return False