aboutsummaryrefslogtreecommitdiff
path: root/app/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/main.py')
-rw-r--r--app/main.py56
1 files changed, 54 insertions, 2 deletions
diff --git a/app/main.py b/app/main.py
index 9fbdf19..46f4424 100644
--- a/app/main.py
+++ b/app/main.py
@@ -7,6 +7,7 @@ from aiohttp import web
import socketio
import logging
import json
+import pathlib
from ytdl import DownloadQueueNotifier, DownloadQueue
@@ -16,6 +17,8 @@ class Config:
_DEFAULTS = {
'DOWNLOAD_DIR': '.',
'AUDIO_DOWNLOAD_DIR': '%%DOWNLOAD_DIR',
+ 'CUSTOM_DIRS': 'true',
+ 'CREATE_CUSTOM_DIRS': 'true',
'STATE_DIR': '.',
'URL_PREFIX': '',
'OUTPUT_TEMPLATE': '%(title)s.%(ext)s',
@@ -23,12 +26,19 @@ class Config:
'YTDL_OPTIONS': '{}',
}
+ _BOOLEAN = ('CUSTOM_DIRS', 'CREATE_CUSTOM_DIRS')
+
def __init__(self):
for k, v in self._DEFAULTS.items():
setattr(self, k, os.environ[k] if k in os.environ else v)
for k, v in self.__dict__.items():
if v.startswith('%%'):
setattr(self, k, getattr(self, v[2:]))
+ if k in self._BOOLEAN:
+ if v not in ('true', 'false', 'True', 'False', 'on', 'off', '1', '0'):
+ log.error(f'Environment variable "{k}" is set to a non-boolean value "{v}"')
+ sys.exit(1)
+ setattr(self, k, v in ('true', 'True', 'on', '1'))
if not self.URL_PREFIX.endswith('/'):
self.URL_PREFIX += '/'
try:
@@ -80,7 +90,8 @@ async def add(request):
if not url or not quality:
raise web.HTTPBadRequest()
format = post.get('format')
- status = await dqueue.add(url, quality, format)
+ folder = post.get('folder')
+ status = await dqueue.add(url, quality, format, folder)
return web.Response(text=serializer.encode(status))
@routes.post(config.URL_PREFIX + 'delete')
@@ -96,6 +107,40 @@ async def delete(request):
@sio.event
async def connect(sid, environ):
await sio.emit('all', serializer.encode(dqueue.get()), to=sid)
+ await sio.emit('configuration', serializer.encode(config), to=sid)
+ if config.CUSTOM_DIRS:
+ await sio.emit('custom_dirs', serializer.encode(get_custom_dirs()), to=sid)
+
+def get_custom_dirs():
+ def recursive_dirs(base):
+ path = pathlib.Path(base)
+
+ # Converts PosixPath object to string, and remove base/ prefix
+ def convert(p):
+ s = str(p)
+ if s.startswith(base):
+ s = s[len(base):]
+
+ if s.startswith('/'):
+ s = s[1:]
+
+ return s
+
+ # Recursively lists all subdirectories of DOWNLOAD_DIR
+ dirs = list(filter(None, map(convert, path.glob('**'))))
+
+ return dirs
+
+ download_dir = recursive_dirs(config.DOWNLOAD_DIR)
+
+ audio_download_dir = download_dir
+ if config.DOWNLOAD_DIR != config.AUDIO_DOWNLOAD_DIR:
+ audio_download_dir = recursive_dirs(config.AUDIO_DOWNLOAD_DIR)
+
+ return {
+ "download_dir": download_dir,
+ "audio_download_dir": audio_download_dir
+ }
@routes.get(config.URL_PREFIX)
def index(request):
@@ -112,8 +157,15 @@ if config.URL_PREFIX != '/':
routes.static(config.URL_PREFIX + 'favicon/', 'favicon')
routes.static(config.URL_PREFIX + 'download/', config.DOWNLOAD_DIR)
+routes.static(config.URL_PREFIX + 'audio_download/', config.AUDIO_DOWNLOAD_DIR)
routes.static(config.URL_PREFIX, 'ui/dist/metube')
-app.add_routes(routes)
+try:
+ app.add_routes(routes)
+except ValueError as e:
+ if 'ui/dist/metube' in str(e):
+ raise RuntimeError('Could not find the frontend UI static assets. Please run `node_modules/.bin/ng build` inside the ui folder') from e
+ raise e
+
# https://github.com/aio-libs/aiohttp/pull/4615 waiting for release
bgstack15