(paper: Dict[str, Any])
| 1515 | |
| 1516 | def _backfill_metadata_from_source_page(paper: Dict[str, Any]) -> Dict[str, Any]: |
| 1517 | if _paper_has_sufficient_metadata_for_report(paper) and not paper.get("_prefer_detail_abstract"): |
| 1518 | return paper |
| 1519 | |
| 1520 | source_urls = _collect_source_page_urls(paper) |
| 1521 | if not source_urls: |
| 1522 | return paper |
| 1523 | |
| 1524 | try: |
| 1525 | journal_fetcher = _load_journal_fetcher() |
| 1526 | except Exception as exc: |
| 1527 | print(f"Unable to load journal fetcher for metadata backfill: {exc}") |
| 1528 | return paper |
| 1529 | |
| 1530 | fetch_detail = getattr(journal_fetcher, "_fetch_article_detail", None) |
| 1531 | if not callable(fetch_detail): |
| 1532 | return paper |
| 1533 | |
| 1534 | enriched = dict(paper) |
| 1535 | for source_url in source_urls: |
| 1536 | try: |
| 1537 | detail = fetch_detail(source_url) |
| 1538 | except Exception as exc: |
| 1539 | print(f" Metadata backfill failed for {source_url[:120]}: {exc}") |
| 1540 | continue |
| 1541 | |
| 1542 | if not isinstance(detail, dict) or detail.get("_skip"): |
| 1543 | continue |
| 1544 | |
| 1545 | merged = _merge_paper_details(enriched, detail) |
| 1546 | improved = ( |
| 1547 | _clean_text(merged.get("abstract")) != _clean_text(enriched.get("abstract")) |
| 1548 | or _parse_jsonish_list(merged.get("authors")) != _parse_jsonish_list(enriched.get("authors")) |
| 1549 | or _clean_text(merged.get("pdf_url")) != _clean_text(enriched.get("pdf_url")) |
| 1550 | ) |
| 1551 | enriched = merged |
| 1552 | if improved: |
| 1553 | print(f" Metadata backfilled from source page: {source_url[:120]}") |
| 1554 | if _paper_has_sufficient_metadata_for_report(enriched): |
| 1555 | break |
| 1556 | |
| 1557 | return enriched |
| 1558 | |
| 1559 | |
| 1560 | def _looks_like_pdf_url(candidate: Any) -> bool: |
| 1561 | text = _clean_text(candidate).lower() |
no test coverage detected