/
plex_watcher.py
executable file
·111 lines (89 loc) · 2.56 KB
/
plex_watcher.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
import argparse
import json
import logging
import os
import ssl
import sys
import urllib2
PATH = os.path.dirname(os.path.realpath(__file__))
xmltodict_path = os.path.join(PATH, 'xmltodict')
sys.path.append(xmltodict_path)
import xmltodict
def GetStatus(ip, port):
status_xml = ''
url = 'https://%s:%s/status/sessions' % (ip, port)
context = ssl._create_unverified_context()
request = urllib2.urlopen(url, context=context)
response = request.read()
return xmltodict.parse(response, dict_constructor=dict)['MediaContainer']
def ParseItem(data):
if '@grandparentTitle' in data:
artist = '%s - ' % data['@grandparentTitle']
else:
artist = ''
try:
title = artist + data['@title']
user = data['User']['@title']
player = data['Player']['@title']
except:
user = ''
title = ''
player = ''
return '%s is accessing %s on %s' % (user, title, player)
def ParseData(data):
stats = []
for k in ['Video', 'Track']:
if k in data:
v = data[k]
if type(v) == list:
for i in v:
stats.append(ParseItem(i))
else:
stats.append(ParseItem(v))
return stats
def ReadStats(stats_file):
try:
with open(stats_file) as f:
stats = json.load(f)
except:
stats = []
return stats
def WriteStats(stats, stats_file):
with open(stats_file, 'w') as f:
json.dump(stats, f)
def UpdateStats(old_stats, cur_stats):
new_stats = []
for i in cur_stats:
if i not in old_stats:
new_stats.append(i)
return new_stats
def Main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--ip', default='10.0.0.2',
help='the ip of the plex server')
parser.add_argument('-p', '--port', default=32400,
help='the port plex is running on')
args = parser.parse_args()
logFormatter = logging.Formatter('%(asctime)s %(message)s',
datefmt='%m-%d %H:%M:%S')
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler('/var/log/plex_watcher.log')
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
try:
data = GetStatus(args.ip, args.port)
except urllib2.URLError:
# Plex is down.
return
old_stats = ReadStats('%s/stats.json' % PATH)
cur_stats = ParseData(data)
new_stats = UpdateStats(old_stats, cur_stats)
if cur_stats != old_stats:
WriteStats(cur_stats, '%s/stats.json' % PATH)
if len(new_stats) != 0:
for line in new_stats:
logging.info(line)
if __name__ == "__main__":
Main()