diff --git a/app/ytdl.py b/app/ytdl.py index d4955f3..5be7a29 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -33,6 +33,7 @@ class DownloadQueueNotifier: class DownloadInfo: def __init__(self, id, title, url, quality, format, folder, custom_name_prefix, error): self.id = id if len(custom_name_prefix) == 0 else f'{custom_name_prefix}.{id}' + self.id = f'{self.id}.{format}' self.title = title if len(custom_name_prefix) == 0 else f'{custom_name_prefix}.{title}' self.url = url self.quality = quality @@ -51,8 +52,8 @@ class Download: def __init__(self, download_dir, temp_dir, output_template, output_template_chapter, quality, format, ytdl_opts, info): self.download_dir = download_dir self.temp_dir = temp_dir - self.output_template = output_template - self.output_template_chapter = output_template_chapter + self.output_template = self._add_format_identifier(format, output_template) + self.output_template_chapter = self._add_format_identifier(format, output_template_chapter) self.format = get_format(format, quality) self.ytdl_opts = get_opts(format, quality, ytdl_opts) if "impersonate" in self.ytdl_opts: @@ -139,6 +140,8 @@ class Download: if self.status_queue is not None: self.status_queue.put(None) + self._delete_format_identifier() + def running(self): try: return self.proc is not None and self.proc.is_alive() @@ -174,6 +177,49 @@ class Download: self.info.eta = status.get('eta') log.info(f"Updating status for {self.info.title}: {status}") await self.notifier.updated(self.info) + + def _add_format_identifier(self, identifier, template): + # Preventing the post-processing of YT-DLP from deleting the intermediate file which was download before. + return f'{identifier}_{template}' + + def _delete_format_identifier(self): + # Delete the identifier in the file name after the post-processing is complete. + if self.canceled or self.info.status != 'finished' or not hasattr(self.info,'filename'): + return + + try: + filename = re.sub(r'^\w+_', '', self.info.filename) + filepath_idt = os.path.join(self.download_dir, self.info.filename) + filepath = os.path.join(self.download_dir, filename) + if os.path.exists(filepath): + os.remove(filepath) + os.rename(filepath_idt, filepath) + log.info(f"Renamed file '{filepath_idt}' to '{filepath}'") + except PermissionError as e: + log.warning(f"Error deleting old file '{filepath}': {e} ") + return + except Exception as e: + log.warning(f"Error renaming file '{filepath_idt}': {e} ") + return + + self.info.filename = filename + + def delete_tmpfile(self): + if not self.tmpfilename or not self.download_dir: + return + if not os.path.isdir(self.download_dir): + return + + tmpfilename = os.path.basename(self.tmpfilename) + def is_tmpfile(filename): + return filename.startswith(tmpfilename) + + try: + tmpfiles = filter(is_tmpfile, os.listdir(self.download_dir)) + for tmpfile in tmpfiles: + os.remove(os.path.join(self.download_dir, tmpfile)) + except Exception as e: + log.warning(f"Error deleting temporary files: {e}") class PersistentQueue: def __init__(self, path): @@ -203,7 +249,7 @@ class PersistentQueue: return sorted(shelf.items(), key=lambda item: item[1].timestamp) def put(self, value): - key = value.info.url + key = value.info.id self.dict[key] = value with shelve.open(self.path, 'w') as shelf: shelf[key] = value.info @@ -278,17 +324,13 @@ class DownloadQueue: def _post_download_cleanup(self, download): if download.info.status != 'finished': - if download.tmpfilename and os.path.isfile(download.tmpfilename): - try: - os.remove(download.tmpfilename) - except: - pass + download.delete_tmpfile() download.info.status = 'error' download.close() - if self.queue.exists(download.info.url): - self.queue.delete(download.info.url) + if self.queue.exists(download.info.id): + self.queue.delete(download.info.id) if download.canceled: - asyncio.create_task(self.notifier.canceled(download.info.url)) + asyncio.create_task(self.notifier.canceled(download.info.id)) else: self.done.put(download) asyncio.create_task(self.notifier.completed(download.info)) @@ -361,9 +403,9 @@ class DownloadQueue: return {'status': 'ok'} elif etype == 'video' or (etype.startswith('url') and 'id' in entry and 'title' in entry): log.debug('Processing as a video') - key = entry.get('webpage_url') or entry['url'] - if not self.queue.exists(key): - dl = DownloadInfo(entry['id'], entry.get('title') or entry['id'], key, quality, format, folder, custom_name_prefix, error) + url = entry.get('webpage_url') or entry['url'] + dl = DownloadInfo(entry['id'], entry.get('title') or entry['id'], url, quality, format, folder, custom_name_prefix, error) + if not self.queue.exists(dl.id): dldirectory, error_message = self.__calc_download_path(quality, format, folder) if error_message is not None: return error_message diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index a536154..1b3d9f1 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -290,7 +290,7 @@