· 5 years ago · Dec 12, 2020, 03:48 PM
1#!/usr/bin/env python3
2
3import socket
4import sys
5import urllib.request
6import os
7import subprocess
8import time
9from datetime import datetime
10
11#
12# Install python3 via Package Manager
13#
14# Adjust python path above accordingly but should work as is with syno
15#
16# run in the background on linux: https://askubuntu.com/questions/396654/how-to-run-a-python-program-in-the-background-even-after-closing-the-terminal
17# run in the background on windows: https://stackoverflow.com/questions/32808730/running-python-script-as-a-windows-background-process
18
19settings = {
20 'api':'', # discordnotifier.com api key
21 'account_map':'', # for shared plex servers, you can map an email to a name.. someemail,Name|someotheremail|name
22 'plex_host':'localhost', # hostname to your plex machine
23 'plex_port':'32400', # port to your plex machine
24 'plex_token':'', # https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
25 'listen_host':'localhost', # should be same host as plex_host above
26 'listen_port':32402, # port to listen to, needs to match the plex webhook & not already be in use
27}
28
29postData = session = top = ''
30counter = 0
31
32sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
33server_address = (settings['listen_host'], settings['listen_port'])
34print('listening on %s:%s' % server_address, file=sys.stderr)
35sock.bind(server_address)
36sock.listen(1)
37
38while True:
39 payload = elements = ''
40 connection, client_address = sock.accept()
41 try:
42 while True:
43 connection.settimeout(3)
44 data = connection.recv(4096)
45 payload += str(data.decode('utf-8', errors='replace').rstrip())
46 except socket.timeout:
47 if len(payload):
48 # trim the message from plex
49 pos = payload.find('{"event')
50 payload = payload[pos:len(payload)]
51
52 # grab cpu/ram data
53 process = subprocess.Popen('top -n 1 -b | awk \'/^%Cpu/{print $2}\' && top -n 1 -b | awk \'/used/ && /buff/ {print $8}{print $1}\'', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
54 for line in process.stdout:
55 top += line.decode();
56
57 # grab plex session data
58 time.sleep(10) # plex sessions take time to generate, the wait time is necessary
59 session = urllib.request.Request('http://'+ settings['plex_host'] +':'+ settings['plex_port'] +'/status/sessions', headers={'Content-Type':'application/json','X-Plex-Token':settings['plex_token']})
60
61 # push out the notification
62 postData = urllib.parse.urlencode({'api':settings['api'], 'session': session, 'cpu': cpu, 'ramAvailable': ramBytes, 'ramTotal': ramTotal, 'top': top, 'plexPython': payload, 'map': settings['account_map']})
63 req = urllib.request.Request('https://discordnotifier.com/notifier.php', data=postData.encode('ascii'), headers={'content-type':'application/x-www-form-urlencoded'})
64
65 # log it in the console
66 counter += 1
67 now = datetime.now()
68 elements = (counter, now.strftime("%H:%M:%S"))
69 print('notification #%s pushed at %s :)' % elements, file=sys.stderr)
70 finally:
71 connection.close()
72