use click for CLI

This commit is contained in:
Matt Behrens 2018-04-02 20:09:25 -04:00
parent 2a683ced56
commit 62978504c1
9 changed files with 95 additions and 50 deletions

3
.env
View file

@ -1,4 +1,3 @@
FEDIPLAY_API_BASE_URL="https://cybre.space"
#FEDIPLAY_NO_CHECK_CERTIFICATE=1 #FEDIPLAY_NO_CHECK_CERTIFICATE=1
FEDIPLAY_PLAY_COMMAND="ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}" FEDIPLAY_PLAY_COMMAND="ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}"
#FEDIPLAY_PLAY_COMMAND="afplay {filename}" #FEDIPLAY_PLAY_COMMAND="afplay {filename}"

View file

@ -9,9 +9,10 @@ name = "pypi"
cssselect = "*" cssselect = "*"
lxml = "*" lxml = "*"
"Mastodon.py" = "*" "mastodon.py" = "*"
youtube_dl = "*" youtube-dl = "*"
python-dotenv = "*" python-dotenv = "*"
click = "*"
[dev-packages] [dev-packages]

10
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "f73fe1eabc45c7e9153c6512b0a96453287660e8a6c78539eb32a539211d71eb" "sha256": "6fc6e44d60c327acc186d1cc98bbe895ad8f82134ccbc02093aed5acd8a11823"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -30,6 +30,14 @@
], ],
"version": "==3.0.4" "version": "==3.0.4"
}, },
"click": {
"hashes": [
"sha256:29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d",
"sha256:f15516df478d5a56180fbf80e68f206010e6d160fc39fa508b65e035fd75130b"
],
"index": "pypi",
"version": "==6.7"
},
"cssselect": { "cssselect": {
"hashes": [ "hashes": [
"sha256:066d8bc5229af09617e24b3ca4d52f1f9092d9e061931f4184cd572885c23204", "sha256:066d8bc5229af09617e24b3ca4d52f1f9092d9e061931f4184cd572885c23204",

75
fediplay/cli.py Normal file
View file

@ -0,0 +1,75 @@
'''Entry point for command-line interface.'''
from os import path
import sys
import click
from mastodon import Mastodon
import fediplay.env as env
import fediplay.mastodon as mastodon
def api_base_url_from_instance(instance):
return 'https://' + instance
@click.group()
def cli():
'''Command-line interface group.'''
pass
@cli.command()
@click.argument('instance')
def register(instance):
'''Register fediplay to the instance.'''
api_base_url = api_base_url_from_instance(instance)
if path.exists('clientcred.secret'):
click.echo('clientcred.secret already exists')
sys.exit(1)
mastodon.register(api_base_url)
@cli.command()
@click.argument('instance')
def login(instance):
'''Log in to the instance.'''
api_base_url = api_base_url_from_instance(instance)
if path.exists('usercred.secret'):
click.echo('usercred.secret already exists')
sys.exit(1)
if not path.exists('clientcred.secret'):
click.echo('clientcred.secret does not exist; try `fediplay register`')
sys.exit(1)
client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url)
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())
click.echo('')
grant_code = input('Code: ')
mastodon.login(client, grant_code)
@cli.command()
@click.argument('instance')
def stream(instance):
'''Stream music from the instance.'''
api_base_url = api_base_url_from_instance(instance)
if not path.exists('clientcred.secret'):
click.echo('clientcred.secret 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`')
sys.exit(1)
mastodon.stream(api_base_url)

View file

@ -4,9 +4,6 @@ from os import getenv
from dotenv import load_dotenv, find_dotenv from dotenv import load_dotenv, find_dotenv
def api_base_url():
return getenv('FEDIPLAY_API_BASE_URL')
def no_check_certificate(): def no_check_certificate():
return bool(getenv('FEDIPLAY_NO_CHECK_CERTIFICATE')) return bool(getenv('FEDIPLAY_NO_CHECK_CERTIFICATE'))

View file

@ -1,36 +0,0 @@
'''Entry point for command-line interface.'''
from os import path
import sys
from mastodon import Mastodon
import fediplay.env as env
from fediplay.mastodon import stream, register, login
def main():
'''Run fediplay command-line interface.'''
api_base_url = env.api_base_url()
if not api_base_url:
print('FEDIPLAY_API_BASE_URL environment variable not set')
sys.exit(1)
if not path.exists('clientcred.secret'):
print('==> No clientcred.secret; registering application')
register(api_base_url)
if not path.exists('usercred.secret'):
print('==> No usercred.secret; logging in')
client = Mastodon(client_id='clientcred.secret', api_base_url=api_base_url)
print('Open this page in your browser and follow the instructions.')
print('Paste the code here.')
print('')
print(client.auth_request_url())
print('')
grant_code = input('Code: ')
login(client, grant_code)
stream(api_base_url)

View file

@ -2,6 +2,7 @@
from os import umask from os import umask
import click
from lxml.etree import HTML # pylint: disable=no-name-in-module from lxml.etree import HTML # pylint: disable=no-name-in-module
import mastodon import mastodon
from youtube_dl.utils import DownloadError from youtube_dl.utils import DownloadError
@ -22,7 +23,7 @@ class StreamListener(mastodon.StreamListener):
links = extract_links(status) links = extract_links(status)
for link in links: for link in links:
try: try:
print('==> Trying', link) click.echo('==> Trying {}'.format(link))
self.queue.add(link) self.queue.add(link)
return return
except DownloadError: except DownloadError:
@ -48,7 +49,7 @@ def stream(api_base_url):
client = Mastodon(client_id='clientcred.secret', access_token='usercred.secret', client = Mastodon(client_id='clientcred.secret', access_token='usercred.secret',
api_base_url=api_base_url) api_base_url=api_base_url)
listener = StreamListener(Queue()) listener = StreamListener(Queue())
print('==> Streaming from', api_base_url) click.echo('==> Streaming from {}'.format(api_base_url))
client.stream_user(listener) client.stream_user(listener)
def extract_tags(toot): def extract_tags(toot):

View file

@ -4,6 +4,7 @@ import shlex
from subprocess import run from subprocess import run
from threading import Thread, Lock from threading import Thread, Lock
import click
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
import fediplay.env as env import fediplay.env as env
@ -32,11 +33,10 @@ class Queue(object):
self.playing = True self.playing = True
def _run_thread(filename, cb_complete): def _run_thread(filename, cb_complete):
print('==> Playing', filename)
play_command = build_play_command(filename) play_command = build_play_command(filename)
print('[executing]', play_command) click.echo('==> Playing {} with {}'.format(filename, play_command))
run(play_command, shell=True) run(play_command, shell=True)
print('==> Playback complete') click.echo('==> Playback complete')
cb_complete() cb_complete()
thread = Thread(target=_run_thread, args=(filename, cb_complete)) thread = Thread(target=_run_thread, args=(filename, cb_complete))

View file

@ -12,7 +12,7 @@ setup(
], ],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'fediplay = fediplay.main:main' 'fediplay = fediplay.cli:cli'
] ]
} }
) )