funkwlmpv/funkwhale_cli.py

58 lines
1.7 KiB
Python
Raw Normal View History

from src.fw_api import s, select_instance, instance
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
import json, sys
from loguru import logger
2022-11-01 12:15:28 +02:00
from pyfzf.pyfzf import FzfPrompt
fzf = FzfPrompt()
def main():
logger.remove()
logger.add(sys.stderr, filter='src.fw_api')
logger.add(sys.stderr, filter='src.fw_radios')
2022-11-08 19:58:27 +02:00
logger.add(sys.stderr, filter='src.fw_artists')
2022-11-01 12:15:28 +02:00
while True:
2022-11-08 19:58:27 +02:00
menu = ['Radios',
'Artists',
'Switch instance']
if not s.headers.get('Authorization'):
menu.append('Sign in')
2022-11-01 12:15:28 +02:00
ids = fzf.prompt(menu)
selected = ids[0]
if selected == 'Radios':
list_radios()
2022-11-08 19:58:27 +02:00
if selected == 'Artists':
list_artists()
if selected == 'Switch instance':
with open('config.json', 'rt') as f:
conf = json.loads(f.read())
list_instances = conf.get('public_list_instances')
instance = fzf.prompt(list_instances, '--header \'Select instance\'')[0]
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
Scopes: mark Read
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
select_instance(instance)
2022-11-01 12:15:28 +02:00
if __name__ == '__main__':
main()