2022-11-01 12:15:28 +02:00
|
|
|
from src.fw_api import get_radios, post_radio_session, get_track_radio, concatinate_endpoint
|
2022-11-06 02:45:56 +02:00
|
|
|
from src.utils import download_track
|
2022-11-01 12:15:28 +02:00
|
|
|
from src.mpv_control import player
|
|
|
|
from pyfzf.pyfzf import FzfPrompt
|
2022-11-06 02:45:56 +02:00
|
|
|
from loguru import logger
|
2022-11-02 02:05:08 +02:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2022-11-01 12:15:28 +02:00
|
|
|
fzf = FzfPrompt()
|
|
|
|
|
|
|
|
|
|
|
|
def list_radios():
|
|
|
|
radios = get_radios()
|
|
|
|
count = radios.get('count')
|
|
|
|
|
|
|
|
results = radios.get('results')
|
|
|
|
view = []
|
|
|
|
for i in results:
|
|
|
|
index = results.index(i)
|
|
|
|
id_radio = i.get('id')
|
|
|
|
name = i.get('name')
|
|
|
|
view.append(f'{index}.{name}')
|
|
|
|
|
2022-11-03 01:58:55 +02:00
|
|
|
view.append('Favourites')
|
|
|
|
|
2022-11-02 02:05:08 +02:00
|
|
|
selected = fzf.prompt(view, f'--header \'Found {count} radios\'')[0].split('.', 1)
|
2022-11-03 01:58:55 +02:00
|
|
|
if 'Favourites' in selected:
|
|
|
|
type_radio = 'favorites'
|
|
|
|
radio_load(id_radio, type_radio)
|
|
|
|
else:
|
|
|
|
id_selected = selected[0]
|
|
|
|
id_radio = results[int(id_selected)].get('id')
|
|
|
|
radio_load(id_radio)
|
2022-11-06 02:45:56 +02:00
|
|
|
|
2022-11-01 12:15:28 +02:00
|
|
|
|
2022-11-02 02:05:08 +02:00
|
|
|
def radio_generator(radio_session_id):
|
2022-11-06 02:45:56 +02:00
|
|
|
count_t = 0
|
2022-11-02 02:05:08 +02:00
|
|
|
while radio_session_id != '':
|
2022-11-06 02:45:56 +02:00
|
|
|
time.sleep(1)
|
|
|
|
if not radio_event_gen.wait(0):
|
|
|
|
break
|
|
|
|
count_t += 1
|
|
|
|
if count_t >= 60:
|
|
|
|
radio_get_track(radio_session_id)
|
|
|
|
logger.info('Radio generator stopped')
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-06 02:45:56 +02:00
|
|
|
radio_event_gen = threading.Event()
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-03 01:58:55 +02:00
|
|
|
def radio_load(id_radio=None, type_radio='custom'):
|
2022-11-01 12:15:28 +02:00
|
|
|
requested_radio = {
|
|
|
|
'custom_radio': id_radio,
|
2022-11-03 01:58:55 +02:00
|
|
|
'radio_type': type_radio,
|
2022-11-01 12:15:28 +02:00
|
|
|
'related_object_id': None
|
|
|
|
}
|
|
|
|
radio_session_id = post_radio_session(requested_radio).get('id')
|
2022-11-03 01:58:55 +02:00
|
|
|
for i in range(0, 5):
|
2022-11-01 12:15:28 +02:00
|
|
|
radio_get_track(radio_session_id)
|
2022-11-02 02:05:08 +02:00
|
|
|
|
2022-11-01 12:15:28 +02:00
|
|
|
player.playlist_pos = 0
|
2022-11-06 02:45:56 +02:00
|
|
|
radio_event_gen.set()
|
2022-11-02 02:05:08 +02:00
|
|
|
radio_task = threading.Thread(target=radio_generator, args=(radio_session_id,), daemon=True)
|
|
|
|
radio_task.start()
|
|
|
|
while True:
|
|
|
|
try:
|
2022-11-06 02:45:56 +02:00
|
|
|
input()
|
|
|
|
select = fzf.prompt(('Next', 'Prev', 'Pause', 'Download', 'Exit'), f'--header=\'Radio playing...\'')[0]
|
2022-11-02 02:05:08 +02:00
|
|
|
if select == 'Next':
|
|
|
|
radio_get_track(radio_session_id)
|
|
|
|
player.playlist_next()
|
|
|
|
elif select == 'Prev':
|
|
|
|
player.playlist_prev()
|
|
|
|
elif select == 'Pause':
|
|
|
|
if player.pause:
|
|
|
|
player.pause = False
|
|
|
|
else:
|
|
|
|
player.pause = True
|
2022-11-06 02:45:56 +02:00
|
|
|
elif select == 'Download':
|
|
|
|
download_track(player.filename)
|
2022-11-02 02:05:08 +02:00
|
|
|
elif select == 'Exit':
|
|
|
|
try:
|
2022-11-06 02:45:56 +02:00
|
|
|
radio_event_gen.clear()
|
2022-11-02 02:05:08 +02:00
|
|
|
except:
|
2022-11-06 02:45:56 +02:00
|
|
|
logger.exception('Error stopping Thread radio generator')
|
2022-11-02 02:05:08 +02:00
|
|
|
pass
|
|
|
|
player.playlist_clear()
|
|
|
|
player.stop()
|
|
|
|
break
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
2022-11-01 12:15:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def radio_get_track(radio_session_id):
|
|
|
|
radio_context = get_track_radio({'session': radio_session_id})
|
2022-11-03 01:58:55 +02:00
|
|
|
|
|
|
|
if radio_context == "Radio doesn't have more candidates":
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
track = radio_context.get('track')
|
|
|
|
listen_url = track.get('listen_url')
|
|
|
|
player.playlist_append(concatinate_endpoint(listen_url))
|
2022-11-01 12:15:28 +02:00
|
|
|
|
|
|
|
|