58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import json
|
|
import random
|
|
import re
|
|
import time
|
|
from pathlib import Path
|
|
from typing import Any, Iterable
|
|
|
|
|
|
# Файловые утилиты
|
|
def save_to_json(data: Any, filename: str | Path) -> None:
|
|
path = Path(filename)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
|
|
# Небольшая случайная пауза между запросами
|
|
def short_sleep(a: float = 0.10, b: float = 0.35) -> None:
|
|
time.sleep(random.uniform(a, b))
|
|
|
|
|
|
# Маскирование персональных данных
|
|
def mask_email(email: str) -> str:
|
|
if "@" not in email:
|
|
return "***"
|
|
local, domain = email.split("@", 1)
|
|
safe_local = local[:2] + "***" if len(local) > 2 else local[:1] + "*"
|
|
return f"{safe_local}@{domain}"
|
|
|
|
|
|
# Возвращает первое непустое значение
|
|
def first_non_empty(values: Iterable[Any]) -> Any | None:
|
|
for value in values:
|
|
if value not in (None, "", [], {}, ()):
|
|
return value
|
|
return None
|
|
|
|
|
|
# Регулярные выражения для VIN, lot и price
|
|
VIN_RE = re.compile(r"\b([A-HJ-NPR-Z0-9]{17})\b", re.IGNORECASE)
|
|
LOT_RE = re.compile(r"\b(\d{7,10})\b")
|
|
PRICE_RE = re.compile(r"\$\s?([\d,]+(?:\.\d{1,2})?)")
|
|
|
|
|
|
# Глубокий рекурсивный поиск значений по ключам
|
|
def deep_find_key(obj, target_keys: set[str], max_depth: int = 64, _depth: int = 0) -> list:
|
|
found = []
|
|
if _depth >= max_depth:
|
|
return found
|
|
if isinstance(obj, dict):
|
|
for key, value in obj.items():
|
|
if key.lower() in target_keys:
|
|
found.append(value)
|
|
found.extend(deep_find_key(value, target_keys, max_depth=max_depth, _depth=_depth + 1))
|
|
elif isinstance(obj, list):
|
|
for item in obj:
|
|
found.extend(deep_find_key(item, target_keys, max_depth=max_depth, _depth=_depth + 1))
|
|
return found
|