тест 3
This commit is contained in:
@@ -50,6 +50,10 @@ def parse_russian_date(date_str: str, year: int | None = None) -> datetime:
|
|||||||
return datetime(year, month, int(day), int(hh), int(mm), tzinfo=TZ)
|
return datetime(year, month, int(day), int(hh), int(mm), tzinfo=TZ)
|
||||||
|
|
||||||
|
|
||||||
|
class SiteUnavailableError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def fetch_html(url: str) -> str:
|
def fetch_html(url: str) -> str:
|
||||||
headers = {
|
headers = {
|
||||||
"User-Agent": "Mozilla/5.0",
|
"User-Agent": "Mozilla/5.0",
|
||||||
@@ -62,28 +66,28 @@ def fetch_html(url: str) -> str:
|
|||||||
return response.text
|
return response.text
|
||||||
|
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
return "Ошибка: сайт слишком долго отвечает"
|
raise SiteUnavailableError("Сайт слишком долго отвечает")
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
return "Ошибка: не удалось подключиться к сайту"
|
raise SiteUnavailableError("Не удалось подключиться к сайту")
|
||||||
|
|
||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
return f"Ошибка HTTP: {e.response.status_code}"
|
raise SiteUnavailableError(f"Ошибка HTTP: {e.response.status_code}")
|
||||||
|
|
||||||
except requests.exceptions.TooManyRedirects:
|
except requests.exceptions.TooManyRedirects:
|
||||||
return "Ошибка: слишком много редиректов"
|
raise SiteUnavailableError("Слишком много редиректов")
|
||||||
|
|
||||||
except requests.exceptions.InvalidURL:
|
except requests.exceptions.InvalidURL:
|
||||||
return "Ошибка: неверный URL"
|
raise SiteUnavailableError("Неверный URL")
|
||||||
|
|
||||||
except requests.exceptions.MissingSchema:
|
except requests.exceptions.MissingSchema:
|
||||||
return "Ошибка: отсутствует схема URL (https://)"
|
raise SiteUnavailableError("Отсутствует схема URL, например https://")
|
||||||
|
|
||||||
except requests.exceptions.SSLError:
|
except requests.exceptions.SSLError:
|
||||||
return "Ошибка SSL сертификата"
|
raise SiteUnavailableError("Ошибка SSL сертификата")
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
return f"Неизвестная ошибка запроса: {e}"
|
raise SiteUnavailableError(f"Неизвестная ошибка запроса: {e}")
|
||||||
|
|
||||||
def normalize_status(score1: str | None, score2: str | None, dt: datetime) -> str:
|
def normalize_status(score1: str | None, score2: str | None, dt: datetime) -> str:
|
||||||
now = datetime.now(TZ)
|
now = datetime.now(TZ)
|
||||||
@@ -190,11 +194,29 @@ def parse_schedule(html: str) -> list[dict]:
|
|||||||
return matches_data
|
return matches_data
|
||||||
|
|
||||||
|
|
||||||
def run_parser_schedule() -> None:
|
def run_parser_schedule() -> dict:
|
||||||
|
try:
|
||||||
html = fetch_html(URL_SCHEDULE)
|
html = fetch_html(URL_SCHEDULE)
|
||||||
matches_data = parse_schedule(html)
|
matches_data = parse_schedule(html)
|
||||||
sync_matches(matches_data)
|
sync_matches(matches_data)
|
||||||
print(f"[parser_schedule] Matches synced: {len(matches_data)}")
|
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"message": f"Матчи синхронизированы: {len(matches_data)}",
|
||||||
|
"count": len(matches_data),
|
||||||
|
}
|
||||||
|
|
||||||
|
except SiteUnavailableError as e:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": f"Сайт недоступен: {e}",
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": f"Ошибка парсера: {e}",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user