Upload 2 files
Browse files- core/sources/ctx.py +26 -0
- core/sources/ntp.py +28 -0
core/sources/ctx.py
CHANGED
|
@@ -239,6 +239,32 @@ async def fetch_ctx_genetox(cas_or_query: str, http: httpx.AsyncClient) -> Dict[
|
|
| 239 |
if not q:
|
| 240 |
return {"ok": False, "error": "Empty query"}
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
dtxsid = await resolve_dtxsid(q, http)
|
| 243 |
if not dtxsid:
|
| 244 |
return {
|
|
|
|
| 239 |
if not q:
|
| 240 |
return {"ok": False, "error": "Empty query"}
|
| 241 |
|
| 242 |
+
# Prefer worker proxy if configured (matches production behavior)
|
| 243 |
+
if settings.worker_base_url:
|
| 244 |
+
try:
|
| 245 |
+
worker_url = settings.worker_base_url.rstrip("/") + "/ctx-genetox"
|
| 246 |
+
payload = {"dtxsid": q} if q.upper().startswith("DTXSID") else {"query": q}
|
| 247 |
+
r = await http.post(worker_url, json=payload, timeout=25.0)
|
| 248 |
+
if r.status_code < 400:
|
| 249 |
+
data = r.json()
|
| 250 |
+
if data.get("summary"):
|
| 251 |
+
dtxsid = data.get("dtxsid")
|
| 252 |
+
return {
|
| 253 |
+
"ok": True,
|
| 254 |
+
"dtxsid": dtxsid,
|
| 255 |
+
"summary": data.get("summary"),
|
| 256 |
+
"dashboard_url": dashboard_details_url(dtxsid or q),
|
| 257 |
+
}
|
| 258 |
+
if data.get("resolveUrl"):
|
| 259 |
+
return {
|
| 260 |
+
"ok": False,
|
| 261 |
+
"error": data.get("message") or "No DTXSID found for this query.",
|
| 262 |
+
"dashboard_search": data.get("resolveUrl"),
|
| 263 |
+
}
|
| 264 |
+
# If worker errors, fall through to direct CTX
|
| 265 |
+
except Exception:
|
| 266 |
+
pass
|
| 267 |
+
|
| 268 |
dtxsid = await resolve_dtxsid(q, http)
|
| 269 |
if not dtxsid:
|
| 270 |
return {
|
core/sources/ntp.py
CHANGED
|
@@ -5,6 +5,8 @@ from urllib.parse import urljoin
|
|
| 5 |
|
| 6 |
import httpx
|
| 7 |
|
|
|
|
|
|
|
| 8 |
REPORTS_URL = "https://ntp.niehs.nih.gov/publications/reports"
|
| 9 |
BASE = "https://ntp.niehs.nih.gov"
|
| 10 |
INDEX_URL = "https://ntp.niehs.nih.gov/data/tr"
|
|
@@ -92,6 +94,32 @@ async def search_technical_reports(query: str, http: httpx.AsyncClient, limit: i
|
|
| 92 |
if not q:
|
| 93 |
return {"ok": False, "error": "Empty query", "items": []}
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
r = await http.get(REPORTS_URL, timeout=25, follow_redirects=True)
|
| 97 |
r.raise_for_status()
|
|
|
|
| 5 |
|
| 6 |
import httpx
|
| 7 |
|
| 8 |
+
from core.config import settings
|
| 9 |
+
|
| 10 |
REPORTS_URL = "https://ntp.niehs.nih.gov/publications/reports"
|
| 11 |
BASE = "https://ntp.niehs.nih.gov"
|
| 12 |
INDEX_URL = "https://ntp.niehs.nih.gov/data/tr"
|
|
|
|
| 94 |
if not q:
|
| 95 |
return {"ok": False, "error": "Empty query", "items": []}
|
| 96 |
|
| 97 |
+
# Prefer worker proxy if configured (matches production behavior)
|
| 98 |
+
if settings.worker_base_url:
|
| 99 |
+
try:
|
| 100 |
+
worker_url = settings.worker_base_url.rstrip("/") + "/ntp-tr"
|
| 101 |
+
r = await http.post(worker_url, json={"query": q, "limit": limit}, timeout=25.0)
|
| 102 |
+
if r.status_code < 400:
|
| 103 |
+
data = r.json()
|
| 104 |
+
rows = data.get("results") or []
|
| 105 |
+
items: List[Dict[str, Any]] = []
|
| 106 |
+
for row in rows:
|
| 107 |
+
if not isinstance(row, dict):
|
| 108 |
+
continue
|
| 109 |
+
items.append(
|
| 110 |
+
{
|
| 111 |
+
"num": (row.get("tr") or "").replace("TR-", ""),
|
| 112 |
+
"tr": row.get("tr"),
|
| 113 |
+
"report_page": row.get("page") or row.get("url"),
|
| 114 |
+
"title": row.get("title") or "NTP Technical Report",
|
| 115 |
+
"year": row.get("year"),
|
| 116 |
+
"pdf": row.get("pdf"),
|
| 117 |
+
}
|
| 118 |
+
)
|
| 119 |
+
return {"ok": True, "query": q, "items": items}
|
| 120 |
+
except Exception:
|
| 121 |
+
pass
|
| 122 |
+
|
| 123 |
try:
|
| 124 |
r = await http.get(REPORTS_URL, timeout=25, follow_redirects=True)
|
| 125 |
r.raise_for_status()
|