forked from iBaa/PlexConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlexGDM.py
executable file
·121 lines (89 loc) · 3.47 KB
/
PlexGDM.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
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
"""
loosely based on hippojay's plexGDM:
https://github.com/hippojay/plugin.video.plexbmc
"""
import sys
import struct
import httplib, socket
from Debug import * # dprint()
PMS_list = []
IP_PlexGDM = '<broadcast>'
Port_PlexGDM = 32414
Msg_PlexGDM = 'M-SEARCH * HTTP/1.0'
def getIP_PMS():
# todo: currently only one server - return first entry
if len(PMS_list)>0:
return PMS_list[0]['server']
else:
return '127.0.0.1'
def getPort_PMS():
# todo: currently only one server - return first entry
if len(PMS_list)>0:
return PMS_list[0]['port']
else:
return '32400'
def Run():
dprint(__name__, 0, "***")
dprint(__name__, 0, "looking up Plex Media Server")
dprint(__name__, 0, "***")
# setup socket for discovery -> multicast message
GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
GDM.settimeout(1.0)
# Set the time-to-live for messages to 1 for local network
GDM.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
returnData = []
try:
# Send data to the multicast group
dprint(__name__, 1, "Sending discovery message: {0}", Msg_PlexGDM)
GDM.sendto(Msg_PlexGDM, (IP_PlexGDM, Port_PlexGDM))
# Look for responses from all recipients
while True:
try:
data, server = GDM.recvfrom(1024)
dprint(__name__, 1, "Received data from {0}", server)
dprint(__name__, 1, "Data received:\n {0}", data)
returnData.append( { 'from' : server,
'data' : data } )
except socket.timeout:
break
finally:
GDM.close()
discovery_complete = True
global PMS_list
PMS_list = []
if returnData:
for response in returnData:
update = { 'server' : response.get('from')[0] }
# Check if we had a positive HTTP response
if "200 OK" in response.get('data'):
for each in response.get('data').split('\n'):
# decode response data
update['discovery'] = "auto"
update['owned']='1'
update['master']= 1
update['role']='master'
if "Content-Type:" in each:
update['content-type'] = each.split(':')[1].strip()
elif "Resource-Identifier:" in each:
update['uuid'] = each.split(':')[1].strip()
elif "Name:" in each:
update['serverName'] = each.split(':')[1].strip()
elif "Port:" in each:
update['port'] = each.split(':')[1].strip()
elif "Updated-At:" in each:
update['updated'] = each.split(':')[1].strip()
elif "Version:" in each:
update['version'] = each.split(':')[1].strip()
PMS_list.append(update)
if PMS_list==[]:
dprint(__name__, 0, "No servers discovered")
else:
dprint(__name__, 0, "servers discovered: {0}", len(PMS_list))
for items in PMS_list:
dprint(__name__, 1, "{0} {1}:{2}", items['serverName'], items['server'], items['port'])
return len(PMS_list)
if __name__ == '__main__':
Run()
print "IP:", getIP_PMS()
print "Port:", getPort_PMS()