nikeshn commited on
Commit
9043adc
Β·
verified Β·
1 Parent(s): 1a22c1a

Update src/api/search.py

Browse files
Files changed (1) hide show
  1. src/api/search.py +136 -63
src/api/search.py CHANGED
@@ -1,11 +1,19 @@
1
- from fastapi import APIRouter, HTTPException
2
- from pydantic import BaseModel
3
  from typing import Optional
 
 
4
  import httpx
5
- import os
 
6
 
 
 
 
7
  router = APIRouter()
8
 
 
 
 
9
  class SearchRequest(BaseModel):
10
  query: str
11
  source: str = "primo"
@@ -15,86 +23,145 @@ class SearchRequest(BaseModel):
15
  year_from: Optional[str] = None
16
  year_to: Optional[str] = None
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  @router.post("")
19
  async def search_endpoint(req: SearchRequest):
20
  settings = get_settings()
21
-
22
  if req.source == "primo":
23
  return await search_primo(req, settings.primo_api_key)
24
  elif req.source == "pubmed":
25
  return await search_pubmed(req)
26
  elif req.source == "scholar":
27
  return await search_scholar(req)
28
- else:
29
- raise HTTPException(status_code=400, detail=f"Unknown source: {req.source}")
 
30
 
31
  async def search_primo(req: SearchRequest, api_key: str):
32
  if not api_key:
33
- return {"error": "PRIMO_API_KEY not configured", "results": []}
34
-
35
- # Simple query for now
36
- from urllib.parse import quote
37
- query = quote(req.query)
38
-
39
- url = f"https://api-eu.hosted.exlibrisgroup.com/primo/v1/search?vid=971KUOSTAR_INST:KU&tab=Everything&scope=MyInst_and_CI&q=any,contains,{query}&lang=en&sort=rank&limit={req.limit}&offset=0&mode=advanced&apikey={api_key}"
40
-
 
 
 
 
41
  try:
42
  async with httpx.AsyncClient(timeout=15) as client:
43
- r = await client.get(url, headers={"Accept": "application/json"})
 
 
 
 
 
 
44
  if r.status_code != 200:
45
- return {"error": f"PRIMO API error: {r.status_code}", "results": []}
46
-
 
47
  data = r.json()
48
  results = []
49
  for doc in data.get("docs", [])[:req.limit]:
50
  pnx = doc.get("pnx", {})
51
  d = pnx.get("display", {})
52
- a = pnx.get("addata", {})
53
-
54
  results.append({
55
  "title": (d.get("title") or ["Untitled"])[0],
56
  "creator": "; ".join(d.get("creator") or d.get("contributor") or []) or "Unknown",
57
  "date": (d.get("date") or [""])[0],
58
  "type": (d.get("type") or [""])[0],
59
  "source": (d.get("source") or [""])[0],
60
- "primo_url": f"https://khalifa.primo.exlibrisgroup.com/discovery/search?query=any,contains,{query}&vid=971KUOSTAR_INST:KU",
61
- "_source": "PRIMO"
 
 
 
62
  })
63
-
64
- return {"total": data.get("info", {}).get("total", 0), "results": results, "source": "PRIMO"}
65
-
 
 
 
 
66
  except Exception as e:
67
- return {"error": str(e), "results": []}
 
 
 
 
68
 
69
  async def search_pubmed(req: SearchRequest):
 
 
70
  try:
71
  async with httpx.AsyncClient(timeout=15) as client:
72
- # Search
73
- search_url = f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term={req.query}&retmax={req.limit}&retmode=json"
 
 
 
74
  r = await client.get(search_url)
75
  if r.status_code != 200:
76
- return {"error": "PubMed search failed", "results": []}
77
-
78
  ids = r.json().get("esearchresult", {}).get("idlist", [])
79
  if not ids:
80
  return {"total": 0, "results": [], "source": "PubMed"}
81
-
82
- # Fetch details
83
- fetch_url = f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id={','.join(ids)}&retmode=json"
 
 
 
84
  r2 = await client.get(fetch_url)
85
  if r2.status_code != 200:
86
- return {"error": "PubMed fetch failed", "results": []}
87
-
88
  data = r2.json().get("result", {})
89
  results = []
90
  for pid in ids:
91
  rec = data.get(pid, {})
92
  if not isinstance(rec, dict):
93
  continue
94
- authors = ", ".join(a.get("name", "") for a in rec.get("authors", [])[:3])
95
- if len(rec.get("authors", [])) > 3:
 
96
  authors += " et al."
97
-
98
  results.append({
99
  "title": rec.get("title", ""),
100
  "creator": authors,
@@ -103,54 +170,60 @@ async def search_pubmed(req: SearchRequest):
103
  "pmid": pid,
104
  "link": f"https://pubmed.ncbi.nlm.nih.gov/{pid}/",
105
  "type": "Journal Article",
106
- "_source": "PubMed"
107
  })
108
-
109
  return {"total": len(ids), "results": results, "source": "PubMed"}
110
-
111
  except Exception as e:
112
- return {"error": str(e), "results": []}
 
 
 
 
113
 
114
  async def search_scholar(req: SearchRequest):
115
- # Semantic Scholar implementation
116
  try:
117
- url = "https://api.semanticscholar.org/graph/v1/paper/search"
118
  params = {
119
  "query": req.query,
120
  "limit": req.limit,
121
- "fields": "title,authors,year,venue,externalIds,citationCount"
122
  }
123
-
124
  async with httpx.AsyncClient(timeout=15) as client:
125
- r = await client.get(url, params=params)
 
 
 
126
  if r.status_code != 200:
127
- return {"error": f"Semantic Scholar API: {r.status_code}", "results": []}
128
-
129
  data = r.json()
130
  results = []
131
  for paper in data.get("data", []):
132
- authors = ", ".join(a.get("name", "") for a in (paper.get("authors") or [])[:3])
133
- if len(paper.get("authors") or []) > 3:
 
134
  authors += " et al."
135
-
136
- ext_ids = paper.get("externalIds") or {}
137
-
138
  results.append({
139
  "title": paper.get("title", ""),
140
  "creator": authors,
141
  "date": str(paper.get("year", "")),
142
  "source": paper.get("venue", ""),
143
  "citations": paper.get("citationCount", 0),
144
- "link": f"https://www.semanticscholar.org/paper/{paper.get('paperId')}" if paper.get("paperId") else None,
145
  "type": "Article",
146
- "_source": "Semantic Scholar"
147
  })
148
-
149
- return {"total": data.get("total", 0), "results": results, "source": "Semantic Scholar"}
150
-
151
- except Exception as e:
152
- return {"error": str(e), "results": []}
153
 
154
- def get_settings():
155
- from src.config import get_settings as gs
156
- return gs()
 
 
 
 
 
 
 
1
+ import logging
 
2
  from typing import Optional
3
+ from urllib.parse import quote
4
+
5
  import httpx
6
+ from fastapi import APIRouter, HTTPException
7
+ from pydantic import BaseModel, field_validator
8
 
9
+ from src.config import get_settings
10
+
11
+ logger = logging.getLogger(__name__)
12
  router = APIRouter()
13
 
14
+
15
+ # ── Request Model ──────────────────────────────────────────────────────────────
16
+
17
  class SearchRequest(BaseModel):
18
  query: str
19
  source: str = "primo"
 
23
  year_from: Optional[str] = None
24
  year_to: Optional[str] = None
25
 
26
+ @field_validator("query")
27
+ @classmethod
28
+ def query_not_empty(cls, v: str) -> str:
29
+ v = v.strip()
30
+ if not v:
31
+ raise ValueError("query must not be empty")
32
+ if len(v) > 500:
33
+ raise ValueError("query must be under 500 characters")
34
+ return v
35
+
36
+ @field_validator("limit")
37
+ @classmethod
38
+ def limit_in_range(cls, v: int) -> int:
39
+ if not 1 <= v <= 20:
40
+ raise ValueError("limit must be between 1 and 20")
41
+ return v
42
+
43
+ @field_validator("source")
44
+ @classmethod
45
+ def valid_source(cls, v: str) -> str:
46
+ allowed = {"primo", "pubmed", "scholar"}
47
+ if v not in allowed:
48
+ raise ValueError(f"source must be one of: {allowed}")
49
+ return v
50
+
51
+
52
+ # ── Main Endpoint ──────────────────────────────────────────────────────────────
53
+
54
  @router.post("")
55
  async def search_endpoint(req: SearchRequest):
56
  settings = get_settings()
57
+
58
  if req.source == "primo":
59
  return await search_primo(req, settings.primo_api_key)
60
  elif req.source == "pubmed":
61
  return await search_pubmed(req)
62
  elif req.source == "scholar":
63
  return await search_scholar(req)
64
+
65
+
66
+ # ── Primo ──────────────────────────────────────────────────────────────────────
67
 
68
  async def search_primo(req: SearchRequest, api_key: str):
69
  if not api_key:
70
+ return {"error": "PRIMO_API_KEY not configured", "results": [], "source": "PRIMO"}
71
+
72
+ query_encoded = quote(req.query)
73
+
74
+ # API key in header, not URL query string
75
+ url = (
76
+ f"https://api-eu.hosted.exlibrisgroup.com/primo/v1/search"
77
+ f"?vid=971KUOSTAR_INST:KU&tab=Everything&scope=MyInst_and_CI"
78
+ f"&q=any,contains,{query_encoded}&lang=en&sort=rank"
79
+ f"&limit={req.limit}&offset=0&mode=advanced"
80
+ )
81
+
82
  try:
83
  async with httpx.AsyncClient(timeout=15) as client:
84
+ r = await client.get(
85
+ url,
86
+ headers={
87
+ "Accept": "application/json",
88
+ "Authorization": f"apikey {api_key}",
89
+ },
90
+ )
91
  if r.status_code != 200:
92
+ logger.warning(f"Primo API returned {r.status_code}")
93
+ return {"error": f"PRIMO API error: {r.status_code}", "results": [], "source": "PRIMO"}
94
+
95
  data = r.json()
96
  results = []
97
  for doc in data.get("docs", [])[:req.limit]:
98
  pnx = doc.get("pnx", {})
99
  d = pnx.get("display", {})
100
+
 
101
  results.append({
102
  "title": (d.get("title") or ["Untitled"])[0],
103
  "creator": "; ".join(d.get("creator") or d.get("contributor") or []) or "Unknown",
104
  "date": (d.get("date") or [""])[0],
105
  "type": (d.get("type") or [""])[0],
106
  "source": (d.get("source") or [""])[0],
107
+ "primo_url": (
108
+ f"https://khalifa.primo.exlibrisgroup.com/discovery/search"
109
+ f"?query=any,contains,{query_encoded}&vid=971KUOSTAR_INST:KU"
110
+ ),
111
+ "_source": "PRIMO",
112
  })
113
+
114
+ return {
115
+ "total": data.get("info", {}).get("total", 0),
116
+ "results": results,
117
+ "source": "PRIMO",
118
+ }
119
+
120
  except Exception as e:
121
+ logger.error(f"Primo search error: {e}")
122
+ return {"error": str(e), "results": [], "source": "PRIMO"}
123
+
124
+
125
+ # ── PubMed ─────────────────────────────────────────────────────────────────────
126
 
127
  async def search_pubmed(req: SearchRequest):
128
+ query_encoded = quote(req.query)
129
+
130
  try:
131
  async with httpx.AsyncClient(timeout=15) as client:
132
+ # Step 1: search for IDs
133
+ search_url = (
134
+ f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
135
+ f"?db=pubmed&term={query_encoded}&retmax={req.limit}&retmode=json"
136
+ )
137
  r = await client.get(search_url)
138
  if r.status_code != 200:
139
+ return {"error": "PubMed search failed", "results": [], "source": "PubMed"}
140
+
141
  ids = r.json().get("esearchresult", {}).get("idlist", [])
142
  if not ids:
143
  return {"total": 0, "results": [], "source": "PubMed"}
144
+
145
+ # Step 2: fetch summaries
146
+ fetch_url = (
147
+ f"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
148
+ f"?db=pubmed&id={','.join(ids)}&retmode=json"
149
+ )
150
  r2 = await client.get(fetch_url)
151
  if r2.status_code != 200:
152
+ return {"error": "PubMed fetch failed", "results": [], "source": "PubMed"}
153
+
154
  data = r2.json().get("result", {})
155
  results = []
156
  for pid in ids:
157
  rec = data.get(pid, {})
158
  if not isinstance(rec, dict):
159
  continue
160
+ author_list = rec.get("authors", [])
161
+ authors = ", ".join(a.get("name", "") for a in author_list[:3])
162
+ if len(author_list) > 3:
163
  authors += " et al."
164
+
165
  results.append({
166
  "title": rec.get("title", ""),
167
  "creator": authors,
 
170
  "pmid": pid,
171
  "link": f"https://pubmed.ncbi.nlm.nih.gov/{pid}/",
172
  "type": "Journal Article",
173
+ "_source": "PubMed",
174
  })
175
+
176
  return {"total": len(ids), "results": results, "source": "PubMed"}
177
+
178
  except Exception as e:
179
+ logger.error(f"PubMed search error: {e}")
180
+ return {"error": str(e), "results": [], "source": "PubMed"}
181
+
182
+
183
+ # ── Semantic Scholar ───────────────────────────────────────────────────────────
184
 
185
  async def search_scholar(req: SearchRequest):
 
186
  try:
 
187
  params = {
188
  "query": req.query,
189
  "limit": req.limit,
190
+ "fields": "title,authors,year,venue,externalIds,citationCount",
191
  }
192
+
193
  async with httpx.AsyncClient(timeout=15) as client:
194
+ r = await client.get(
195
+ "https://api.semanticscholar.org/graph/v1/paper/search",
196
+ params=params,
197
+ )
198
  if r.status_code != 200:
199
+ return {"error": f"Semantic Scholar API: {r.status_code}", "results": [], "source": "Semantic Scholar"}
200
+
201
  data = r.json()
202
  results = []
203
  for paper in data.get("data", []):
204
+ author_list = paper.get("authors") or []
205
+ authors = ", ".join(a.get("name", "") for a in author_list[:3])
206
+ if len(author_list) > 3:
207
  authors += " et al."
208
+
209
+ paper_id = paper.get("paperId")
 
210
  results.append({
211
  "title": paper.get("title", ""),
212
  "creator": authors,
213
  "date": str(paper.get("year", "")),
214
  "source": paper.get("venue", ""),
215
  "citations": paper.get("citationCount", 0),
216
+ "link": f"https://www.semanticscholar.org/paper/{paper_id}" if paper_id else "",
217
  "type": "Article",
218
+ "_source": "Semantic Scholar",
219
  })
 
 
 
 
 
220
 
221
+ return {
222
+ "total": data.get("total", 0),
223
+ "results": results,
224
+ "source": "Semantic Scholar",
225
+ }
226
+
227
+ except Exception as e:
228
+ logger.error(f"Semantic Scholar search error: {e}")
229
+ return {"error": str(e), "results": [], "source": "Semantic Scholar"}