diff --git a/Pipfile b/Pipfile index f4b2536..24478b1 100644 --- a/Pipfile +++ b/Pipfile @@ -9,8 +9,9 @@ name = "pypi" cssselect = "*" lxml = "*" -"mastodon.py" = "*" -youtube-dl = "*" +"Mastodon.py" = "*" +youtube_dl = "*" +python-dotenv = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 3f2d84e..37e7be1 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "f0d0df34026363f209c1bda09fac7757ccdd8e90d348cba74e5aa278850abe5d" + "sha256": "f73fe1eabc45c7e9153c6512b0a96453287660e8a6c78539eb32a539211d71eb" }, "pipfile-spec": 6, "requires": { @@ -101,6 +101,14 @@ ], "version": "==2.7.0" }, + "python-dotenv": { + "hashes": [ + "sha256:4965ed170bf51c347a89820e8050655e9c25db3837db6602e906b6d850fad85c", + "sha256:509736185257111613009974e666568a1b031b028b61b500ef1ab4ee780089d5" + ], + "index": "pypi", + "version": "==0.8.2" + }, "pytz": { "hashes": [ "sha256:07edfc3d4d2705a20a6e99d97f0c4b61c800b8232dc1c04d87e8554f130148dd", diff --git a/fediplay/env.py b/fediplay/env.py new file mode 100644 index 0000000..9eb14bb --- /dev/null +++ b/fediplay/env.py @@ -0,0 +1,17 @@ +'''Environment variable management.''' + +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')) + +def play_command(): + return (getenv('FEDIPLAY_PLAY_COMMAND') or + 'ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}') + +load_dotenv(find_dotenv()) diff --git a/fediplay/main.py b/fediplay/main.py index 1572407..a1eb788 100644 --- a/fediplay/main.py +++ b/fediplay/main.py @@ -1,16 +1,17 @@ '''Entry point for command-line interface.''' -from os import path, environ +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 = environ.get('FEDIPLAY_API_BASE_URL') + api_base_url = env.api_base_url() if not api_base_url: print('FEDIPLAY_API_BASE_URL environment variable not set') sys.exit(1) diff --git a/fediplay/queue.py b/fediplay/queue.py index 4733646..dc17c66 100644 --- a/fediplay/queue.py +++ b/fediplay/queue.py @@ -1,12 +1,13 @@ '''The play queue.''' -from os import environ import shlex from subprocess import run from threading import Thread, Lock from youtube_dl import YoutubeDL +import fediplay.env as env + class Queue(object): '''The play queue.''' @@ -64,7 +65,7 @@ class Getter(object): options = { 'format': 'mp3/mp4', - 'nocheckcertificate': 'FEDIPLAY_NO_CHECK_CERTIFICATE' in environ, + 'nocheckcertificate': env.no_check_certificate(), 'progress_hooks': [self._progress_hook] } with YoutubeDL(options) as downloader: @@ -76,8 +77,5 @@ def build_play_command(filename): '''Builds a play command for the given filename.''' escaped_filename = shlex.quote(filename) - template = environ.get( - 'FEDIPLAY_PLAY_COMMAND', - 'ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}' - ) + template = env.play_command() return template.format(filename=escaped_filename)