(argv: list[str] | None = None)
| 81 | |
| 82 | |
| 83 | def main(argv: list[str] | None = None) -> None: |
| 84 | args = parser().parse_args(argv) |
| 85 | input_record = maybe_load_json_record(args.input) |
| 86 | if input_record is not None: |
| 87 | record = dict(input_record) |
| 88 | else: |
| 89 | record = enrich_metadata(resolve_reference(args.input)) |
| 90 | |
| 91 | record["paper_id"] = args.paper_id or record.get("paper_id") or paper_id_for_record(record) |
| 92 | source_candidates = pdf_source_candidates(record) |
| 93 | source_kind, source_value = source_candidates[0] if source_candidates else ("", "") |
| 94 | |
| 95 | if not source_kind: |
| 96 | payload = { |
| 97 | "status": "error", |
| 98 | "script": "fetch_pdf.py", |
| 99 | "paper_id": record["paper_id"], |
| 100 | "title": record.get("title", ""), |
| 101 | "error": "No accessible PDF source found.", |
| 102 | "source_url": record.get("source_url", ""), |
| 103 | } |
| 104 | emit(payload, args.output) |
| 105 | raise SystemExit(1) |
| 106 | |
| 107 | if source_kind == "local_pdf": |
| 108 | pdf_path = Path(source_value) |
| 109 | payload = { |
| 110 | "status": "ok", |
| 111 | "script": "fetch_pdf.py", |
| 112 | "paper_id": record["paper_id"], |
| 113 | "title": record.get("title", ""), |
| 114 | "pdf_path": str(pdf_path), |
| 115 | "pdf_source": "local_pdf", |
| 116 | "source_url": record.get("source_url", "") or str(pdf_path), |
| 117 | "pdf_url": "", |
| 118 | } |
| 119 | emit(payload, args.output) |
| 120 | return |
| 121 | |
| 122 | target_path = default_pdf_path(record, dest_dir=args.dest_dir) |
| 123 | attempted_sources: list[dict[str, str]] = [] |
| 124 | downloaded: tuple[str, str, bytes] | None = None |
| 125 | for candidate_kind, candidate_value in source_candidates: |
| 126 | if candidate_kind != "pdf_url": |
| 127 | continue |
| 128 | try: |
| 129 | data = http_get_bytes(candidate_value) |
| 130 | except Exception as exc: |
| 131 | attempted_sources.append( |
| 132 | {"kind": candidate_kind, "url": candidate_value, "status": f"download_error:{exc}"} |
| 133 | ) |
| 134 | continue |
| 135 | if not is_pdf_content(data): |
| 136 | attempted_sources.append( |
| 137 | {"kind": candidate_kind, "url": candidate_value, "status": "not_pdf_content"} |
| 138 | ) |
| 139 | continue |
| 140 | downloaded = (candidate_kind, candidate_value, data) |
no test coverage detected