· 5 years ago · Dec 09, 2020, 09:40 PM
1#!/usr/bin/env python3
2
3import urllib.request
4import os
5import subprocess
6
7#
8# Install python3 via Package Manager
9#
10# Adjust python path above accordingly but should work as is with syno
11#
12# Add a cron to run it when desired (Below is every 30 minutes)
13# User who runs the cron needs sudo ability
14#
15# sudo -i
16# vi /etc/crontab
17# 0,30 * * * * root /volume1/data/discordnotifier_plexSessions_synology.py
18#
19
20settings = {
21 'api':'', # discordnotifier.com api key
22 'account_map':'', # for shared plex servers, you can map an email to a name.. someemail,Name|someotheremail|name
23 'server':'plex', # name of the server the notifications are coming from (not required)
24 'plex_host':'localhost', # hostname of your plex machine
25 'plex_port':'32400', # port of your plex machine
26 'plex_token':'', # https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
27}
28
29postData = session = top = ''
30
31# grab cpu/ram data
32process = subprocess.Popen('top -n 1 -b | awk \'/^%Cpu/{print $2}\' && top -n 1 -b | awk \'/used/ && /buff/ {print $8}\'', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33for line in process.stdout:
34 top += line.decode();
35
36# grab plex session data
37url = 'http://'+ settings['plex_host'] +':'+ settings['plex_port'] +'/status/sessions'
38req = urllib.request.Request(url, headers={'Content-Type':'application/json','X-Plex-Token':settings['plex_token']})
39f = urllib.request.urlopen(req)
40session = f.read()
41f.close()
42
43# push out the notification
44postData = urllib.parse.urlencode({'api': settings['api'], 'plexPython': 'sessions', 'session': session, 'top': top, 'map': settings['account_map']})
45url = "https://discordnotifier.com/notifierTest.php"
46req = urllib.request.Request(url, postData.encode('ascii'))
47f = urllib.request.urlopen(req)
48response = f.read()
49f.close()