Files
synology-webhook/synology-webhook.py
2024-04-15 21:35:19 +02:00

43 lines
1.3 KiB
Python

import socketserver
import http.server
import json
import re
try:
config_file = open('config.json', 'r')
except FileNotFoundError:
print('Config file not found')
exit()
config = json.load(config_file)
def do_shutdown():
import subprocess
import shlex
command = shlex.split('sudo shutdown -h now')
subprocess.call(command)
class MyHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
if self.path == '/shutdown':
match = False
for keyword in config['shutdown_keywords']:
if re.search(keyword, data['message']):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Shutting down...')
print('Shutting down...')
do_shutdown()
match = True
return
if not match:
self.send_response(200)
self.end_headers()
self.wfile.write(b'Not shutting down')
print('Not shutting down')
httpd = socketserver.TCPServer(('', 48080), MyHandler)
httpd.serve_forever()