funkwlmpv/src/fw_radios.py

80 lines
2.3 KiB
Python
Raw Normal View History

2022-11-01 12:15:28 +02:00
from src.fw_api import get_radios, post_radio_session, get_track_radio, concatinate_endpoint
from src.mpv_control import player
from pyfzf.pyfzf import FzfPrompt
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}')
selected = fzf.prompt(view, f'--header \'Found {count} radios\'')[0].split('.', 1)
id_selected = selected[0]
id_radio = results[int(id_selected)].get('id')
2022-11-01 12:15:28 +02:00
radio_load(id_radio)
def radio_generator(radio_session_id):
while radio_session_id != '':
radio_get_track(radio_session_id)
time.sleep(60)
2022-11-01 12:15:28 +02:00
def radio_load(id_radio):
requested_radio = {
'custom_radio': id_radio,
'radio_type': 'custom',
'related_object_id': None
}
radio_session_id = post_radio_session(requested_radio).get('id')
for i in range(0, 10):
radio_get_track(radio_session_id)
2022-11-01 12:15:28 +02:00
player.playlist_pos = 0
radio_task = threading.Thread(target=radio_generator, args=(radio_session_id,), daemon=True)
radio_task.start()
while True:
try:
print(len(player.playlist))
select = fzf.prompt(('Next', 'Prev', 'Pause', 'Exit'), f'--header=\'Radio playing...\'')[0]
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
elif select == 'Exit':
try:
radio_task.join(1)
except:
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})
track = radio_context.get('track')
listen_url = track.get('listen_url')
player.playlist_append(concatinate_endpoint(listen_url))