From 1be7fa445416438fc6229f683ef5e038e315d7b4 Mon Sep 17 00:00:00 2001 From: Matt Behrens Date: Fri, 26 Jan 2018 20:49:45 -0500 Subject: [PATCH] configurable play command --- .env | 4 +++- fediplay.py | 13 ++++++++++++- test_fediplay.py | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 2404992..e663427 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ -FEDIPLAY_API_BASE_URL=https://cybre.space +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 diff --git a/fediplay.py b/fediplay.py index 118c0f8..24bbac1 100644 --- a/fediplay.py +++ b/fediplay.py @@ -1,4 +1,5 @@ from os import environ, umask +import shlex from subprocess import run from threading import Thread, Lock @@ -26,7 +27,9 @@ class Queue(object): def run_thread(filename, cb_complete): print('==> Playing', filename) - run(['ffplay', '-v', '0', '-nostats', '-hide_banner', '-autoexit', '-nodisp', filename]) + play_command = build_play_command(filename) + print('[executing]', play_command) + run(play_command, shell=True) print('==> Playback complete') cb_complete() @@ -97,6 +100,14 @@ def extract_links(toot): all_links = html.cssselect('a') return [link.attrib['href'] for link in all_links if not link_is_internal(link)] +def build_play_command(filename): + escaped_filename = shlex.quote(filename) + template = environ.get( + 'FEDIPLAY_PLAY_COMMAND', + 'ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}' + ) + return template.format(filename=escaped_filename) + def main(): from getpass import getpass from os import path diff --git a/test_fediplay.py b/test_fediplay.py index cee4dc7..ca47a26 100644 --- a/test_fediplay.py +++ b/test_fediplay.py @@ -1,3 +1,5 @@ +from os import environ + import fediplay def test_extract_links(): @@ -6,3 +8,13 @@ def test_extract_links(): } urls = fediplay.extract_links(toot) assert urls == ['https://www.youtube.com/watch?v=eTLTXDHrgtw'] + +def test_build_play_command_default(): + environ.pop('FEDIPLAY_PLAY_COMMAND') + play_command = fediplay.build_play_command('Awesome Music.mp3') + assert play_command == 'ffplay -v 0 -nostats -hide_banner -autoexit -nodisp \'Awesome Music.mp3\'' + +def test_build_play_command_specified(): + environ.update(FEDIPLAY_PLAY_COMMAND='afplay {filename}') + play_command = fediplay.build_play_command('Awesome Music.mp3') + assert play_command == 'afplay \'Awesome Music.mp3\''