(pdf_url: str, title: str, referer: Optional[str] = None)
| 1934 | |
| 1935 | def _download_pdf(pdf_url: str, title: str, referer: Optional[str] = None) -> str: |
| 1936 | response = _http_get( |
| 1937 | pdf_url, |
| 1938 | timeout=PDF_DOWNLOAD_TIMEOUT, |
| 1939 | referer=referer, |
| 1940 | accept_pdf=True, |
| 1941 | ) |
| 1942 | content_type = str(response.headers.get("Content-Type") or "").lower() |
| 1943 | if "application/pdf" not in content_type and not response.content.startswith(b"%PDF"): |
| 1944 | raise ValueError( |
| 1945 | f"Resolved URL did not return a PDF (content-type={content_type or 'unknown'})" |
| 1946 | ) |
| 1947 | |
| 1948 | safe_prefix = re.sub(r"[^A-Za-z0-9]+", "_", title or "paper").strip("_")[:40] or "paper" |
| 1949 | with tempfile.NamedTemporaryFile(prefix=f"{safe_prefix}_", suffix=".pdf", delete=False) as temp_file: |
| 1950 | temp_file.write(response.content) |
| 1951 | return temp_file.name |
| 1952 | |
| 1953 | |
| 1954 | def _persist_pdf_file( |
| 1955 | source_pdf_path: str, |
no test coverage detected