From 44226c9a04f0d5ceaef1930616ab2124153bf688 Mon Sep 17 00:00:00 2001 From: arabcoders Date: Thu, 19 Jun 2025 19:02:44 +0300 Subject: [PATCH] when item download fails, trigger item_error event --- app/library/DataStore.py | 10 ++++++++-- app/library/DownloadQueue.py | 12 ++++++++---- app/library/Events.py | 1 + app/library/ItemDTO.py | 5 ++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/library/DataStore.py b/app/library/DataStore.py index cce7766a..96c64a3d 100644 --- a/app/library/DataStore.py +++ b/app/library/DataStore.py @@ -1,3 +1,4 @@ +import asyncio import copy import json import logging @@ -54,7 +55,7 @@ class DataStore: if (key and self.dict[i].info._id == key) or (url and self.dict[i].info.url == url): return self.dict[i] - msg = f"{key=} or {url=} not found." + msg: str = f"{key=} or {url=} not found." raise KeyError(msg) def get_by_id(self, id: str) -> Download | None: @@ -82,6 +83,11 @@ class DataStore: return items def put(self, value: Download) -> Download: + if "error" == value.info.status: + from app.library.Events import EventBus, Events + + asyncio.create_task(EventBus.get_instance().emit(Events.ITEM_ERROR, value.info), name="emit_item_error") + self.dict.update({value.info._id: value}) self._update_store_item(self.type, value.info) @@ -121,7 +127,7 @@ class DataStore: ON CONFLICT DO UPDATE SET "type" = ?, "url" = ?, "data" = ?, created_at = ? """ - stored = copy.deepcopy(item) + stored: ItemDTO = copy.deepcopy(item) if hasattr(stored, "datetime"): try: diff --git a/app/library/DownloadQueue.py b/app/library/DownloadQueue.py index f885e46b..60e357c6 100644 --- a/app/library/DownloadQueue.py +++ b/app/library/DownloadQueue.py @@ -8,6 +8,7 @@ from datetime import UTC, datetime, timedelta from email.utils import formatdate from pathlib import Path from sqlite3 import Connection +from typing import TYPE_CHECKING import yt_dlp from aiohttp import web @@ -38,6 +39,9 @@ from .Utils import ( ) from .YTDLPOpts import YTDLPOpts +if TYPE_CHECKING: + from app.library.Presets import Preset + LOG = logging.getLogger("DownloadQueue") @@ -490,7 +494,7 @@ class DownloadQueue(metaclass=Singleton): { "status": "text" } """ - _preset = Presets.get_instance().get(item.preset) + _preset: Preset | None = Presets.get_instance().get(item.preset) if item.has_cli(): try: @@ -520,9 +524,9 @@ class DownloadQueue(metaclass=Singleton): already.add(item.url) try: - logs = [] + logs: list = [] - yt_conf = { + yt_conf: dict = { "callback": { "func": lambda _, msg: logs.append(msg), "level": logging.WARNING, @@ -542,7 +546,7 @@ class DownloadQueue(metaclass=Singleton): await self._notify.emit(Events.LOG_WARNING, data=event_warning(message)) return {"status": "error", "msg": message} - started = time.perf_counter() + started: float = time.perf_counter() if item.cookies: try: diff --git a/app/library/Events.py b/app/library/Events.py index b07ab345..937b743e 100644 --- a/app/library/Events.py +++ b/app/library/Events.py @@ -109,6 +109,7 @@ class Events: INITIAL_DATA = "initial_data" ITEM_DELETE = "item_delete" ITEM_CANCEL = "item_cancel" + ITEM_ERROR = "item_error" STATUS = "status" CLI_CLOSE = "cli_close" CLI_OUTPUT = "cli_output" diff --git a/app/library/ItemDTO.py b/app/library/ItemDTO.py index 68c0e804..b66c54fc 100644 --- a/app/library/ItemDTO.py +++ b/app/library/ItemDTO.py @@ -235,6 +235,9 @@ class ItemDTO: def get_id(self) -> str: return self._id + def name(self) -> str: + return f'id="{self.id}", title="{self.title}"' + @staticmethod def removed_fields() -> tuple: """Fields that once existed but are no longer used.""" @@ -247,5 +250,5 @@ class ItemDTO: "output_template", "output_template_chapter", "config", - "temp_path" + "temp_path", )