forked from mdbraber/radiobar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiobar.py
executable file
·328 lines (276 loc) · 12.2 KB
/
radiobar.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rumps
import ctypes
import os
import platform
import requests
import socket
import threading
import json
import time
import re
from AppKit import NSAttributedString, NSScreen
from PyObjCTools.Conversion import propertyListFromPythonCollection
from Cocoa import (NSFont, NSFontAttributeName, NSColor, NSForegroundColorAttributeName)
# preload libvlccore.dylib
# https://github.com/oaubert/python-vlc/issues/37
d = '/Applications/VLC.app/Contents/MacOS/'
p = d + 'lib/libvlc.dylib'
if os.path.exists(p):
# force pre-load of libvlccore.dylib # ****
ctypes.CDLL(d + 'lib/libvlccore.dylib') # ****
dll = ctypes.CDLL(p)
import vlc
rumps.debug_mode(False)
if 'VLC_PLUGIN_PATH' not in os.environ:
# print('VLC_PLUGIN_PATH not set. Setting now...')
os.environ['VLC_PLUGIN_PATH'] = '$VLC_PLUGIN_PATH:/Applications/VLC.app/Contents/MacOS/plugins'
class RadioBarRemoteThread(threading.Thread):
def __init__(self, radiobar, host, port):
super(RadioBarRemoteThread, self).__init__()
self.stop_event = threading.Event()
self.radiobar = radiobar
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.port = port
self.socket.bind((self.host, self.port))
self.socket.listen()
def run(self):
radiobar = self.radiobar
while not self.stop_event.is_set():
c, addr = self.socket.accept()
data = c.recv(1024)
msg = data.decode("utf-8")
print("Remote received: " + msg)
if msg == "":
radiobar.toggle(radiobar.menu[radiobar.active_station])
elif msg.isnumeric() and 0 <= int(msg)-1 < len(radiobar.stations):
radiobar.play(radiobar.menu[radiobar.stations[int(msg)-1]['title']])
c.send(b'Listening to ' + radiobar.stations[int(msg)-1]['title'].encode('utf-8'))
elif msg == "off":
radiobar.stop(radiobar.menu["Stop"])
c.send(b'Off')
elif msg == "on" or msg == "resume":
if radiobar.active_station:
radiobar.toggle(radiobar.menu[radiobar.active_station])
c.send(b'On')
elif msg == "pause":
if radiobar.active_station:
radiobar.toggle(radiobar.menu[radiobar.active_station])
c.send(b'Pause')
elif msg == "nowplaying":
c.send(bytes(radiobar.nowplaying.encode('utf-8')))
elif msg == "show":
radiobar.notify(radiobar.nowplaying)
c.send(bytes(radiobar.nowplaying.encode('utf-8')))
elif msg == "toggle":
radiobar.toggle()
c.send(b'Toggle ' + radiobar.active_station.encode('utf-8'))
else:
c.send(b'Unknown input')
c.close()
def stop(self):
self.stop_event.set()
class RadioBar(rumps.App):
def __init__(self):
super(RadioBar, self).__init__('RadioBar',icon='radio-icon-grey.png', template=None, quit_button=None)
self.show_notifications = True
self.show_notification_station_change = False
self.show_nowplaying_menubar = True
self.default_icon = 'radio-icon.png' # 'radio-icon.png' or 'radio-icon-green.png'
self.default_icon_disabled = 'radio-icon-grey.png'
self.default_color_list = [255,255,255,1] # format: r,g,b,alpha // [29,185,84,1] = "Spotify green" // [0,0,0,1] = "White"
self.default_color_list_disabled = [255,255,255,0.4]
# Truncate if the screen is smaller than 1440px wide
self.truncate = NSScreen.screens()[0].frame().size.width <= 1440
self.active_station = None
self.nowplaying = None
self.player = vlc.MediaPlayer()
self.stations = []
self.urls = {}
self.get_stations()
# prevent multiple calls from sleep/wake
self.awake = True
self.threads = []
remote_thread = RadioBarRemoteThread(self, '127.0.0.1', 65432)
remote_thread.daemon = True
self.threads.append(remote_thread)
remote_thread.start()
def set_title(self, title, color_list=None):
self.title = title
if color_list is None:
color_list = self.default_color_list
if title is not None:
if self.truncate and len(title) > 40:
title = title[:37] + '...'
# This is hacky, but works
# https://github.com/jaredks/rumps/issues/30
color = NSColor.colorWithCalibratedRed_green_blue_alpha_(color_list[0]/255, color_list[1]/255, color_list[2]/255, color_list[3])
font = NSFont.menuBarFontOfSize_(0)
attributes = propertyListFromPythonCollection({NSForegroundColorAttributeName: color, NSFontAttributeName: font}, conversionHelper=lambda x: x)
string = NSAttributedString.alloc().initWithString_attributes_(' ' + title, attributes)
self._nsapp.nsstatusitem.setAttributedTitle_(string)
def build_menu(self):
self.menu.clear()
if len(self.stations) < 1:
rumps.alert('No stations loaded.')
new_menu = []
new_menu.append(rumps.MenuItem('Now Playing', callback=None))
new_menu.append(rumps.separator)
for station in self.stations:
item = rumps.MenuItem(station['title'], callback=self.toggle)
new_menu.append(item)
new_menu.append(rumps.separator)
new_menu.append(rumps.MenuItem('Stop'))
new_menu.append(rumps.separator)
new_menu.append(rumps.MenuItem('Quit RadioBar',callback=self.quit))
self.menu = new_menu
def get_stations(self):
if len(self.stations) > 0:
return
try:
with open('channels.json') as json_data:
j = json.load(json_data)
for c in j['channels']:
self.stations.append(c)
self.urls[c['title']] = c['url']
except requests.exceptions.RequestException as e:
rumps.alert(e)
self.build_menu()
def start_radio(self):
# craft station url
station_url = self.urls[self.active_station]
print(u'Playing URL %s' % station_url)
# feed url to player
self.player.set_mrl(station_url)
self.player.play()
def reset_menu_state(self):
if self.active_station is None:
return
self.menu[self.active_station].state = 0
self.menu[self.active_station].set_callback(self.toggle)
self.active_station = None
self.set_title(None)
self.menu['Stop'].state = 0
self.menu['Stop'].title = 'Stop'
self.menu['Stop'].set_callback(None)
self.icon = self.default_icon_disabled
self.menu['Now Playing'].title = 'Nothing playing...'
def play(self, sender):
# is there already a station playing or a paused station?
if self.active_station is not None and self.menu[self.active_station].state is not 0:
self.reset_menu_state()
self.active_station = sender.title
self.set_title(sender.title)
self.icon = self.default_icon
sender.state = 1
self.menu['Stop'].set_callback(self.stop)
self.menu['Stop'].title = 'Stop'
print("Switching to station: " + self.active_station)
self.start_radio()
time.sleep(.3)
self.update_nowplaying()
print("Playing: " + self.nowplaying)
if self.show_notification_station_change:
self.notify(self.nowplaying)
def toggle(self, sender):
# Stopped -> Playing
if sender is not None:
# Starting to play - not been playing before
if self.active_station is None:
self.play(sender)
# Paused, but we want to play another station
elif self.menu[self.active_station] is not sender:
self.play(sender)
# Paused and clicked the currently paused station
else:
active_menu = self.menu[self.active_station]
# Playing -> Paused
if active_menu.state == 1:
self.pause(active_menu)
# Paused -> Playing
elif active_menu.state == -1:
self.play(active_menu)
def pause(self, sender):
# We're really stopping (because it's live radio and we don't want to buffer)
self.player.stop()
sender.state = - 1
self.set_title(self.active_station, self.default_color_list_disabled)
self.icon = self.default_icon_disabled
self.nowplaying = None
self.menu['Now Playing'].title = 'Paused'
def stop(self, sender):
self.player.stop()
self.reset_menu_state()
self.notify("Stopped")
def get_nowplaying(self):
if self.active_station is not None:
media = self.player.get_media()
try:
media.parse_with_options(vlc.MediaParseFlag.network, 0)
title = media.get_meta(vlc.Meta.Title)
artist = media.get_meta(vlc.Meta.Artist)
nowplaying = media.get_meta(vlc.Meta.NowPlaying)
if artist and artist != "" and title and title != "":
return artist + " - " + title
elif nowplaying and nowplaying != "":
return nowplaying
elif self.active_station != "":
return self.active_station
else:
return "Nothing playing..."
except AttributeError as e:
return None
def update_nowplaying(self):
state = self.player.get_state()
# Try to update information asap, even if vlc.State.Opening
if self.active_station is not None and self.player.get_state() in {vlc.State.Playing, vlc.State.Opening}:
old_info = self.nowplaying
new = self.get_nowplaying()
new_info = new.replace(self.active_station + " - ","")
# Remove non-info like "TOPSONG: " (NPO Radio 2)
new_info = new_info.replace("TOPSONG: ","")
new_info = new_info.replace("HI: ","")
# Remove trailing station info like "De Nieuws BV - BNN-VARA"
new_info = re.sub(r' - [A-Z-]*$', "", new_info)
if new_info.isupper():
# Fix ALL UPPERCASE strings (and some annyoing regressions)
new_info = new_info.title()
# Get rid of uninteresting info like "Franz Ferdinand - This Fire (3Fm Intro)"
new_info = re.sub(r'\(3Fm .*\)', "", new_info)
self.nowplaying = new_info
self.menu['Now Playing'].title = new_info
if new_info == self.active_station:
self.set_title(new_info)
if old_info is None or new_info != old_info:
if self.show_nowplaying_menubar:
self.set_title(self.active_station + ' - ' + new_info)
# This depends on how your stations work, but for me the station changes back to "Station Name - Show Name" after a song
# and I don't want notifications all the time the show name comes back on as Now Playing new_info...
# So we only show notifications when the new info doesn't start with the station name.
if not new.startswith(self.active_station):
self.notify(new_info)
@rumps.timer(10)
def track_metadata_changes(self, sender):
self.update_nowplaying()
def notify(self, msg):
print("Notification: " + msg)
if self.active_station:
rumps.notification('RadioBar', self.active_station, msg)
else:
rumps.notification('RadioBar', msg, None)
def quit(self, sender):
for t in self.threads:
t.stop()
rumps.quit_application(sender)
def sleep(self):
print("Going to sleep!")
if self.awake and self.active_station:
self.pause(self.menu[self.active_station])
self.awake = False
def wake(self):
print("Waking up!")
self.awake = True
if __name__ == "__main__":
RadioBar().run()