This repository was archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbn_webhook_service.py
More file actions
115 lines (100 loc) · 3.62 KB
/
tbn_webhook_service.py
File metadata and controls
115 lines (100 loc) · 3.62 KB
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
112
113
114
115
import web
import time
import os
import sqlite3
import getpass
import urllib3
urls = ('/.*', 'hooks')
app = web.application(urls, globals())
DEFAULTDIR = homedir
MYDB = DEFAULTDIR + "myplex.db"
sql = sqlite3.connect(MYDB, check_same_thread=False)
cur = sql.cursor()
http = urllib3.PoolManager()
def getclient(data):
client = data
client = client.split("\"Player\":")
client = client[1]
client = client.split("},")
client = client[0]
client = client.split("title\":\"")
client = client[1]
client = client.split("\"")
client = client[0]
return (client)
def getaction(data):
action = data
action = action.split("event\":\"")
action = action[1]
action = action.split("\"")
action = action[0]
return action
class hooks:
def POST(self):
cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'')
PLEXCLIENT = cur.fetchone()
PLEXCLIENT = PLEXCLIENT[0]
cur.execute("SELECT State FROM States WHERE Option LIKE \"WEBHOOKSTATUS\"")
WSTATUS = cur.fetchone()[0]
data = web.data()
#print (data)
client = getclient(data)
action = getaction(data)
#print WSTATUS
if WSTATUS == "ON":
if (("media.pause" in action) and (PLEXCLIENT == client)):
#PAUSED ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
#elif (("media.resume" in action) and (PLEXCLIENT == client)):
#RESUME ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
# pass
elif (("media.stop" in action) and (PLEXCLIENT == client)):
#stopped ACTIONS GO BETWEEN HERE and the time.sleep()...
time.sleep(2)
cmd1 = "python " + homedir + "system.py startnextprogram"
os.system(cmd1)
#WSTATUS = "ON"
#print ("Stopped")
#WSTATUS = "Pending"
#command = "python " + homedir + "system.py startnextprogram"
#print ("Starting Next Program")
#os.system(command)
elif (("media.play" in action) and (PLEXCLIENT == client)):
#PLAY ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
elif WSTATUS == "PENDING":
if (("media.pause" in action) and (PLEXCLIENT == client)):
#PAUSED ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
elif (("media.resume" in action) and (PLEXCLIENT == client)):
#RESUME ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
# elif (("media.stop" in action) and (PLEXCLIENT == client)):
# time.sleep(2)
# cmd1 = "python " + homedir + "system.py startnextprogram"
# os.system(cmd1)
# print ("Duplicate Request, Dropping.")
elif (("media.play" in action) and (PLEXCLIENT == client)):
#print ("Media Playing, Resetting Status.")
time.sleep(5)
WSTATUS = "ON"
cmd1 = "python " + homedir + "system.py setwebhookstatus on"
os.system(cmd1)
elif WSTATUS == "SLEEP":
if (("media.pause" in action) and (PLEXCLIENT == client)):
#PAUSED ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
elif (("media.resume" in action) and (PLEXCLIENT == client)):
#RESUME ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
elif (("media.stop" in action) and (PLEXCLIENT == client)):
cmd1 = "python /home/pi/hasystem/system.py setwebhookstatus off"
os.system(cmd1)
#stopped ACTIONS GO BETWEEN HERE
#pass
elif (("media.play" in action) and (PLEXCLIENT == client)):
#PLAY ACTIONS GO BELOW HERE... COMMMENT OUT THE pass TO USE
pass
return 'OK'
if __name__ == '__main__':
app.run()