This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
372 lines (308 loc) · 16.5 KB
/
__init__.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
# Copyright (C) 2016 - 2019 Sylvia van Os <sylvia@hackerchick.me>
#
# Pext RadioBrowser module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gettext
import html
import os
import time
from pyradios import RadioBrowser
from shutil import which
from signal import SIGTERM
from subprocess import Popen
from pext_base import ModuleBase
from pext_helpers import Action, SelectionType
class Module(ModuleBase):
def init(self, settings, q):
self.module_path = os.path.dirname(os.path.abspath(__file__))
try:
lang = gettext.translation('pext_module_radio', localedir=os.path.join(self.module_path, 'locale'),
languages=[settings['_locale']])
except FileNotFoundError:
lang = gettext.NullTranslations()
print("No {} translation available for pext_module_radio".format(settings['_locale']))
lang.install()
self.rb = RadioBrowser()
self.settings = settings
self.q = q
self.favourites = []
try:
with open(os.path.join(self.module_path, "_user_favourites.txt"), "r") as favourites_file:
for favourite in favourites_file:
self.favourites.append(favourite.strip())
except IOError:
pass
self.cached = {'countries': {'time': 0},
'codecs': {'time': 0},
'languages': {'time': 0},
'tags': {'time': 0}}
self.cachedStations = {'_favourites': {'time': 0},
'countries': {},
'codecs': {},
'languages': {},
'tags': {},
'topvote': {'time': 0},
'topclick': {'time': 0},
'lastclick': {'time': 0},
'lastchange': {'time': 0}}
self.nowPlaying = None
if not which("ffplay"):
self.q.put([Action.critical_error, _("ffplay is not installed, please install it.")])
return
self._get_entries()
def _cache_expired(self, cache):
return cache['time'] < time.time() - 600
def _entry_depth(self, text):
if self._menu_to_type(text) in self.cached:
return 2
else:
return 1
def _menu_to_type(self, text):
if text == _('Favourites'):
return '_favourites'
elif text == _('By Country'):
return 'countries'
elif text == _('By Codec'):
return 'codecs'
elif text == _('By Language'):
return 'languages'
elif text == _('By Tags'):
return 'tags'
elif text == _('By Votes'):
return 'topvote'
elif text == _('By Most Tune-Ins'):
return 'topclick'
elif text == _('By Most Recent Listener'):
return 'lastclick'
elif text == _('By Most Recent Change'):
return 'lastchange'
else:
raise ValueError("Invalid text")
def _get_stations_by_menu_type(self, search_type):
if search_type == 'countries':
return self.rb.countries()
elif search_type == 'codecs':
return self.rb.codecs()
elif search_type == 'languages':
return self.rb.languages()
elif search_type == 'tags':
return self.rb.tags()
else:
return self._search_stations_by_type(search_type)
def _search_stations_by_type(self, search_type, search_term=None):
if search_type == 'countries':
return self.rb.stations_by_country(search_term, True)
elif search_type == 'codecs':
return self.rb.stations_by_codec(search_term, True)
elif search_type == 'languages':
return self.rb.stations_by_language(search_term, True)
elif search_type == 'tags':
return self.rb.stations_by_tag(search_term, True)
elif search_type == 'topvote':
return self.rb.stations(order='votes')
elif search_type == 'topclick':
return self.rb.stations(order='clickcount')
elif search_type == 'lastclick':
return self.rb.stations(order='clicktimestamp')
elif search_type == 'lastchange':
return self.rb.stations(order='lastchangetime')
else:
raise ValueError("Invalid type")
def _get_entries(self):
if self.favourites:
self.q.put([Action.add_entry, _('Favourites')])
self.q.put([Action.add_entry, _('By Country')])
self.q.put([Action.add_entry, _('By Codec')])
self.q.put([Action.add_entry, _('By Language')])
self.q.put([Action.add_entry, _('By Tags')])
self.q.put([Action.add_entry, _('By Votes')])
self.q.put([Action.add_entry, _('By Most Tune-Ins')])
self.q.put([Action.add_entry, _('By Most Recent Listener')])
self.q.put([Action.add_entry, _('By Most Recent Change')])
if self.settings['_api_version'] < [0, 11, 0] and self.nowPlaying:
if self.nowPlaying['process']:
self.q.put([Action.add_command, _('mute')])
else:
self.q.put([Action.add_command, _('unmute')])
self.q.put([Action.add_command, _('stop')])
self.q.put([Action.add_command, _('vote')])
def _get_list(self, path):
self.q.put([Action.replace_entry_list, []])
if self._cache_expired(self.cached[path]):
self.cached[path] = {'time': time.time(), 'data': self._get_stations_by_menu_type(path)}
for entry in self.cached[path]['data']:
self.q.put([Action.add_entry, _('{} ({} stations)').format(entry['name'], entry['stationcount'])])
def _get_stations(self, search_type, search_term):
if search_type == '_favourites':
if self._cache_expired(self.cachedStations[search_type]):
data = []
for favourite in self.favourites:
station_data = self.rb.station_by_uuid(favourite)
if station_data:
data.append(station_data[0])
self.cachedStations[search_type] = {'time': time.time(), 'data': data}
return self.cachedStations[search_type]
if search_term:
if search_term not in self.cachedStations[search_type] or self._cache_expired(
self.cachedStations[search_type][search_term]):
self.cachedStations[search_type][search_term] = {'time': time.time(),
'data': self._search_stations_by_type(search_type,
search_term)}
return self.cachedStations[search_type][search_term]
if self._cache_expired(self.cachedStations[search_type]):
self.cachedStations[search_type] = {'time': time.time(), 'data': self._search_stations_by_type(search_type)}
return self.cachedStations[search_type]
def _list_stations(self, search_type, search_term):
self.q.put([Action.replace_entry_list, []])
cache = self._get_stations(search_type, search_term)
for entry in cache['data']:
self.q.put([Action.add_entry, entry['name']])
if self.settings['_api_version'] >= [0, 3, 1]:
self.q.put([Action.set_entry_info, entry['name'], _("<b>{}</b><br/><br/><b>Bitrate: </b>{} kbps<br/><b>Codec: </b>{}<br/><b>Language: </b>{}<br/><b>Location: </b>{}<br/><b>Tags: </b>{}<br/><b>Homepage: </b><a href='{}'>{}</a>")
.format(html.escape(entry['name']), html.escape(str(entry['bitrate'])), html.escape(entry['codec']), html.escape(entry['language']), "{}, {}".format(html.escape(entry['state']), html.escape(entry['country'])) if entry['state'] else html.escape(entry['country']), html.escape(", ".join(entry['tags'].split(",")) if entry['tags'] else "None"), html.escape(entry['homepage']), html.escape(entry['homepage']))])
if self.settings['_api_version'] >= [0, 6, 0] and search_type == '_favourites':
self.q.put([Action.set_entry_context, entry['name'], [_("Unfavourite")]])
def _play_station(self, byType, searchTerm, stationName):
self._stop_playing()
if searchTerm:
cache = self.cachedStations[byType][searchTerm]
else:
cache = self.cachedStations[byType]
for station in cache['data']:
if station['name'] == stationName:
station_uuid = station['stationuuid']
station_info = station
break
response = self.rb.click_counter(station_uuid)
if response['ok'] == 'false':
self.q.put([Action.add_error, response['message']])
return False
# TODO: Replace ffplay with something more easily scriptable that
# preferably notifies us of song changes on the station.
self.nowPlaying = {'id': station_uuid,
'name': stationName,
'url': response['url'],
'process': None}
if self.settings['_api_version'] >= [0, 6, 0]:
self.q.put([Action.set_base_info, _("<b>Tuned into:</b><br/>{}<br/><br/><b>Bitrate: </b>{} kbps<br/><b>Codec: </b>{}<br/><b>Language: </b>{}<br/><b>Location: </b>{}<br/><b>Tags: </b>{}<br/><b>Homepage: </b><a href='{}'>{}</a>")
.format(html.escape(station_info['name']), html.escape(str(station_info['bitrate'])), html.escape(station_info['codec']), html.escape(station_info['language']), "{}, {}".format(html.escape(station_info['state']), html.escape(station_info['country'])) if station_info['state'] else html.escape(station_info['country']), html.escape(", ".join(station_info['tags'].split(",")) if station_info['tags'] else "None"), html.escape(station_info['homepage']), html.escape(station_info['homepage']))])
self._toggle_mute()
return True
def _toggle_mute(self):
"""Toggle mute.
While this function technically disconnects or connects to the
station, instead of just muting, it is simpler code-wise and has
the added benefit of saving bandwidth.
TODO: Replace this with an actual mute function.
"""
if self.nowPlaying:
if self.nowPlaying['process']:
os.kill(self.nowPlaying['process'].pid, SIGTERM)
self.nowPlaying['process'] = None
self.q.put([Action.set_header, _('Tuned into {} (muted)').format(self.nowPlaying['name'])])
if self.settings['_api_version'] >= [0, 6, 0]:
self.q.put([Action.set_base_context, [_("Unmute"), _("Stop"), _("Favourite"), _("Vote up")]])
else:
self.q.put([Action.set_header, _('Tuned into {}').format(self.nowPlaying['name'])])
self.nowPlaying['process'] = Popen(['ffplay',
'-nodisp',
'-nostats',
'-loglevel', '0',
self.nowPlaying['url']])
if self.settings['_api_version'] >= [0, 6, 0]:
self.q.put([Action.set_base_context, [_("Mute"), _("Stop"), _("Favourite"), _("Vote up")]])
def _stop_playing(self):
if self.nowPlaying:
if self.nowPlaying['process']:
os.kill(self.nowPlaying['process'].pid, SIGTERM)
self.nowPlaying = None
self.q.put([Action.set_header])
if self.settings['_api_version'] >= [0, 6, 0]:
self.q.put([Action.set_base_info])
self.q.put([Action.set_base_context])
def _add_to_favourites(self, station_id):
with open(os.path.join(self.module_path, "_user_favourites.txt"), "a") as favourites_file:
favourites_file.write('{}\n'.format(station_id))
self.favourites.append(station_id)
self.cachedStations['_favourites'] = {'time': 0}
def _remove_from_favourites(self, station_name):
for station in self._get_stations('_favourites', '')['data']:
if station['name'] == station_name:
self.favourites.remove(station['stationuuid'])
with open(os.path.join(self.module_path, "_user_favourites.txt"), "w") as favourites_file:
for favourite in self.favourites:
favourites_file.write('{}\n'.format(favourite))
self.cachedStations['_favourites'] = {'time': 0}
return
self.q.put([Action.add_error, _('Could not find {} in favourites').format(station_name)])
def _vote_station(self):
result = self.rb.client.get('vote/{}'.format(self.nowPlaying['id']))
if result['ok']:
self.q.put([Action.add_message, _('Voted for station {}').format(self.nowPlaying['name'])])
else:
self.q.put(
[Action.add_error, _('Failed to vote for {}: {}').format(self.nowPlaying['name'], result['message'])])
def stop(self):
self._stop_playing()
def selection_made(self, selection):
if self.settings['_api_version'] >= [0, 6, 0] and len(selection) > 0 and selection[-1]['context_option']:
if selection[-1]['type'] == SelectionType.none:
if selection[-1]['context_option'] in [_('Mute'), _('Unmute')]:
self._toggle_mute()
elif selection[-1]['context_option'] == _('Stop'):
self._stop_playing()
elif selection[-1]['context_option'] == _('Favourite'):
self._add_to_favourites(self.nowPlaying['id'])
elif selection[-1]['context_option'] == _('Vote up'):
self._vote_station()
elif selection[-1]['context_option'] == _("Unfavourite"):
self._remove_from_favourites(selection[-1]['value'])
if not self.favourites:
self.q.put([Action.set_selection, []])
return
self.q.put([Action.set_selection, selection[:-1]])
return
self.q.put([Action.replace_command_list, []])
if len(selection) == 0:
self.q.put([Action.replace_entry_list, []])
self._get_entries()
elif len(selection) == 1:
# Force station list when no subcategories
if self._entry_depth(selection[0]['value']) == 1:
self._list_stations(self._menu_to_type(selection[0]['value']), '')
return
menu_text = selection[0]['value']
self._get_list(self._menu_to_type(menu_text))
elif len(selection) == 2:
# Force playing when no subcategories
if self._entry_depth(selection[0]['value']) == 1:
if self._play_station(self._menu_to_type(selection[0]['value']), '', selection[1]['value']):
self.q.put([Action.close])
else:
self.q.put([Action.set_selection, selection[:-1]])
return
# Remove station count from search term
search_term = selection[1]['value'][:selection[1]['value'].rfind('(')].rstrip()
self._list_stations(self._menu_to_type(selection[0]['value']), search_term)
elif len(selection) == 3:
# Remove station count from search term
search_term = selection[1]['value'][:selection[1]['value'].rfind('(')].rstrip()
if self._play_station(self._menu_to_type(selection[0]['value']), search_term, selection[2]['value']):
self.q.put([Action.close])
else:
self.q.put([Action.set_selection, selection[:-1]])
else:
self.q.put([Action.critical_error, _('Unexpected selection_made value: {}').format(selection)])
def process_response(self, response):
pass