fediplug/fediplay/cli.py

88 lines
2.5 KiB
Python
Raw Normal View History

2018-04-03 02:09:25 +02:00
'''Entry point for command-line interface.'''
2018-07-20 17:57:00 +02:00
import os
2018-07-23 02:13:10 +02:00
path = os.path
2018-04-03 02:09:25 +02:00
import sys
2018-07-20 17:57:00 +02:00
import appdirs
2018-04-03 02:09:25 +02:00
import click
from mastodon import Mastodon
import fediplay.mastodon as mastodon
2018-07-23 02:13:10 +02:00
import fediplay.keyring as keyring
2018-04-03 02:09:25 +02:00
2018-07-20 17:57:00 +02:00
def ensure_dirs():
'''Make sure the application directories exist.'''
2018-07-23 02:13:10 +02:00
if not path.exists(keyring.dirs.user_config_dir):
os.makedirs(keyring.dirs.user_config_dir)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
if not path.exists(keyring.dirs.user_cache_dir):
os.makedirs(keyring.dirs.user_cache_dir)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
def get_access_token(instance):
'''Ensure the user credential exists.'''
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
keyring.migrate_access_token(instance)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
if not keyring.has_credential(instance, keyring.CREDENTIAL_ACCESS_TOKEN):
click.echo('user credential for {} does not exist; try `fediplay login`'.format(instance))
sys.exit(1)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
return keyring.get_credential(instance, keyring.CREDENTIAL_ACCESS_TOKEN)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
def get_client_credentials(instance):
'''Ensure the client credentials exist.'''
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
keyring.migrate_client_credentials(instance)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
if not (keyring.has_credential(instance, keyring.CREDENTIAL_CLIENT_ID) and
keyring.has_credential(instance, keyring.CREDENTIAL_CLIENT_SECRET)):
click.echo('client credentials for {} do not exist; try `fediplay register`'.format(instance))
sys.exit(1)
2018-07-20 17:57:00 +02:00
2018-07-23 02:13:10 +02:00
return (
keyring.get_credential(instance, keyring.CREDENTIAL_CLIENT_ID),
keyring.get_credential(instance, keyring.CREDENTIAL_CLIENT_SECRET)
)
2018-04-03 02:09:25 +02:00
@click.group()
def cli():
2018-07-22 22:03:28 +02:00
'''A program to play music your friends post on Mastodon.'''
2018-04-03 02:09:25 +02:00
2018-07-20 17:57:00 +02:00
ensure_dirs()
2018-04-03 02:09:25 +02:00
@cli.command()
@click.argument('instance')
def register(instance):
2018-07-22 22:03:28 +02:00
'''Register fediplay on your Mastodon instance.'''
2018-04-03 02:09:25 +02:00
2018-07-23 02:13:10 +02:00
mastodon.register(instance)
2018-04-03 02:09:25 +02:00
@cli.command()
@click.argument('instance')
def login(instance):
2018-07-22 22:03:28 +02:00
'''Log in to your Mastodon instance.'''
2018-04-03 02:09:25 +02:00
2018-07-23 02:13:10 +02:00
client_id, client_secret = get_client_credentials(instance)
2018-04-03 02:09:25 +02:00
click.echo('Open this page in your browser and follow the instructions.')
click.echo('Paste the code here.')
click.echo('')
2018-07-23 02:13:10 +02:00
click.echo(mastodon.get_auth_request_url(instance, client_id, client_secret))
2018-04-03 02:09:25 +02:00
click.echo('')
grant_code = input('Code: ')
2018-07-23 02:13:10 +02:00
mastodon.login(instance, client_id, client_secret, grant_code)
2018-04-03 02:09:25 +02:00
@cli.command()
@click.argument('instance')
def stream(instance):
2018-07-22 22:03:28 +02:00
'''Stream music from your timeline.'''
2018-04-03 02:09:25 +02:00
2018-07-23 02:13:10 +02:00
client_id, client_secret = get_client_credentials(instance)
access_token = get_access_token(instance)
2018-04-03 02:09:25 +02:00
2018-07-23 02:13:10 +02:00
mastodon.stream(instance, client_id, client_secret, access_token, cache_dir=keyring.dirs.user_cache_dir)