diff options
author | Alex <alexta69@gmail.com> | 2022-09-30 09:10:57 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-30 09:10:57 +0300 |
commit | b7c8c80362fdfb9a465042d8027c0af9063ff62a (patch) | |
tree | c8d195e229a7d9b9c98ffe71881fafaf96637320 /app/ytdl.py | |
parent | upgraded yt-dlp (diff) | |
parent | Merge branch 'master' of https://github.com/alexta69/metube into custom-downl... (diff) | |
download | metube-b7c8c80362fdfb9a465042d8027c0af9063ff62a.tar.gz metube-b7c8c80362fdfb9a465042d8027c0af9063ff62a.tar.bz2 metube-b7c8c80362fdfb9a465042d8027c0af9063ff62a.zip |
Merge pull request #177 from jwoglom/custom-download-folder
Custom download folders (fixes #129)
Diffstat (limited to 'app/ytdl.py')
-rw-r--r-- | app/ytdl.py | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/app/ytdl.py b/app/ytdl.py index 338c6e4..0bafc91 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -28,10 +28,11 @@ class DownloadQueueNotifier: raise NotImplementedError
class DownloadInfo:
- def __init__(self, id, title, url, quality, format):
+ def __init__(self, id, title, url, quality, format, folder):
self.id, self.title, self.url = id, title, url
self.quality = quality
self.format = format
+ self.folder = folder
self.status = self.msg = self.percent = self.speed = self.eta = None
self.filename = None
self.timestamp = time.time_ns()
@@ -197,7 +198,7 @@ class DownloadQueue: async def __import_queue(self):
for k, v in self.queue.saved_items():
- await self.add(v.url, v.quality, v.format)
+ await self.add(v.url, v.quality, v.format, folder=v.folder)
async def initialize(self):
self.event = asyncio.Event()
@@ -212,7 +213,7 @@ class DownloadQueue: **self.config.YTDL_OPTIONS,
}).extract_info(url, download=False)
- async def __add_entry(self, entry, quality, format, already):
+ async def __add_entry(self, entry, quality, format, folder, already):
etype = entry.get('_type') or 'video'
if etype == 'playlist':
entries = entry['entries']
@@ -225,14 +226,28 @@ class DownloadQueue: for property in ("id", "title", "uploader", "uploader_id"):
if property in entry:
etr[f"playlist_{property}"] = entry[property]
- results.append(await self.__add_entry(etr, quality, format, already))
+ results.append(await self.__add_entry(etr, quality, format, folder, already))
if any(res['status'] == 'error' for res in results):
return {'status': 'error', 'msg': ', '.join(res['msg'] for res in results if res['status'] == 'error' and 'msg' in res)}
return {'status': 'ok'}
elif etype == 'video' or etype.startswith('url') and 'id' in entry and 'title' in entry:
if not self.queue.exists(entry['id']):
- dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'], quality, format)
- dldirectory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR
+ dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'], quality, format, folder)
+ # Keep consistent with frontend
+ base_directory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR
+ if folder:
+ if self.config.CUSTOM_DIRS != 'true':
+ return {'status': 'error', 'msg': f'A folder for the download was specified but CUSTOM_DIRS is not true in the configuration.'}
+ dldirectory = os.path.realpath(os.path.join(base_directory, folder))
+ real_base_directory = os.path.realpath(base_directory)
+ if not dldirectory.startswith(real_base_directory):
+ return {'status': 'error', 'msg': f'Folder "{folder}" must resolve inside the base download directory "{real_base_directory}"'}
+ if not os.path.isdir(dldirectory):
+ if self.config.CREATE_CUSTOM_DIRS != 'true':
+ return {'status': 'error', 'msg': f'Folder "{folder}" for download does not exist inside base directory "{real_base_directory}", and CREATE_CUSTOM_DIRS is not true in the configuration.'}
+ os.makedirs(dldirectory, exist_ok=True)
+ else:
+ dldirectory = base_directory
output = self.config.OUTPUT_TEMPLATE
output_chapter = self.config.OUTPUT_TEMPLATE_CHAPTER
for property, value in entry.items():
@@ -243,11 +258,11 @@ class DownloadQueue: await self.notifier.added(dl)
return {'status': 'ok'}
elif etype.startswith('url'):
- return await self.add(entry['url'], quality, format, already)
+ return await self.add(entry['url'], quality, format, folder, already)
return {'status': 'error', 'msg': f'Unsupported resource "{etype}"'}
- async def add(self, url, quality, format, already=None):
- log.info(f'adding {url}')
+ async def add(self, url, quality, format, folder, already=None):
+ log.info(f'adding {url}: {quality=} {format=} {already=} {folder=}')
already = set() if already is None else already
if url in already:
log.info('recursion detected, skipping')
@@ -258,7 +273,7 @@ class DownloadQueue: entry = await asyncio.get_running_loop().run_in_executor(None, self.__extract_info, url)
except yt_dlp.utils.YoutubeDLError as exc:
return {'status': 'error', 'msg': str(exc)}
- return await self.__add_entry(entry, quality, format, already)
+ return await self.__add_entry(entry, quality, format, folder, already)
async def cancel(self, ids):
for id in ids:
|