Fetch PDF using curl-cffi with Chrome TLS impersonation.
(pdf_url: str, source_url: str = "")
| 140 | |
| 141 | @staticmethod |
| 142 | def fetch_pdf(pdf_url: str, source_url: str = "") -> Optional[bytes]: |
| 143 | """Fetch PDF using curl-cffi with Chrome TLS impersonation.""" |
| 144 | try: |
| 145 | resp = FDAFetcher._cffi_get(pdf_url, timeout=120) |
| 146 | resp.raise_for_status() |
| 147 | |
| 148 | content = resp.content |
| 149 | if len(content) > MAX_PDF_SIZE: |
| 150 | logger.warning(f" PDF too large: {len(content) / 1024 / 1024:.1f} MB") |
| 151 | return None |
| 152 | |
| 153 | content_type = resp.headers.get("content-type", "").lower() |
| 154 | if "html" in content_type and len(content) < 20000: |
| 155 | body = content.decode("utf-8", errors="replace") |
| 156 | if "Access Denied" in body or "Pardon Our" in body or "apology" in body: |
| 157 | logger.warning(f" Blocked by Akamai (response contains block page)") |
| 158 | return None |
| 159 | |
| 160 | return content |
| 161 | except Exception as e: |
| 162 | logger.warning(f" Failed to fetch PDF from {pdf_url}: {e}") |
| 163 | return None |
| 164 | |
| 165 | |
| 166 | class GenericFetcher: |
no test coverage detected