2022-11-08 00:29:44 +00:00
|
|
|
from src.fw_api import s, get_radios, post_radio_session, get_track_radio, list_libraries, concatinate_endpoint
|
2022-11-08 22:34:33 +00:00
|
|
|
from src.fw_libraries import libraries
|
2022-11-06 00:45:56 +00:00
|
|
|
from src.utils import download_track
|
2022-11-01 10:15:28 +00:00
|
|
|
from src.mpv_control import player
|
|
|
|
from pyfzf.pyfzf import FzfPrompt
|
2022-11-06 00:45:56 +00:00
|
|
|
from loguru import logger
|
2022-11-02 00:05:08 +00:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2022-11-01 10:15:28 +00:00
|
|
|
fzf = FzfPrompt()
|
|
|
|
|
2022-11-07 23:40:26 +00:00
|
|
|
audio_info = {}
|
2022-11-01 10:15:28 +00:00
|
|
|
|
2022-11-08 20:39:01 +00:00
|
|
|
@logger.catch
|
2022-11-01 10:15:28 +00:00
|
|
|
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-06 01:26:50 +00:00
|
|
|
if s.headers.get('Authorization'): # Radios avalaible only for auth user
|
|
|
|
view.append('Favourites')
|
|
|
|
view.append('Less listened')
|
|
|
|
view.append('Random')
|
2022-11-08 20:39:01 +00:00
|
|
|
view.append('Libraries')
|
|
|
|
view.append('Users')
|
2022-11-06 01:26:50 +00:00
|
|
|
view.append('Recently Added')
|
2022-11-02 23:58:55 +00:00
|
|
|
|
2022-11-02 00:05:08 +00:00
|
|
|
selected = fzf.prompt(view, f'--header \'Found {count} radios\'')[0].split('.', 1)
|
2022-11-02 23:58:55 +00:00
|
|
|
if 'Favourites' in selected:
|
2022-11-06 01:26:50 +00:00
|
|
|
radio_load(id_radio, 'favorites')
|
|
|
|
elif 'Random' in selected:
|
|
|
|
radio_load(id_radio, 'random')
|
2022-11-08 20:39:01 +00:00
|
|
|
elif 'Libraries' in selected:
|
2022-11-08 22:34:33 +00:00
|
|
|
id_radio, type_radio, name_radio, related_obj = libraries()
|
|
|
|
radio_load(id_radio, type_radio, name_radio, related_obj)
|
2022-11-08 20:39:01 +00:00
|
|
|
elif 'Users' in selected:
|
2022-11-08 00:29:44 +00:00
|
|
|
libs = list_libraries()['results']
|
|
|
|
libraries_listing = []
|
|
|
|
for lib_i in libs:
|
|
|
|
lib_name = lib_i.get('actor').get('full_username')
|
|
|
|
if lib_name not in libraries_listing:
|
|
|
|
libraries_listing.append(lib_name)
|
|
|
|
libraries_listing.append('Custom')
|
|
|
|
lib_addr = fzf.prompt(libraries_listing)[0]
|
|
|
|
if lib_addr == 'Custom':
|
|
|
|
print('Input remote user library (ex. nick@funkwhale.domain.example: ')
|
|
|
|
lib_addr = input()
|
2022-11-07 23:40:26 +00:00
|
|
|
radio_load(None, 'actor-content', lib_addr, lib_addr)
|
2022-11-06 01:26:50 +00:00
|
|
|
elif 'Recently Added' in selected:
|
|
|
|
radio_load(id_radio, 'recently-added')
|
|
|
|
elif 'Less listened' in selected:
|
|
|
|
radio_load(id_radio, 'less-listened')
|
2022-11-02 23:58:55 +00:00
|
|
|
else:
|
|
|
|
id_selected = selected[0]
|
|
|
|
id_radio = results[int(id_selected)].get('id')
|
2022-11-07 11:36:02 +00:00
|
|
|
name_radio = results[int(id_selected)].get('name')
|
|
|
|
radio_load(id_radio, name=name_radio)
|
2022-11-06 00:45:56 +00:00
|
|
|
|
2022-11-01 10:15:28 +00:00
|
|
|
|
2022-11-02 00:05:08 +00:00
|
|
|
def radio_generator(radio_session_id):
|
2022-11-06 00:45:56 +00:00
|
|
|
count_t = 0
|
2022-11-02 00:05:08 +00:00
|
|
|
while radio_session_id != '':
|
2022-11-06 00:45:56 +00:00
|
|
|
time.sleep(1)
|
|
|
|
if not radio_event_gen.wait(0):
|
|
|
|
break
|
|
|
|
count_t += 1
|
|
|
|
if count_t >= 60:
|
2022-11-06 22:21:49 +00:00
|
|
|
count_t = 0
|
2022-11-06 00:45:56 +00:00
|
|
|
radio_get_track(radio_session_id)
|
|
|
|
logger.info('Radio generator stopped')
|
2022-11-02 00:05:08 +00:00
|
|
|
|
2022-11-06 00:45:56 +00:00
|
|
|
radio_event_gen = threading.Event()
|
2022-11-02 00:05:08 +00:00
|
|
|
|
2022-11-08 18:37:43 +00:00
|
|
|
|
|
|
|
@logger.catch
|
2022-11-07 23:40:26 +00:00
|
|
|
def radio_load(id_radio=None, type_radio='custom', name=None, related_object=None):
|
2022-11-01 10:15:28 +00:00
|
|
|
requested_radio = {
|
|
|
|
'custom_radio': id_radio,
|
2022-11-02 23:58:55 +00:00
|
|
|
'radio_type': type_radio,
|
2022-11-07 23:40:26 +00:00
|
|
|
'related_object_id': related_object
|
2022-11-01 10:15:28 +00:00
|
|
|
}
|
|
|
|
radio_session_id = post_radio_session(requested_radio).get('id')
|
2022-11-07 23:40:26 +00:00
|
|
|
for i in range(0, 2):
|
2022-11-01 10:15:28 +00:00
|
|
|
radio_get_track(radio_session_id)
|
2022-11-02 00:05:08 +00:00
|
|
|
|
2022-11-06 00:45:56 +00:00
|
|
|
radio_event_gen.set()
|
2022-11-02 00:05:08 +00:00
|
|
|
radio_task = threading.Thread(target=radio_generator, args=(radio_session_id,), daemon=True)
|
|
|
|
radio_task.start()
|
|
|
|
while True:
|
|
|
|
try:
|
2022-11-07 23:40:26 +00:00
|
|
|
select = fzf.prompt(('Next', 'Prev', 'Pause', 'Download', 'Info', 'Exit'), f"--header=\'Radio {name} playing...\'")[0]
|
2022-11-02 00:05:08 +00:00
|
|
|
if select == 'Next':
|
2022-11-09 18:51:20 +00:00
|
|
|
threading.Thread(target=radio_get_track, args=(radio_session_id,), daemon=True).start()
|
2022-11-02 00:05:08 +00:00
|
|
|
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 00:45:56 +00:00
|
|
|
elif select == 'Download':
|
2022-11-06 00:56:09 +00:00
|
|
|
print('Downloading...')
|
|
|
|
name_downloaded = download_track(player.filename)
|
2022-11-07 23:40:26 +00:00
|
|
|
print(f'Downloaded: {name_downloaded}')
|
|
|
|
elif select == 'Info':
|
|
|
|
track = audio_info.get(player.filename)
|
|
|
|
for i in ('title', 'fid', 'license', 'album', 'artist'):
|
|
|
|
if i in ('album', 'artist'):
|
|
|
|
name_aa = track.get(i).get('name')
|
|
|
|
if not name_aa:
|
|
|
|
name_aa = track.get(i).get('title')
|
|
|
|
print(i + ': '+ name_aa)
|
|
|
|
key = track.get(i)
|
|
|
|
if key and isinstance(key, str):
|
|
|
|
print(i + ': ' + key)
|
|
|
|
input()
|
2022-11-02 00:05:08 +00:00
|
|
|
elif select == 'Exit':
|
|
|
|
try:
|
2022-11-06 00:45:56 +00:00
|
|
|
radio_event_gen.clear()
|
2022-11-02 00:05:08 +00:00
|
|
|
except:
|
2022-11-06 00:45:56 +00:00
|
|
|
logger.exception('Error stopping Thread radio generator')
|
2022-11-02 00:05:08 +00:00
|
|
|
pass
|
|
|
|
player.playlist_clear()
|
|
|
|
player.stop()
|
|
|
|
break
|
2022-11-09 18:51:20 +00:00
|
|
|
except:
|
|
|
|
try:
|
|
|
|
radio_event_gen.clear()
|
|
|
|
except:
|
|
|
|
logger.exception('Error stopping Thread radio generator')
|
|
|
|
pass
|
|
|
|
player.playlist_clear()
|
|
|
|
player.stop()
|
|
|
|
logger.exception('Radio force stopped')
|
2022-11-02 00:05:08 +00:00
|
|
|
break
|
2022-11-01 10:15:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def radio_get_track(radio_session_id):
|
|
|
|
radio_context = get_track_radio({'session': radio_session_id})
|
2022-11-06 22:21:49 +00:00
|
|
|
if not radio_context:
|
|
|
|
return
|
2022-11-09 18:51:20 +00:00
|
|
|
if isinstance(radio_context, str):
|
|
|
|
logger.error(radio_context)
|
2022-11-10 20:21:56 +00:00
|
|
|
if radio_context == "Radio doesn't have more candidates":
|
|
|
|
radio_event_gen.clear()
|
2022-11-09 18:51:20 +00:00
|
|
|
return
|
|
|
|
if radio_context.get('error'):
|
|
|
|
logger.error(radio_context.get('error'))
|
2022-11-02 23:58:55 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
track = radio_context.get('track')
|
|
|
|
listen_url = track.get('listen_url')
|
2022-11-07 23:40:26 +00:00
|
|
|
audio_info[concatinate_endpoint(listen_url)] = track
|
|
|
|
player.loadfile(concatinate_endpoint(listen_url), 'append-play')
|
2022-11-01 10:15:28 +00:00
|
|
|
|
|
|
|
|