mirror of
https://github.com/nova-r/fediplug.git
synced 2025-01-22 08:36:29 +01:00
use click for CLI
This commit is contained in:
parent
2a683ced56
commit
62978504c1
9 changed files with 95 additions and 50 deletions
3
.env
3
.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}"
|
||||
#FEDIPLAY_PLAY_COMMAND="afplay {filename}"
|
||||
|
|
5
Pipfile
5
Pipfile
|
@ -9,9 +9,10 @@ name = "pypi"
|
|||
|
||||
cssselect = "*"
|
||||
lxml = "*"
|
||||
"Mastodon.py" = "*"
|
||||
youtube_dl = "*"
|
||||
"mastodon.py" = "*"
|
||||
youtube-dl = "*"
|
||||
python-dotenv = "*"
|
||||
click = "*"
|
||||
|
||||
|
||||
[dev-packages]
|
||||
|
|
10
Pipfile.lock
generated
10
Pipfile.lock
generated
|
@ -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",
|
||||
|
|
75
fediplay/cli.py
Normal file
75
fediplay/cli.py
Normal 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)
|
|
@ -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'))
|
||||
|
||||
|
|
|
@ -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)
|
|
@ -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):
|
||||
|
|
|
@ -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))
|
||||
|
|
2
setup.py
2
setup.py
|
@ -12,7 +12,7 @@ setup(
|
|||
],
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'fediplay = fediplay.main:main'
|
||||
'fediplay = fediplay.cli:cli'
|
||||
]
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue