diff --git a/.env b/.env index e663427..9200bba 100644 --- a/.env +++ b/.env @@ -1,4 +1,3 @@ -FEDIPLAY_API_BASE_URL="https://cybre.space" #FEDIPLAY_NO_CHECK_CERTIFICATE=1 FEDIPLAY_PLAY_COMMAND="ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}" -#FEDIPLAY_PLAY_COMMAND="afplay {filename}" \ No newline at end of file +#FEDIPLAY_PLAY_COMMAND="afplay {filename}" diff --git a/Pipfile b/Pipfile index 24478b1..b025db2 100644 --- a/Pipfile +++ b/Pipfile @@ -9,9 +9,10 @@ name = "pypi" cssselect = "*" lxml = "*" -"Mastodon.py" = "*" -youtube_dl = "*" +"mastodon.py" = "*" +youtube-dl = "*" python-dotenv = "*" +click = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 37e7be1..3a2d2c8 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "f73fe1eabc45c7e9153c6512b0a96453287660e8a6c78539eb32a539211d71eb" + "sha256": "6fc6e44d60c327acc186d1cc98bbe895ad8f82134ccbc02093aed5acd8a11823" }, "pipfile-spec": 6, "requires": { @@ -30,6 +30,14 @@ ], "version": "==3.0.4" }, + "click": { + "hashes": [ + "sha256:29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d", + "sha256:f15516df478d5a56180fbf80e68f206010e6d160fc39fa508b65e035fd75130b" + ], + "index": "pypi", + "version": "==6.7" + }, "cssselect": { "hashes": [ "sha256:066d8bc5229af09617e24b3ca4d52f1f9092d9e061931f4184cd572885c23204", diff --git a/fediplay/cli.py b/fediplay/cli.py new file mode 100644 index 0000000..8cd41b2 --- /dev/null +++ b/fediplay/cli.py @@ -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) diff --git a/fediplay/env.py b/fediplay/env.py index 9eb14bb..72f48e4 100644 --- a/fediplay/env.py +++ b/fediplay/env.py @@ -4,9 +4,6 @@ from os import getenv from dotenv import load_dotenv, find_dotenv -def api_base_url(): - return getenv('FEDIPLAY_API_BASE_URL') - def no_check_certificate(): return bool(getenv('FEDIPLAY_NO_CHECK_CERTIFICATE')) diff --git a/fediplay/main.py b/fediplay/main.py deleted file mode 100644 index a1eb788..0000000 --- a/fediplay/main.py +++ /dev/null @@ -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) diff --git a/fediplay/mastodon.py b/fediplay/mastodon.py index 69af7f4..93a2212 100644 --- a/fediplay/mastodon.py +++ b/fediplay/mastodon.py @@ -2,6 +2,7 @@ from os import umask +import click from lxml.etree import HTML # pylint: disable=no-name-in-module import mastodon from youtube_dl.utils import DownloadError @@ -22,7 +23,7 @@ class StreamListener(mastodon.StreamListener): links = extract_links(status) for link in links: try: - print('==> Trying', link) + click.echo('==> Trying {}'.format(link)) self.queue.add(link) return except DownloadError: @@ -48,7 +49,7 @@ def stream(api_base_url): client = Mastodon(client_id='clientcred.secret', access_token='usercred.secret', api_base_url=api_base_url) listener = StreamListener(Queue()) - print('==> Streaming from', api_base_url) + click.echo('==> Streaming from {}'.format(api_base_url)) client.stream_user(listener) def extract_tags(toot): diff --git a/fediplay/queue.py b/fediplay/queue.py index dc17c66..93fa333 100644 --- a/fediplay/queue.py +++ b/fediplay/queue.py @@ -4,6 +4,7 @@ import shlex from subprocess import run from threading import Thread, Lock +import click from youtube_dl import YoutubeDL import fediplay.env as env @@ -32,11 +33,10 @@ class Queue(object): self.playing = True def _run_thread(filename, cb_complete): - print('==> Playing', filename) play_command = build_play_command(filename) - print('[executing]', play_command) + click.echo('==> Playing {} with {}'.format(filename, play_command)) run(play_command, shell=True) - print('==> Playback complete') + click.echo('==> Playback complete') cb_complete() thread = Thread(target=_run_thread, args=(filename, cb_complete)) diff --git a/setup.py b/setup.py index fd6da03..75f6395 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( ], entry_points={ 'console_scripts': [ - 'fediplay = fediplay.main:main' + 'fediplay = fediplay.cli:cli' ] } )