forked from megawubs/pyplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyplex.py
257 lines (204 loc) · 7.02 KB
/
pyplex.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import urllib2, re, xml.etree.cElementTree as et
from pyomxplayer import OMXPlayer
from urlparse import urlparse
import avahi, dbus, sys, platform
from pprint import pprint
import socket, subprocess, signal, os, logging
from threading import Thread
import Queue
import udplistener
import httplistener
from plexInterface import PlexInterface
__all__ = ["ZeroconfService"]
class ZeroconfService:
"""A simple class to publish a network service with zeroconf using
avahi.
"""
def __init__(self, name, port, stype="_plexclient._tcp",
domain="", host="", text=""):
self.name = name
self.stype = stype
self.domain = domain
self.host = host
self.port = port
self.text = text
def publish(self):
bus = dbus.SystemBus()
server = dbus.Interface(
bus.get_object(
avahi.DBUS_NAME,
avahi.DBUS_PATH_SERVER),
avahi.DBUS_INTERFACE_SERVER)
g = dbus.Interface(
bus.get_object(avahi.DBUS_NAME,
server.EntryGroupNew()),
avahi.DBUS_INTERFACE_ENTRY_GROUP)
g.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC,dbus.UInt32(0),
self.name, self.stype, self.domain, self.host,
dbus.UInt16(self.port), self.text)
g.Commit()
self.group = g
print 'Service published'
def unpublish(self):
self.group.Reset()
urls = (
'/xbmcCmds/xbmcHttp','xbmcCmdsXbmcHttp',
'/(.*)', 'stop', 'hello'
)
def string_to_lines(string):
return string.strip().replace('\r\n', '\n').split('\n')
class hello:
def GET(self, message):
return 'Hello, World'
class CGCommands:
def SendCmd (self, command, attributes):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(1)
try:
sock.connect(("localhost", 9010))
except socket.error, msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(2)
sock.send("%s|%s\r\n" % (command , attributes))
def SendCommand(self, message = None):
self.SendCmd("K",message)
def SendPlay(self, url = None):
self.SendCmd("V",url)
class xbmcCommands:
def __init__(self, omxArgs):
self.media = None
self.plex = PlexInterface()
self.omx = None
self.omxArgs = omxArgs
self.cgcmd = CGCommands();
def PlayMedia(self, fullpath, tag, unknown1, unknown2, unknown3):
global parsed_path
global media_key
global duration
parsed_path = urlparse(fullpath)
media_path = parsed_path.scheme + "://" + parsed_path.netloc + tag
self.media = self.plex.getMedia(media_path)
print 'mediapath', media_path
if(self.omx):
self.Stop()
transcodeURL = self.media.getTranscodeURL()
f = urllib2.urlopen(transcodeURL)
rawXML = f.read()
f.close()
for line in string_to_lines(rawXML):
line = line.strip()
if line.startswith("#"):
sessionURL = ""
else:
sessionURL = self.media.getTranscodeSessionURL() + line
self.cgcmd.SendPlay(sessionURL)
def Pause(self, message):
self.cgcmd.SendCommand(40);
def Play(self, message):
self.cgcmd.SendCommand(39);
def Stop(self, message):
self.cgcmd.SendCommand(41);
def Up(self, message):
self.cgcmd.SendCommand(19);
def Down(self, message):
self.cgcmd.SendCommand(20);
def Left(self, message):
self.cgcmd.SendCommand(21);
def Right(self, message):
self.cgcmd.SendCommand(22);
def ContextMenu(self, message):
self.cgcmd.SendCommand(17);
def ParentDir(self, message):
self.cgcmd.SendCommand(24);
def Select(self, message):
self.cgcmd.SendCommand(23);
def OSD(self, message):
self.cgcmd.SendCommand(2);
def getMilliseconds(self,s):
hours, minutes, seconds = (["0", "0"] + ("%s" % s).split(":"))[-3:]
hours = int(hours)
minutes = int(minutes)
seconds = float(seconds)
miliseconds = int(3600000 * hours + 60000 * minutes + 1000 * seconds)
return miliseconds
def getPosMilli(self):
return self.getMilliseconds(self.omx.position)
def setPlayed(self):
self.media.setPlayed()
def isFinished(self):
if(self.omx):
finished = self.omx.finished
else:
finished = True
return finished
def isRunning(self):
if(self.omx):
return True
return False
def updatePosition(self):
if self.isFinished():
if (self.getPosMilli() > (self.media.duration * .95)):
self.setPlayed()
self.Stop()
else:
self.media.updatePosition(self.getPosMilli())
http = None
udp = None
if __name__ == "__main__":
hostname = platform.uname()[1]
try:
print "starting, please wait..."
global service
global queue
global parsed_path
global media_key
global duration
duration = 0
args = len(sys.argv)
if args > 1:
if sys.argv[1] == "hdmi":
omxCommand = '-o hdmi'
print "Audo output over HDMI"
else:
omxCommand = ''
print "Audio output over 3,5mm jack"
xbmcCmmd = xbmcCommands(omxCommand)
media_key = None
parsed_path = None
queue = Queue.Queue()
service = ZeroconfService(name= "MC-4GS-HD", port=3000, text=["machineIdentifier=" + hostname,"version=2.0"])
service.publish()
udp = udplistener.udplistener(queue)
udp.start()
http = httplistener.httplistener(queue)
http.start()
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
while True:
try:
command, args = queue.get(True, 2)
print "Got command: %s, args: %s" %(command, args)
if not hasattr(xbmcCmmd, command):
print "Command %s not implemented yet" % command
else:
func = getattr(xbmcCmmd, command)
func(*args)
# service.unpublish()
except Queue.Empty:
pass
if(xbmcCmmd.isRunning()):
# print omx.position
xbmcCmmd.updatePosition()
except:
print "Caught exception"
if(xbmcCmmd):
xbmcCmmd.Stop("")
if(udp):
udp.stop()
udp.join()
if(http):
http.stop()
http.join()
raise