-
Notifications
You must be signed in to change notification settings - Fork 0
/
webradio
executable file
·110 lines (89 loc) · 3.56 KB
/
webradio
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
#!/usr/bin/env python
import dbus, dbus.mainloop.qt
import signal
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
def main(argv):
# We want ctrl-c to kill qt
signal.signal(signal.SIGINT, signal.SIG_DFL)
radio = Radio(argv)
radio.choose_station()
radio.window.show()
configure_dbus(radio)
return radio.exec_()
class Radio(QtGui.QApplication):
def __init__(self, argv):
QtGui.QApplication.__init__(self, argv)
self.setWindowIcon(QtGui.QIcon.fromTheme("applications-multimedia"))
self.window = QtGui.QMainWindow()
self.quitOnLastWindowClosed = True
self.browser = QtWebKit.QWebView(
titleChanged = self.window.setWindowTitle)
self.window.setCentralWidget(self.browser)
settings = self.browser.settings()
settings.setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
self.station = Station(self.browser, {"url": None})
QtGui.QShortcut("Ctrl+S", self.window, activated = self.choose_station)
def choose_station(self):
stations = get_stations()
station, ok = QtGui.QInputDialog.getItem(self.window,
"Choose a station",
"Station:",
stations.keys(),
editable = False)
if ok:
self.load_station(stations[str(station)])
def load_station(self, station_config):
self.station = Station(self.browser, station_config)
self.station.load()
def handle_media_key(self, source, event):
self.station.press_key(event)
class Station(object):
def __init__(self, browser, config):
self.browser = browser
self.is_paused = False
self.config = config
def load(self):
self.browser.load(QtCore.QUrl(self.config["url"]))
def press_key(self, key):
if key == "Play" and self.is_paused:
cmd = self.config["play"]
self.is_paused = False
elif key == "Play" or key == "Stop":
cmd = self.config["pause"]
self.is_paused = True
else:
cmd = self.config.get(key.lower())
if not cmd:
print "'%s' is not supported." % key
return
frame = self.browser.page().mainFrame()
frame.evaluateJavaScript(cmd)
def get_stations():
default_stations = {
"pandora": {
"url": "http://www.pandora.com",
"play": 'Pandora.sendTunerCommand("ext_play", "");',
"pause": 'Pandora.sendTunerCommand("ext_pause", "");',
},
"grooveshark": {
"url": "http://listen.grooveshark.com",
"play": "Grooveshark.play();",
"pause": "Grooveshark.pause();",
"previous": "Grooveshark.previous();",
"next": "Grooveshark.next();",
},
}
return default_stations
def configure_dbus(radio):
dbus.mainloop.qt.DBusQtMainLoop(set_as_default=True)
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
bus_object = bus.get_object('org.gnome.SettingsDaemon',
'/org/gnome/SettingsDaemon/MediaKeys')
dbus_interface = 'org.gnome.SettingsDaemon.MediaKeys'
bus_object.GrabMediaPlayerKeys("WebRadio", 0,
dbus_interface = dbus_interface)
bus_object.connect_to_signal('MediaPlayerKeyPressed',
radio.handle_media_key)
if __name__ == "__main__":
sys.exit(main(sys.argv))