funkwlmpv/funkwhale_cli.py

119 lines
4.1 KiB
Python
Raw Normal View History

2022-12-07 22:26:59 +02:00
#!/usr/bin/env python3
2022-12-04 03:01:11 +02:00
from src.fw_api import current_instance, federate_search_by_url, get_instance_settings
2022-11-01 12:15:28 +02:00
from src.fw_radios import list_radios
2022-11-08 19:58:27 +02:00
from src.fw_artists import list_artists
2022-11-08 20:08:24 +02:00
from src.fw_albums import list_albums
from src.fw_tracks import list_tracks
from src.fw_channels import list_channels
2022-11-10 17:55:36 +02:00
from src.fw_playlists import list_playlists
2022-11-15 03:22:32 +02:00
from src.fw_fav_tracks import list_favorites_tracks
import src.settings as settings
import src.mpv_control
2022-12-18 03:02:53 +02:00
import json
import sys
import time
from shlex import quote
from loguru import logger
2022-11-01 12:15:28 +02:00
from pyfzf.pyfzf import FzfPrompt
fzf = FzfPrompt()
2022-12-18 03:02:53 +02:00
2022-11-01 12:15:28 +02:00
def main():
2022-11-01 12:15:28 +02:00
while True:
support_message = ''
instance_title = ''
try:
ins_settings = get_instance_settings()
for i in ins_settings:
if i.get('name') == 'support_message':
support_message = i.get('value')
if i.get('name') == 'name':
instance_title = i.get('value')
main_menu_header = quote(f'''{instance_title}'''.strip())
except:
time.sleep(1)
main_menu_header = quote(f'''Connection failed'''.strip())
2022-12-18 03:02:53 +02:00
menu = ['Radios',
'Artists',
'Albums',
'Tracks',
'Channels',
2022-11-10 17:55:36 +02:00
'Playlists',
2022-11-15 03:22:32 +02:00
'Favorites',
'Search',
2022-11-08 19:58:27 +02:00
'Switch instance']
2022-12-04 03:01:11 +02:00
if not current_instance.s.headers.get('Authorization'):
menu.append('Sign in')
if support_message != '':
menu.append('Donate')
if src.mpv_control.player.playlist_playing_pos != -1:
menu.insert(0, 'Player')
ids = fzf.prompt(menu, f"--header={main_menu_header}")
2022-11-01 12:15:28 +02:00
selected = ids[0]
if selected == 'Radios':
list_radios()
2022-11-08 19:58:27 +02:00
if selected == 'Artists':
list_artists()
2022-11-08 20:08:24 +02:00
if selected == 'Albums':
list_albums()
if selected == 'Tracks':
list_tracks()
if selected == 'Channels':
list_channels()
2022-11-10 17:55:36 +02:00
if selected == 'Playlists':
list_playlists()
2022-11-15 03:22:32 +02:00
if selected == 'Favorites':
list_favorites_tracks()
if selected == 'Search':
search_type = fzf.prompt(('Federated', 'All types'))[0]
if search_type == 'Federated':
print('Input url:')
returned_obj = federate_search_by_url(input())
2022-11-10 00:15:57 +02:00
logger.info(str(returned_obj))
if selected == 'Switch instance':
with open('config.json', 'rt') as f:
conf = json.loads(f.read())
2022-11-28 01:48:22 +02:00
public_server_list_instances = settings.get_new_funkwhale_servers()
new_ins_count = len(settings.get_new_funkwhale_servers())
2022-12-18 03:02:53 +02:00
list_instances = conf.get(
'public_list_instances') + public_server_list_instances
instance = fzf.prompt(list_instances,
2022-11-28 01:48:22 +02:00
'--header='+quote(f'Select instance\nServer instances: +{new_ins_count}'))[0]
2022-12-04 03:01:11 +02:00
current_instance.select_instance(instance)
if selected == 'Sign in':
print(f'''
If You want sign in, please visit:
https://{instance}/settings/applications/new
And fill Name funkwhale-cli
2022-12-08 01:21:30 +02:00
Scopes: Read, Write (optional): write:favorites write:listenings write:filters
Insert token from "Access token" here''')
register_token = input()
with open('.auth.json', 'rt') as f:
tkns = json.loads(f.read())
with open('.auth.json', 'wt') as f:
tkns[instance] = register_token
f.write(json.dumps(tkns))
del tkns
del register_token
del f
2022-12-04 03:01:11 +02:00
current_instance.select_instance(instance)
if selected == 'Donate':
print('Support instance message:')
print(support_message)
input()
if selected == 'Player':
2022-12-18 03:02:53 +02:00
src.mpv_control.player_menu(
storage=src.mpv_control.player_fw_storage.storage)
2022-11-01 12:15:28 +02:00
if __name__ == '__main__':
main()