fix: separate news from publications and add employee refresh
This commit is contained in:
@@ -80,6 +80,48 @@ def run_crawl(db: Session, settings: Settings) -> CrawlRun:
|
||||
return run
|
||||
|
||||
|
||||
def refresh_employee(db: Session, employee: Employee, settings: Settings) -> CrawlRun:
|
||||
run = CrawlRun(source_url=employee.canonical_url, status="running", found_count=1)
|
||||
db.add(run)
|
||||
db.commit()
|
||||
db.refresh(run)
|
||||
|
||||
try:
|
||||
with requests.Session() as session:
|
||||
parsed = parse_person_profile(
|
||||
session,
|
||||
employee.canonical_url,
|
||||
HEADERS,
|
||||
settings.request_timeout,
|
||||
settings.parser_use_playwright,
|
||||
)
|
||||
if not parsed:
|
||||
raise ValueError("Профиль не удалось распарсить.")
|
||||
if _parsed_profile_key(parsed) != employee.profile_key:
|
||||
raise ValueError("Распарсенный профиль не совпадает с обновляемым сотрудником.")
|
||||
|
||||
_upsert_employee(db, run, parsed)
|
||||
run.parsed_count = 1
|
||||
run.status = "completed"
|
||||
except Exception as exc:
|
||||
run.status = "failed"
|
||||
run.error_count = 1
|
||||
run.message = str(exc)
|
||||
db.add(
|
||||
CrawlError(
|
||||
crawl_run_id=run.id,
|
||||
profile_url=employee.canonical_url,
|
||||
error_type=type(exc).__name__,
|
||||
message=str(exc),
|
||||
)
|
||||
)
|
||||
finally:
|
||||
run.finished_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
db.refresh(run)
|
||||
return run
|
||||
|
||||
|
||||
def _ensure_source(db: Session, source_url: str) -> ParserSource:
|
||||
source = db.scalar(select(ParserSource).where(ParserSource.source_url == source_url))
|
||||
if source:
|
||||
@@ -91,10 +133,14 @@ def _ensure_source(db: Session, source_url: str) -> ParserSource:
|
||||
return source
|
||||
|
||||
|
||||
def _parsed_profile_key(parsed: dict) -> str:
|
||||
return f"{parsed.get('profile_type')}:{parsed.get('profile_id')}"
|
||||
|
||||
|
||||
def _upsert_employee(db: Session, run: CrawlRun, parsed: dict) -> Employee:
|
||||
html = parsed.pop("_html", None)
|
||||
checksum = _checksum(parsed)
|
||||
key = f"{parsed.get('profile_type')}:{parsed.get('profile_id')}"
|
||||
key = _parsed_profile_key(parsed)
|
||||
employee = db.scalar(select(Employee).where(Employee.profile_key == key))
|
||||
now = datetime.now(timezone.utc)
|
||||
if not employee:
|
||||
|
||||
Reference in New Issue
Block a user