when item download fails, trigger item_error event

This commit is contained in:
arabcoders 2025-06-19 19:02:44 +03:00
parent 86da6c2b06
commit 44226c9a04
4 changed files with 21 additions and 7 deletions

View file

@ -1,3 +1,4 @@
import asyncio
import copy import copy
import json import json
import logging 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): if (key and self.dict[i].info._id == key) or (url and self.dict[i].info.url == url):
return self.dict[i] return self.dict[i]
msg = f"{key=} or {url=} not found." msg: str = f"{key=} or {url=} not found."
raise KeyError(msg) raise KeyError(msg)
def get_by_id(self, id: str) -> Download | None: def get_by_id(self, id: str) -> Download | None:
@ -82,6 +83,11 @@ class DataStore:
return items return items
def put(self, value: Download) -> Download: 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.dict.update({value.info._id: value})
self._update_store_item(self.type, value.info) self._update_store_item(self.type, value.info)
@ -121,7 +127,7 @@ class DataStore:
ON CONFLICT DO UPDATE SET "type" = ?, "url" = ?, "data" = ?, created_at = ? ON CONFLICT DO UPDATE SET "type" = ?, "url" = ?, "data" = ?, created_at = ?
""" """
stored = copy.deepcopy(item) stored: ItemDTO = copy.deepcopy(item)
if hasattr(stored, "datetime"): if hasattr(stored, "datetime"):
try: try:

View file

@ -8,6 +8,7 @@ from datetime import UTC, datetime, timedelta
from email.utils import formatdate from email.utils import formatdate
from pathlib import Path from pathlib import Path
from sqlite3 import Connection from sqlite3 import Connection
from typing import TYPE_CHECKING
import yt_dlp import yt_dlp
from aiohttp import web from aiohttp import web
@ -38,6 +39,9 @@ from .Utils import (
) )
from .YTDLPOpts import YTDLPOpts from .YTDLPOpts import YTDLPOpts
if TYPE_CHECKING:
from app.library.Presets import Preset
LOG = logging.getLogger("DownloadQueue") LOG = logging.getLogger("DownloadQueue")
@ -490,7 +494,7 @@ class DownloadQueue(metaclass=Singleton):
{ "status": "text" } { "status": "text" }
""" """
_preset = Presets.get_instance().get(item.preset) _preset: Preset | None = Presets.get_instance().get(item.preset)
if item.has_cli(): if item.has_cli():
try: try:
@ -520,9 +524,9 @@ class DownloadQueue(metaclass=Singleton):
already.add(item.url) already.add(item.url)
try: try:
logs = [] logs: list = []
yt_conf = { yt_conf: dict = {
"callback": { "callback": {
"func": lambda _, msg: logs.append(msg), "func": lambda _, msg: logs.append(msg),
"level": logging.WARNING, "level": logging.WARNING,
@ -542,7 +546,7 @@ class DownloadQueue(metaclass=Singleton):
await self._notify.emit(Events.LOG_WARNING, data=event_warning(message)) await self._notify.emit(Events.LOG_WARNING, data=event_warning(message))
return {"status": "error", "msg": message} return {"status": "error", "msg": message}
started = time.perf_counter() started: float = time.perf_counter()
if item.cookies: if item.cookies:
try: try:

View file

@ -109,6 +109,7 @@ class Events:
INITIAL_DATA = "initial_data" INITIAL_DATA = "initial_data"
ITEM_DELETE = "item_delete" ITEM_DELETE = "item_delete"
ITEM_CANCEL = "item_cancel" ITEM_CANCEL = "item_cancel"
ITEM_ERROR = "item_error"
STATUS = "status" STATUS = "status"
CLI_CLOSE = "cli_close" CLI_CLOSE = "cli_close"
CLI_OUTPUT = "cli_output" CLI_OUTPUT = "cli_output"

View file

@ -235,6 +235,9 @@ class ItemDTO:
def get_id(self) -> str: def get_id(self) -> str:
return self._id return self._id
def name(self) -> str:
return f'id="{self.id}", title="{self.title}"'
@staticmethod @staticmethod
def removed_fields() -> tuple: def removed_fields() -> tuple:
"""Fields that once existed but are no longer used.""" """Fields that once existed but are no longer used."""
@ -247,5 +250,5 @@ class ItemDTO:
"output_template", "output_template",
"output_template_chapter", "output_template_chapter",
"config", "config",
"temp_path" "temp_path",
) )