-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.env.example
More file actions
244 lines (230 loc) · 8.48 KB
/
Copy path.env.example
File metadata and controls
244 lines (230 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
import re
import requests
import xmltodict
from flask import Flask, request, jsonify, render_template
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
# ------------------------------------------------------------
# 1. arXiv Search (free, no key)
# ------------------------------------------------------------
def search_arxiv(query, max_results=20):
url = "http://export.arxiv.org/api/query"
params = {
"search_query": f"all:{query}",
"start": 0,
"max_results": max_results,
"sortBy": "relevance",
"sortOrder": "descending"
}
try:
resp = requests.get(url, params=params, timeout=10)
data = xmltodict.parse(resp.text)
entries = data.get("feed", {}).get("entry", [])
if not isinstance(entries, list):
entries = [entries]
results = []
for entry in entries:
title = entry.get("title", "").strip()
summary = entry.get("summary", "").strip()
link = entry.get("id", "")
published = entry.get("published", "")[:10]
results.append({
"title": title,
"abstract": summary,
"url": link,
"date": published,
"source": "arXiv",
"type": "preprint"
})
return results
except Exception as e:
print(f"arXiv error: {e}")
return []
# ------------------------------------------------------------
# 2. OpenAlex (free, no key)
# ------------------------------------------------------------
def search_openalex(query, max_results=20):
url = "https://api.openalex.org/works"
params = {
"search": query,
"per-page": max_results,
"sort": "relevance_score:desc"
}
try:
resp = requests.get(url, params=params, timeout=10)
data = resp.json()
results = []
for work in data.get("results", []):
title = work.get("title", "")
abstract = work.get("abstract", "") or ""
doi = work.get("doi", "")
url_link = f"https://doi.org/{doi}" if doi else ""
date = work.get("publication_date", "")[:10]
citations = work.get("cited_by_count", 0)
results.append({
"title": title,
"abstract": abstract[:500] + "..." if len(abstract) > 500 else abstract,
"url": url_link,
"date": date,
"citations": citations,
"source": "OpenAlex",
"type": "journal article"
})
return results
except Exception as e:
print(f"OpenAlex error: {e}")
return []
# ------------------------------------------------------------
# 3. Crossref (free, no key)
# ------------------------------------------------------------
def search_crossref(query, max_results=20):
url = "https://api.crossref.org/works"
params = {
"query": query,
"rows": max_results,
"sort": "relevance"
}
try:
resp = requests.get(url, params=params, timeout=10)
data = resp.json()
items = data.get("message", {}).get("items", [])
results = []
for item in items:
title = item.get("title", [""])[0]
abstract = item.get("abstract", "")
if abstract:
abstract = re.sub(r'<.*?>', '', abstract)[:500]
doi = item.get("DOI", "")
url_link = f"https://doi.org/{doi}" if doi else ""
date_parts = item.get("issued", {}).get("date-parts", [[]])
year = date_parts[0][0] if date_parts and date_parts[0] else ""
results.append({
"title": title,
"abstract": abstract,
"url": url_link,
"date": str(year),
"source": "Crossref",
"type": "journal article"
})
return results
except Exception as e:
print(f"Crossref error: {e}")
return []
# ------------------------------------------------------------
# 4. Semantic Scholar (free, optional key)
# ------------------------------------------------------------
def search_semantic_scholar(query, max_results=20, api_key=""):
url = "https://api.semanticscholar.org/graph/v1/paper/search"
headers = {"x-api-key": api_key} if api_key else {}
params = {
"query": query,
"limit": max_results,
"fields": "title,abstract,url,publicationDate,citationCount,venue"
}
try:
resp = requests.get(url, headers=headers, params=params, timeout=10)
data = resp.json()
results = []
for paper in data.get("data", []):
title = paper.get("title", "")
abstract = paper.get("abstract", "") or ""
url_link = paper.get("url", "")
date = paper.get("publicationDate", "")[:10]
citations = paper.get("citationCount", 0)
venue = paper.get("venue", "")
results.append({
"title": title,
"abstract": abstract[:500],
"url": url_link,
"date": date,
"citations": citations,
"source": f"Semantic Scholar ({venue})" if venue else "Semantic Scholar",
"type": "journal article"
})
return results
except Exception as e:
print(f"Semantic Scholar error: {e}")
return []
# ------------------------------------------------------------
# 5. Lens.org Patent Search (requires free API key)
# ------------------------------------------------------------
def search_lens_patents(query, max_results=20, api_key=""):
if not api_key:
return []
url = "https://api.lens.org/patent/search"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"query": {
"bool": {
"must": {"simple_query_string": {"query": query, "fields": ["title", "abstract"]}}
}
},
"size": max_results,
"include": ["biblio", "abstract", "jurisdiction", "date_published"]
}
try:
resp = requests.post(url, headers=headers, json=payload, timeout=15)
data = resp.json()
results = []
for patent in data.get("data", []):
biblio = patent.get("biblio", {})
title = biblio.get("title", "")
abstract = patent.get("abstract", [{}])[0].get("text", "")
lens_id = patent.get("lens_id", "")
url_link = f"https://lens.org/{lens_id}" if lens_id else ""
jurisdiction = patent.get("jurisdiction", "")
date = patent.get("date_published", "")[:10]
results.append({
"title": title,
"abstract": abstract[:500],
"url": url_link,
"date": date,
"jurisdiction": jurisdiction,
"source": "Lens.org",
"type": "patent"
})
return results
except Exception as e:
print(f"Lens.org error: {e}")
return []
# ------------------------------------------------------------
# Main search endpoint
# ------------------------------------------------------------
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/search')
def search():
query = request.args.get('q', '').strip()
if not query:
return jsonify({"error": "Missing query"}), 400
# Run all enabled APIs in parallel (simplified sequential for stability)
results = []
results.extend(search_arxiv(query, max_results=10))
results.extend(search_openalex(query, max_results=10))
results.extend(search_crossref(query, max_results=10))
results.extend(search_semantic_scholar(query, max_results=10, api_key=os.getenv("SEMANTIC_SCHOLAR_API_KEY", "")))
results.extend(search_lens_patents(query, max_results=10, api_key=os.getenv("LENS_API_KEY", "")))
# Deduplicate by title
seen = set()
unique = []
for r in results:
title_lower = r["title"].lower()
if title_lower and title_lower not in seen:
seen.add(title_lower)
unique.append(r)
# Sort by date (newest first)
unique.sort(key=lambda x: x.get("date", ""), reverse=True)
return jsonify({
"query": query,
"count": len(unique),
"results": unique
})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)