fix: complete site parser source workflow
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
@@ -9,6 +10,18 @@ import aiohttp
|
||||
from .constants import PLATFORM_SITE, PLATFORM_VK
|
||||
|
||||
|
||||
def json_object(value: Any) -> dict[str, Any]:
|
||||
if isinstance(value, dict):
|
||||
return dict(value)
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
parsed = json.loads(value)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
return parsed if isinstance(parsed, dict) else {}
|
||||
return {}
|
||||
|
||||
|
||||
@dataclass
|
||||
class SourceMedia:
|
||||
url: str
|
||||
@@ -42,6 +55,12 @@ def validate_source_config(platform: str, config: dict[str, Any]) -> None:
|
||||
raise ValueError("max_items должен быть целым числом") from exc
|
||||
if not 1 <= max_items <= 100:
|
||||
raise ValueError("max_items должен быть от 1 до 100")
|
||||
try:
|
||||
interval_minutes = int(config.get("interval_minutes", 30))
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValueError("interval_minutes должен быть целым числом") from exc
|
||||
if not 1 <= interval_minutes <= 10080:
|
||||
raise ValueError("interval_minutes должен быть от 1 до 10080")
|
||||
|
||||
|
||||
def _posted_at(value: Any) -> datetime:
|
||||
@@ -72,9 +91,9 @@ class SiteParserClient:
|
||||
async def fetch(self, source: dict) -> tuple[list[SourceItem], dict[str, Any] | None]:
|
||||
if not self.base_url or not self.token:
|
||||
raise RuntimeError("Site Parser URL или токен не настроены")
|
||||
config = dict(source.get("settings_json") or {})
|
||||
config = json_object(source.get("settings_json"))
|
||||
validate_source_config(PLATFORM_SITE, config)
|
||||
runtime_state = dict(source.get("runtime_state_json") or {})
|
||||
runtime_state = json_object(source.get("runtime_state_json"))
|
||||
payload = {
|
||||
"url": source["url"],
|
||||
"config": config,
|
||||
|
||||
Reference in New Issue
Block a user