fediplug/fediplay/cli.py

95 lines
2.7 KiB
Python
Raw Normal View History

2018-04-02 20:09:25 -04:00
'''Entry point for command-line interface.'''
2018-10-22 18:44:23 -04:00
options = {'debug': False}
2018-07-20 11:57:00 -04:00
import os
2018-07-22 20:13:10 -04:00
path = os.path
2018-04-02 20:09:25 -04:00
import sys
2018-07-20 11:57:00 -04:00
import appdirs
2018-04-02 20:09:25 -04:00
import click
from mastodon import Mastodon
2018-07-22 20:34:31 -04:00
from fediplay.dirs import DIRS
2018-04-02 20:09:25 -04:00
import fediplay.mastodon as mastodon
2018-07-22 20:13:10 -04:00
import fediplay.keyring as keyring
2018-04-02 20:09:25 -04:00
2018-07-20 11:57:00 -04:00
def ensure_dirs():
'''Make sure the application directories exist.'''
2018-07-22 20:34:31 -04:00
if not path.exists(DIRS.user_config_dir):
os.makedirs(DIRS.user_config_dir)
2018-07-20 11:57:00 -04:00
2018-07-22 20:34:31 -04:00
if not path.exists(DIRS.user_cache_dir):
os.makedirs(DIRS.user_cache_dir)
2018-07-20 11:57:00 -04:00
2018-07-22 20:13:10 -04:00
def get_access_token(instance):
'''Ensure the user credential exists.'''
2018-07-20 11:57:00 -04:00
2018-07-22 20:13:10 -04:00
keyring.migrate_access_token(instance)
2018-07-20 11:57:00 -04:00
2018-07-22 20:13:10 -04: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 11:57:00 -04:00
2018-07-22 20:13:10 -04:00
return keyring.get_credential(instance, keyring.CREDENTIAL_ACCESS_TOKEN)
2018-07-20 11:57:00 -04:00
2018-07-22 20:13:10 -04:00
def get_client_credentials(instance):
'''Ensure the client credentials exist.'''
2018-07-20 11:57:00 -04:00
2018-07-22 20:13:10 -04:00
keyring.migrate_client_credentials(instance)
2018-07-20 11:57:00 -04:00
2018-07-22 20:13:10 -04: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 11:57:00 -04:00
2018-07-22 20:13:10 -04:00
return (
keyring.get_credential(instance, keyring.CREDENTIAL_CLIENT_ID),
keyring.get_credential(instance, keyring.CREDENTIAL_CLIENT_SECRET)
)
2018-04-02 20:09:25 -04:00
@click.group()
2018-10-22 18:44:23 -04:00
@click.option('-d', '--debug', is_flag=True, help='Print debug messages.')
def cli(debug):
2018-07-22 16:03:28 -04:00
'''A program to play music your friends post on Mastodon.'''
2018-04-02 20:09:25 -04:00
2018-10-22 18:44:23 -04:00
options['debug'] = debug
2018-07-20 11:57:00 -04:00
ensure_dirs()
2018-04-02 20:09:25 -04:00
@cli.command()
@click.argument('instance')
def register(instance):
2018-07-22 16:03:28 -04:00
'''Register fediplay on your Mastodon instance.'''
2018-04-02 20:09:25 -04:00
2018-07-22 20:13:10 -04:00
mastodon.register(instance)
2018-04-02 20:09:25 -04:00
@cli.command()
@click.argument('instance')
def login(instance):
2018-07-22 16:03:28 -04:00
'''Log in to your Mastodon instance.'''
2018-04-02 20:09:25 -04:00
2018-07-22 20:13:10 -04:00
client_id, client_secret = get_client_credentials(instance)
2018-04-02 20:09:25 -04:00
click.echo('Open this page in your browser and follow the instructions.')
click.echo('Paste the code here.')
click.echo('')
2018-07-22 20:13:10 -04:00
click.echo(mastodon.get_auth_request_url(instance, client_id, client_secret))
2018-04-02 20:09:25 -04:00
click.echo('')
grant_code = input('Code: ')
2018-07-22 20:13:10 -04:00
mastodon.login(instance, client_id, client_secret, grant_code)
2018-04-02 20:09:25 -04:00
@cli.command()
@click.argument('instance')
2018-08-14 18:04:22 +02:00
@click.argument('users', nargs=-1)
def stream(instance, users):
2018-07-22 16:03:28 -04:00
'''Stream music from your timeline.'''
2018-04-02 20:09:25 -04:00
2018-07-22 20:13:10 -04:00
client_id, client_secret = get_client_credentials(instance)
access_token = get_access_token(instance)
2018-04-02 20:09:25 -04:00
2018-08-14 18:04:22 +02:00
mastodon.stream(instance, users, client_id, client_secret, access_token, cache_dir=DIRS.user_cache_dir)