/
mpris-test.py
339 lines (294 loc) · 8.68 KB
/
mpris-test.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from PyQt4 import QtDBus
from PyQt4.QtCore import QCoreApplication, QObject, Q_CLASSINFO, pyqtSlot, pyqtProperty, QTimer
from PyQt4.QtDBus import QDBusConnection, QDBusAbstractAdaptor
import http.client as httplib
#https://github.com/intarstudents/keySharky/wiki/api-server
class keySharky():
def __init__(self, address):
self.address = address
def _query(self, path):
conn = httplib.HTTPConnection(self.address)
conn.request("GET", path)
response = conn.getresponse()
return response.read().decode('utf-8')
def _parse(self, text):
lines = text.splitlines(True)
out = {}
for line in lines:
d = line.partition(": ")
out[d[0]] = d[2].strip()
return out
def play(self):
self._query("/play")
def stop(self):
self._query("/stop")
def previous(self):
self._query("/previous")
def next(self):
self._query("/next")
def favorite(self):
self._query("/favorite")
def remove(self):
self._query("/remove")
def voteup(self):
self._query("/voteup")
def votedown(self):
self._query("/votedown")
def voteclear(self):
self._query("/voteclear")
def mute(self):
self._query("/mute")
def voldown(self):
self._query("/voldown")
def volup(self):
self._query("/volup")
def currentSong(self):
s = self._query("/currentSong")
return self._parse(s)
def previousSong(self):
s = self._query("/previousSong")
return self._parse(s)
def nextSong(self):
s = self._query("/nextSong")
return self._parse(s)
def volume(self):
v = self._query("/volume")
return self._parse(v)
def setVolume(self, volume):
self._query("/setVolume?%d" % volume)
def muted(self):
self._query("/muted")
def gs_version(self):
self._query("/gs-version")
def gs_api_version(self):
self._query("/gs-api-version")
class MyServer(QObject):
def __init__(self):
QObject.__init__(self)
self.keySharky = keySharky("127.0.0.1:8800")
self.dbus_main = Mpris2_Main(self)
self.dbus_player = Mpris2_Player(self, self.keySharky)
class Mpris2_Main(QDBusAbstractAdaptor):
Q_CLASSINFO("D-Bus Interface", "org.mpris.MediaPlayer2")
Q_CLASSINFO("D-Bus Introspection",
' <interface name="org.mpris.MediaPlayer2">\n'
' <property name="CanQuit" type="b" access="read"/>\n'
' <property name="CanRaise" type="b" access="read"/>\n'
' <property name="HasTrackList" type="b" access="read"/>\n'
' <property name="Identity" type="b" access="read"/>\n'
' <property name="SupportedUriSchemes" type="b" access="read"/>\n'
' <property name="SupportedMimeTypes" type="b" access="read"/>\n'
' <method name="quit" />\n'
' </interface>\n')
def __init__(self, parent):
super().__init__(parent)
@pyqtProperty(bool)
def CanQuit(self):
return False
@pyqtProperty(bool)
def CanRaise(self):
return False
@pyqtProperty(bool)
def HasTrackList(self):
return False
@pyqtProperty(str)
def Identity(self):
return "Grooveshark"
@pyqtProperty(str)
def SupportedUriSchemes(self):
return ""
@pyqtProperty(str)
def SupportedMimeTypes(self):
return ""
@pyqtSlot()
def quit(self):
import sys
sys.exit(":D")
class Mpris2_Player(QDBusAbstractAdaptor):
Q_CLASSINFO("D-Bus Interface", "org.mpris.MediaPlayer2.Player")
Q_CLASSINFO("D-Bus Introspection",
' <interface name="org.mpris.MediaPlayer2.Player">\n'
' <property name="PlaybackStatus" type="s" access="read"/>\n'
' <property name="Rate" type="d" access="readwrite"/>\n'
' <property name="Shuffle" type="b" access="readwrite"/>\n'
' <property name="Metadata" type="a{sv}" access="read"/>\n'
' <property name="Volume" type="d" access="readwrite"/>\n'
' <property name="Position" type="x" access="read"/>\n'
' <property name="MinimumRate" type="d" access="read"/>\n'
' <property name="MaximumRate" type="d" access="read"/>\n'
' <property name="CanGoNext" type="b" access="read"/>\n'
' <property name="CanGoPrevious" type="b" access="read"/>\n'
' <property name="CanPlay" type="b" access="read"/>\n'
' <property name="CanPause" type="b" access="read"/>\n'
' <property name="CanSeek" type="b" access="read"/>\n'
' <property name="CanControl" type="b" access="read"/>\n'
' <method name="Next" />\n'
' <method name="Previous" />\n'
' <method name="Pause" />\n'
' <method name="PlayPause" />\n'
' <method name="Stop" />\n'
' <method name="Play" />\n'
' <method name="Seek">\n'
' <arg direction="in" type="x" name="Offset" />\n'
' </method>\n'
' <method name="SetPosition">\n'
' <arg direction="in" type="o" name="TrackId" />\n'
' <arg direction="in" type="x" name="Position" />\n'
' </method>\n'
' <method name="OpenUri">\n'
' <arg direction="in" type="s" name="Uri" />\n'
' </method>\n'
' </interface>\n')
def PropertiesChanged(self, interface, property, values):
"""Sends PropertiesChanged signal through sessionBus.
Args:
interface: interface name
property: property name
values: current property value(s)
"""
self.signal.setArguments(
[interface, {property: values}, []]
)
QtDBus.QDBusConnection.sessionBus().send(self.signal)
def __init__(self, parent, keySharky):
super().__init__(parent)
self.keySharky = keySharky
self.signal = QtDBus.QDBusMessage.createSignal(
"/org/mpris/MediaPlayer2",
"org.freedesktop.DBus.Properties",
"PropertiesChanged"
)
self.currentId = 1
self.track = {}
#self.prevTrack = {}
#self.nextTrack = {}
self.timer = QTimer()
self.timer.timeout.connect(self.tick)
self.timer.start(150)
self.tick()
def tick(self):
old = self.track
current = self.keySharky.currentSong()
self.track = current
if "songID" not in old:
return
if old["songID"] != current["songID"]:
self.PropertiesChanged("org.mpris.MediaPlayer2.Player", "Metadata", self.getMetadata())
self.currentId += 1
if old["position"] != current["position"]:
self.PropertiesChanged("org.mpris.MediaPlayer2.Player", "Position", self.Position)
if old["status"] != current["status"]:
self.PropertiesChanged("org.mpris.MediaPlayer2.Player", "PlaybackStatus", self.PlaybackStatus)
def getTrack(self):
return self.track
def getMetadata(self):
entry = self.track
meta = {
"mpris:trackid": self.currentId,
"mpris:length": int(float(entry["calculatedDuration"])*1000),
"mpris:artUrl": entry["artURL"],
"xesam:album": entry["albumName"],
"xesam:artist": entry["artistName"],
"xesam:title": entry["songName"],
"xesam:trackNumber": entry["trackNum"],
"xesam:userRating": 1 if entry["vote"]==1 or entry["isInLibrary"] == "true" else 0
}
return meta
@pyqtProperty(str)
def PlaybackStatus(self):
#song = self.keySharky.currentSong()
#return song["status"]
status = self.getTrack()["status"]
return status.title()
@pyqtProperty(int)
def Rate(self):
return 1
#Todo: metadata
@pyqtProperty("QMap<QString, QVariant>")
def Metadata(self):
return self.getMetadata()
@pyqtProperty(float)
def Volume(self):
volume = self.keySharky.volume()
return float(volume["volume"])/100
@Volume.setter
def Volume(self, value):
self.keySharky.setVolume(value)*100
@pyqtProperty(int)
def Position(self):
return float(self.getTrack()["position"])*1000
@pyqtProperty(int)
def MinimumRate(self):
return 1
@pyqtProperty(int)
def MaximumRate(self):
return 1
@pyqtProperty(bool)
def CanGoNext(self):
song = self.keySharky.nextSong()
if "songID" in song:
return True
return False
@pyqtProperty(bool)
def CanGoPrevious(self):
song = self.keySharky.previousSong()
if "songID" in song:
return True
return False
#Todo: Can Play
@pyqtProperty(bool)
def CanPlay(self):
song = self.getTrack()
if "songID" in song:
return True
return False
@pyqtProperty(bool)
def CanPause(self):
song = self.getTrack()
if "songID" in song:
return True
return False
@pyqtProperty(bool)
def CanSeek(self):
return True
@pyqtProperty(bool)
def CanControl(self):
return True
@pyqtSlot()
def Next(self):
self.keySharky.next()
@pyqtSlot()
def Previous(self):
self.keySharky.previous()
@pyqtSlot()
def Pause(self):
self.keySharky.play()
@pyqtSlot()
def PlayPause(self):
self.keySharky.play()
@pyqtSlot()
def Stop(self):
self.keySharky.stop()
@pyqtSlot()
def Play(self):
self.keySharky.play()
@pyqtSlot()
def Seek(self):
#"If the CanSeek property is false, this has no effect. "
pass
@pyqtSlot()
def SetPosition(self, trackID, position):
#"If the CanSeek property is false, this has no effect. "
pass
@pyqtSlot()
def OpenURI(self, uri):
pass
def start():
app = QCoreApplication([])
bus = QDBusConnection.sessionBus()
server = MyServer()
bus.registerObject('/org/mpris/MediaPlayer2', server)
bus.registerService('org.mpris.MediaPlayer2.grooveshark')
app.exec_()
if __name__ == '__main__':
start()