jellyfin-wol/jellyfin_wol.py
2025-06-09 22:20:18 +02:00

31 lines
1.1 KiB
Python
Executable file

#!/usr/bin/python3
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs
from wakeonlan import send_magic_packet
import os
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
raw_data = self.rfile.read(content_length).decode('utf-8')
post_data = parse_qs(raw_data)
# Check if the payload indicates a user login event
if 'NotificationType' in post_data and 'AuthenticationSuccess' in post_data['NotificationType']:
print("User login detected! Waking up NAS.")
send_magic_packet('1c:1b:0d:99:5c:48')
# Send a 200 OK response
self.send_response(200)
self.end_headers()
self.wfile.write(b'{"status": "success"}')
# Configure and start the HTTP server
if __name__ == '__main__':
host = os.getenv('LISTEN_ADDRESS', '0.0.0.0')
port = int(os.getenv('LISTEN_PORT', 8092))
httpd = HTTPServer((host, port), SimpleHTTPRequestHandler)
print(f"Listening on {host}:{port}...")
httpd.serve_forever()