-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotification_storage.py
More file actions
154 lines (113 loc) · 4.71 KB
/
notification_storage.py
File metadata and controls
154 lines (113 loc) · 4.71 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
"""
Notification storage helpers
Manages pending search notifications in a simple JSON file
"""
import os
import json
import logging
from datetime import datetime, timedelta
from logging_config import main_logger as logger
NOTIFICATION_STORAGE = '/config/pending_notifications.json'
AIRED_NOTIFICATION_STORAGE = '/data/aired_notifications.json'
def store_notification(episode_id, message_id):
"""
Store Discord message ID for an episode search
Args:
episode_id: Sonarr episode ID
message_id: Discord message ID
"""
try:
notifications = {}
if os.path.exists(NOTIFICATION_STORAGE):
with open(NOTIFICATION_STORAGE, 'r') as f:
notifications = json.load(f)
notifications[str(episode_id)] = {
'message_id': message_id,
'timestamp': datetime.utcnow().isoformat()
}
os.makedirs(os.path.dirname(NOTIFICATION_STORAGE), exist_ok=True)
with open(NOTIFICATION_STORAGE, 'w') as f:
json.dump(notifications, f, indent=2)
logger.info(f"💾 Stored notification for episode {episode_id}: {message_id}")
except Exception as e:
logger.error(f"Failed to store notification: {e}")
def get_and_remove_notification(episode_id):
"""
Get and remove notification for an episode
Args:
episode_id: Sonarr episode ID
Returns:
Discord message ID if found, None otherwise
"""
try:
if not os.path.exists(NOTIFICATION_STORAGE):
return None
with open(NOTIFICATION_STORAGE, 'r') as f:
notifications = json.load(f)
notification = notifications.pop(str(episode_id), None)
with open(NOTIFICATION_STORAGE, 'w') as f:
json.dump(notifications, f, indent=2)
if notification:
message_id = notification.get('message_id')
logger.info(f"📋 Retrieved notification for episode {episode_id}: {message_id}")
return message_id
return None
except Exception as e:
logger.error(f"Failed to get notification: {e}")
return None
def notification_exists(episode_id):
"""Check if a notification already exists for an episode"""
try:
if not os.path.exists(NOTIFICATION_STORAGE):
return False
with open(NOTIFICATION_STORAGE, 'r') as f:
notifications = json.load(f)
return str(episode_id) in notifications
except Exception as e:
logger.error(f"Failed to check notification existence: {e}")
return False
# --- Aired-but-not-downloaded notification tracking ---
def aired_notification_exists(episode_id):
"""Check if an aired-not-downloaded notification has already been sent for an episode"""
try:
if not os.path.exists(AIRED_NOTIFICATION_STORAGE):
return False
with open(AIRED_NOTIFICATION_STORAGE, 'r') as f:
notified = json.load(f)
return str(episode_id) in notified
except Exception as e:
logger.error(f"Failed to check aired notification existence: {e}")
return False
def store_aired_notification(episode_id):
"""Record that an aired-not-downloaded notification was sent for an episode"""
try:
notified = {}
if os.path.exists(AIRED_NOTIFICATION_STORAGE):
with open(AIRED_NOTIFICATION_STORAGE, 'r') as f:
notified = json.load(f)
notified[str(episode_id)] = datetime.utcnow().isoformat()
os.makedirs(os.path.dirname(AIRED_NOTIFICATION_STORAGE), exist_ok=True)
with open(AIRED_NOTIFICATION_STORAGE, 'w') as f:
json.dump(notified, f, indent=2)
logger.debug(f"Stored aired notification for episode {episode_id}")
except Exception as e:
logger.error(f"Failed to store aired notification: {e}")
def cleanup_old_aired_notifications():
"""Remove entries older than 30 days from the aired notifications file"""
try:
if not os.path.exists(AIRED_NOTIFICATION_STORAGE):
return
with open(AIRED_NOTIFICATION_STORAGE, 'r') as f:
notified = json.load(f)
cutoff = datetime.utcnow() - timedelta(days=30)
pruned = {
ep_id: ts for ep_id, ts in notified.items()
if datetime.fromisoformat(ts) > cutoff
}
removed = len(notified) - len(pruned)
if removed:
with open(AIRED_NOTIFICATION_STORAGE, 'w') as f:
json.dump(pruned, f, indent=2)
logger.info(f"Cleaned up {removed} old aired notification entries")
except Exception as e:
logger.error(f"Failed to cleanup aired notifications: {e}")