refactor and rename secret files

This commit is contained in:
Matt Behrens 2018-07-20 11:25:41 -04:00
parent d242253a93
commit 074047c602
4 changed files with 60 additions and 35 deletions

View file

@ -8,10 +8,16 @@ from mastodon import Mastodon
import fediplay.mastodon as mastodon
def api_base_url_from_instance(instance):
'''Create an API base url from an instance name.'''
return 'https://' + instance
def build_usercred_filename(instance):
'''Generate a usercred filename from an instance name.'''
return instance + '.usercred.secret'
def build_clientcred_filename(instance):
'''Generate a clientcred filename from an instance name.'''
return instance + '.clientcred.secret'
@click.group()
def cli():
@ -24,53 +30,55 @@ def cli():
def register(instance):
'''Register fediplay to the instance.'''
api_base_url = api_base_url_from_instance(instance)
clientcred = build_clientcred_filename(instance)
if path.exists('clientcred.secret'):
click.echo('clientcred.secret already exists')
if path.exists(clientcred):
click.echo(clientcred + ' already exists')
sys.exit(1)
mastodon.register(api_base_url)
mastodon.register(instance, clientcred)
@cli.command()
@click.argument('instance')
def login(instance):
'''Log in to the instance.'''
api_base_url = api_base_url_from_instance(instance)
usercred = build_usercred_filename(instance)
clientcred = build_clientcred_filename(instance)
if path.exists('usercred.secret'):
click.echo('usercred.secret already exists')
if path.exists(usercred):
click.echo(usercred + ' already exists')
sys.exit(1)
if not path.exists('clientcred.secret'):
click.echo('clientcred.secret does not exist; try `fediplay register`')
if not path.exists(clientcred):
click.echo(clientcred + ' does not exist; try `fediplay register`')
sys.exit(1)
client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url)
client = mastodon.build_client(instance, clientcred)
click.echo('Open this page in your browser and follow the instructions.')
click.echo('Paste the code here.')
click.echo('')
click.echo(client.auth_request_url(scopes=['read']))
click.echo(mastodon.get_auth_request_url(client))
click.echo('')
grant_code = input('Code: ')
mastodon.login(client, grant_code)
mastodon.login(client, grant_code, usercred)
@cli.command()
@click.argument('instance')
def stream(instance):
'''Stream music from the instance.'''
api_base_url = api_base_url_from_instance(instance)
clientcred = build_clientcred_filename(instance)
usercred = build_usercred_filename(instance)
if not path.exists('clientcred.secret'):
click.echo('clientcred.secret does not exist; try `fediplay register`')
if not path.exists(clientcred):
click.echo(clientcred + ' does not exist; try `fediplay register`')
sys.exit(1)
if not path.exists('usercred.secret'):
click.echo('usercred.secret does not exist; try `fediplay login`')
if not path.exists(usercred):
click.echo(usercred + ' does not exist; try `fediplay login`')
sys.exit(1)
mastodon.stream(api_base_url)
mastodon.stream(instance, clientcred, usercred)

View file

@ -4,6 +4,7 @@ from os import getenv
from dotenv import load_dotenv, find_dotenv
def no_check_certificate():
'''Returns whether fediplay should check TLS certificates.'''

View file

@ -11,6 +11,12 @@ from fediplay.queue import Queue
Mastodon = mastodon.Mastodon
def api_base_url(instance):
'''Create an API base url from an instance name.'''
return 'https://' + instance
class StreamListener(mastodon.StreamListener):
'''Listens to a Mastodon timeline and adds links the given Queue.'''
@ -29,31 +35,40 @@ class StreamListener(mastodon.StreamListener):
except DownloadError:
pass
def register(api_base_url):
def register(instance, clientcred):
'''Register fediplay to a Mastodon server and save the client credentials.'''
old_umask = umask(0o77)
saved_umask = umask(0o77)
Mastodon.create_app('fediplay',
scopes=['read'],
api_base_url=api_base_url,
to_file='clientcred.secret')
umask(old_umask)
api_base_url=api_base_url(instance),
to_file=clientcred)
umask(saved_umask)
def login(client, grant_code):
def build_client(instance, clientcred, usercred=None):
'''Builds a Mastodon client.'''
return Mastodon(client_id=clientcred, access_token=usercred, api_base_url=api_base_url(instance))
def get_auth_request_url(client):
'''Gets an authorization request URL from a Mastodon instance.'''
return client.auth_request_url(scopes=['read'])
def login(client, grant_code, usercred):
'''Log in to a Mastodon server and save the user credentials.'''
old_umask = umask(0o77)
client.log_in(code=grant_code, scopes=['read'], to_file='usercred.secret')
umask(old_umask)
saved_umask = umask(0o77)
client.log_in(code=grant_code, scopes=['read'], to_file=usercred)
umask(saved_umask)
def stream(api_base_url):
def stream(instance, clientcred, usercred):
'''Stream statuses and add them to a queue.'''
client = Mastodon(client_id='clientcred.secret',
access_token='usercred.secret',
api_base_url=api_base_url)
url = api_base_url(instance)
client = Mastodon(client_id=clientcred, access_token=usercred, api_base_url=url)
listener = StreamListener(Queue())
click.echo('==> Streaming from {}'.format(api_base_url))
click.echo('==> Streaming from {}'.format(url))
client.stream_user(listener)
def extract_tags(toot):

View file

@ -3,6 +3,7 @@ from os import environ
from fediplay.mastodon import extract_links
from fediplay.queue import build_play_command
def test_extract_links():
toot = {
'content': "<p><a href=\"https://cybre.space/tags/nowplaying\" class=\"mention hashtag\" rel=\"tag\">#<span>nowplaying</span></a> <a href=\"https://cybre.space/tags/fediplay\" class=\"mention hashtag\" rel=\"tag\">#<span>fediplay</span></a> Grimes ft. Janelle Mon\u00e1e - Venus Fly <a href=\"https://www.youtube.com/watch?v=eTLTXDHrgtw\" rel=\"nofollow noopener\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"ellipsis\">youtube.com/watch?v=eTLTXDHrgt</span><span class=\"invisible\">w</span></a></p>"