Spaces:
Paused
Paused
Commit ·
bb294dd
1
Parent(s): 43b78de
Update pg url handler
Browse files55 unmodified lines5657585960616263646566676869707172737455 unmodified lines# Fetch itemsasync def fetch(ref: dict):"""For import: just return the same direct PDF link."""gid = ref.get("id")if not gid:return Noneurl = f"https://gutendex.com/books/{gid}"async with httpx.AsyncClient(timeout=10) as client:r = await client.get(url)if r.status_code != 200:return Nonedata = r.json()pdf_link = next((v for k, v in data["formats"].items() if k.endswith("pdf")), None)if pdf_link:return {"download_available": True, "download_url": pdf_link}return None55 unmodified lines565758596061626364656667686970717273747576777879808182838485868755 unmodified lines# Fetch itemsasync def fetch(ref: dict):"""For import: return direct PDF link if available via Gutendex or fallback to Gutenberg."""gid = ref.get("id")if not gid:return None# Trailing to preview page for PDF confirmationgutendex_url = f"https://gutendex.com/books/{gid}/" # ensure trailing slashtry:async with httpx.AsyncClient(timeout=10, follow_redirects=True) as client:r = await client.get(gutendex_url)if r.status_code == 200:data = r.json()pdf_link = next((v for k, v in data["formats"].items() if k.lower().endswith("pdf")),None)if pdf_link:return {"download_available": True, "download_url": pdf_link}except Exception as e:logger.warning(f"[GUT] Gutendex metadata failed for {gid}: {e}")# Fallback to static Gutenberg URLfallback_url = f"https://www.gutenberg.org/files/{gid}/{gid}-pdf.pdf"async with httpx.AsyncClient(timeout=10) as client:head = await client.head(fallback_url)if head.status_code == 200:logger.info(f"[GUT] Using fallback PDF: {fallback_url}")return {"download_available": True, "download_url": fallback_url}# Loglogger.warning(f"[GUT] No PDF for book {gid}")return None
app/services/project_gutenberg.py+24-11