feat(ext): Фаза 4/5 — охват всех источников/документов + все регионы
fgis_client: opendata_list/meta/download (15 датасетов: КСР/ФСНБ/СПСРП/оплата труда/9 ПССР), bim_leaf_docs (дерево писем-индексов), download_by_guid, all_subject_ids. xsd_client: скрейп .zip-схем экспертизы (html.parser, fail-loud на 0 ссылок). ext_pull: pull_opendata/bim/xsd, --source opendata/bim/xsd/all-sources, --regions all (все 86). ext_check: check_opendata (lastChangeDate)/check_bim(guid)/check_xsd(ссылки) + режим all-regions — чекер мониторит ВСЁ. 10 новых тестов.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
"""xsd_client — XSD-схемы сметной документации/экспертизы с minstroyrf.gov.ru (срез 5.1).
|
||||
|
||||
Единственный НЕ-API источник: HTML-страница /tim/xml-skhemy/ со ссылками на .zip-пакеты
|
||||
схем (ЛСР, ОСР, КАЦ и т.д.). Скрейп стандартным html.parser (без новых зависимостей).
|
||||
fail-loud: 0 ссылок = вёрстка изменилась (НЕ «обновлений нет» — типовая тихая ложь скрейпера).
|
||||
Скачивание/версионирование только; валидация gge_export по схемам (нужен lxml) — отдельный
|
||||
срез 5.2 по согласованию.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from html.parser import HTMLParser
|
||||
|
||||
import ext_fetch
|
||||
|
||||
BASE = os.environ.get("MINSTROY_BASE", "https://minstroyrf.gov.ru")
|
||||
PAGE = os.environ.get("MINSTROY_XSD_PAGE", "/tim/xml-skhemy/")
|
||||
|
||||
|
||||
class _ZipLinkParser(HTMLParser):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.links: list[str] = []
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
if tag == "a":
|
||||
href = dict(attrs).get("href", "") or ""
|
||||
if href.lower().endswith(".zip"):
|
||||
self.links.append(href)
|
||||
|
||||
|
||||
def _page_html(client=None) -> str:
|
||||
c = client or ext_fetch.make_client()
|
||||
try:
|
||||
r = c.get(f"{BASE}{PAGE}")
|
||||
r.raise_for_status()
|
||||
return r.text
|
||||
finally:
|
||||
if client is None:
|
||||
c.close()
|
||||
|
||||
|
||||
def schema_links(client=None) -> list[str]:
|
||||
"""Ссылки на .zip-пакеты схем со страницы. fail-loud при 0 (вёрстка сменилась)."""
|
||||
p = _ZipLinkParser()
|
||||
p.feed(_page_html(client=client))
|
||||
links = sorted(set(p.links))
|
||||
if not links:
|
||||
raise ext_fetch.FetchError("страница XSD-схем: 0 ссылок .zip — изменилась вёрстка/страница?")
|
||||
return links
|
||||
|
||||
|
||||
def _fname(href: str) -> str:
|
||||
return href.rstrip("/").split("/")[-1] or "schema.zip"
|
||||
|
||||
|
||||
def download_schema(href: str, dest, client=None) -> dict:
|
||||
url = href if href.startswith("http") else f"{BASE}{href}"
|
||||
return ext_fetch.fetch_file(url, dest, expect_min_size=100, client=client)
|
||||
Reference in New Issue
Block a user