-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendit.py
78 lines (62 loc) · 2.67 KB
/
sendit.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
import requests
import time
# Replace these values with your Jellyfin server details
JELLYFIN_SERVER_URL = "http://192.168.1.XXX:8096"
JELLYFIN_API_KEY = "YOUR_API_KEY_GOES_HERE"
# Function to get session IDs
def get_session_ids():
headers = {"Authorization": f"MediaBrowser Token={JELLYFIN_API_KEY}"}
params = {"activeWithinSeconds": 60} # Adjust the time as needed
response = requests.get(f"{JELLYFIN_SERVER_URL}/Sessions", headers=headers, params=params)
if response.status_code == 200:
sessions = response.json()
session_ids = [session["Id"] for session in sessions]
return session_ids
else:
print(f"Failed to get session IDs. Status code: {response.status_code}")
return []
# Function to send a message to a specific session
def send_message(session_id, header, text, timeout_ms=10000):
headers = {"Authorization": f"MediaBrowser Token={JELLYFIN_API_KEY}"}
url = f"{JELLYFIN_SERVER_URL}/Sessions/{session_id}/Message"
message_data = {
"Header": header,
"Text": text,
"TimeoutMs": timeout_ms
}
response = requests.post(url, headers=headers, json=message_data)
if response.status_code == 204:
print(f"Message sent to session {session_id}")
else:
print(f"Failed to send message to session {session_id}. Status code: {response.status_code}")
# Function to send maintenance countdown messages
def send_maintenance_countdown(initial_countdown=60):
header = "Maintenance Alert"
# Announce initial countdown
text = f"Server maintenance will start in {initial_countdown} minutes"
send_message_to_all(header, text)
time.sleep(600) # Wait for 10 minutes
# Announce remaining countdown
for minutes in range(initial_countdown - 10, 0, -10):
text = f"Server maintenance will start in {minutes} minute{'s' if minutes > 1 else ''}"
send_message_to_all(header, text)
if minutes > 5:
time.sleep(600) # Wait for 10 minutes
else:
time.sleep(60 * minutes) # Wait for remaining minutes
# Function to send a message to all online users
def send_message_to_all(header, text):
session_ids = get_session_ids()
if session_ids:
for session_id in session_ids:
send_message(session_id, header, text)
# Main script
def main():
# Example: Send immediate messages
header_immediate = "Immediate Alert"
text_immediate = "This is an immediate alert!"
send_message_to_all(header_immediate, text_immediate)
# Example: Send maintenance countdown messages with initial countdown of 60 minutes
send_maintenance_countdown(initial_countdown=60)
if __name__ == "__main__":
main()