-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
235 lines (176 loc) · 6.93 KB
/
app.py
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
import datetime
import time
from flask import Flask, render_template, request
from flask import jsonify, redirect, url_for
from urllib.request import urlopen
import requests
from bs4 import BeautifulSoup
from flask_restx import Resource, Api
import uuid
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
action = request.form['action']
if action=='api':
return redirect("/api/")
elif action == 'search':
term = request.form['term']
return redirect(url_for('search', term=term))
elif action == 'fetch':
start_time = time.time()
taskID = request.form['taskID']
if taskID not in background_jobs_term:
return jsonify({"error": "This Task ID is not found, please enter the ID of the task you searched a term for"}), 400
return redirect(url_for('fetch', taskID=taskID))
else:
return render_template('index.html')
# Set configurations
#app.config['SERVER_NAME'] = 'localhost:5000' # Update with your server name app.config['SERVER_NAME'] = 'localhost:5000'
app.config['APPLICATION_ROOT'] = '/' # Update with your application root
app.config['PREFERRED_URL_SCHEME'] = 'http' # Update with your preferred URL scheme
# Dummy data to simulate background job status
background_jobs = {}
background_jobs_term = {}
@app.route('/search')
def search():
term = request.args.get('term')
if term is None:
return jsonify({"error": "No search term provided"}), 400
records = perform_search(term)
task_id = str(uuid.uuid4())
background_jobs[task_id] = {
"task_id": task_id,
"status": "processing",
"created_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
background_jobs_term[task_id] = {
"task_id": task_id,
"term":term
}
response = {
"records": records,
"query": term,
"task_id": task_id
}
return jsonify(response)
@app.route('/fetch')
def fetch():
start_time = time.time()
taskID = request.args.get('taskID')
job_term=background_jobs_term[taskID]
job = background_jobs[taskID]
search_term = job_term["term"]
pmids=retrieve_pmids(search_term)
status="complete"
# Record the end time
end_time = time.time()
# Calculate the elapsed time
elapsed_time_seconds = end_time - start_time
response = {
"task_id": taskID,
"status": status, "result":{ "pmids": pmids },
"created_time": job["created_time"],
"run_seconds": elapsed_time_seconds
}
return jsonify(response)
with app.app_context():
api = Api(app, doc='/api/', title="Yale BIDS APIs", # Set your custom title here
description="Two APIs for searching terms (e.g., kidney disease) in PubMed and retrieving information based on the task ID (which is generated from search tasks)." ,
default="Click to search and retrieve information", # Set your custom default namespace here
default_label="API endpoints for BIDS functionalities", swagger_ui=True) # Set your custom default namespace description here
def perform_search(term):
try:
# Construct the URL for PubMed search
url = f"https://pubmed.ncbi.nlm.nih.gov/?term={term.replace(' ', '+')}"
# Send HTTP GET request to PubMed website
response = requests.get(url)
# Check if request was successful (status code 200)
if response.status_code == 200:
# Parse HTML response to extract the number of records
records = extract_records(response.text)
return records
else:
print(f"Error: Failed to retrieve search results. Status code: {response.status_code}")
return 0
except Exception as e:
print(f"Error performing PubMed search: {e}")
return 0
def retrieve_pmids(search_term):
try:
# Construct the PubMed search URL
url = f"https://pubmed.ncbi.nlm.nih.gov/?term={search_term.replace(' ', '+')}"
# Send HTTP GET request to PubMed website
response = requests.get(url)
# Check if request was successful (status code 200)
if response.status_code == 200:
# Parse HTML response to extract PubMed IDs (pmids)
soup = BeautifulSoup(response.text, 'html.parser')
pmids = [int(tag['data-article-id']) for tag in soup.find_all(attrs={"data-article-id": True})]
return pmids
else:
print(f"Error: Failed to retrieve PubMed search results. Status code: {response.status_code}")
return []
except Exception as e:
print(f"Error retrieving PubMed search results: {e}")
return []
@api.route('/searchapi/<term>')
@api.doc(params={'term': 'The search term'})
class searchapi(Resource):
def get(self, term):
records = perform_search(term)
task_id = str(uuid.uuid4())
background_jobs[task_id] = {
"task_id": task_id,
"status": "processing",
"created_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
background_jobs_term[task_id] = {
"task_id": task_id,
"term": term
}
response = {
"records": records,
"query": term,
"task_id": task_id
}
return jsonify(response)
@api.route('/fetchapi/<task_id>')
class fetchapi(Resource):
def post(self, task_id):
start_time = time.time()
if task_id not in background_jobs_term:
return jsonify({"error": "This Task ID is not found, please enter the ID of the task you searched a term for"})
job_term = background_jobs_term[task_id]
job = background_jobs[task_id]
search_term = job_term["term"]
pmids = retrieve_pmids(search_term)
status = "complete"
# Record the end time
end_time = time.time()
# Calculate the elapsed time
elapsed_time_seconds = end_time - start_time
response = {
"task_id": task_id,
"status": status, "result": {"pmids": pmids},
"created_time": job["created_time"],
"run_seconds": elapsed_time_seconds
}
return jsonify(response)
def extract_records(html_content):
try:
soup = BeautifulSoup(html_content, 'html.parser')
result_div = soup.find('div', class_='results-amount')
if result_div:
records_text = result_div.get_text()
records = int(records_text.split()[0].replace(',', ''))
return records
else:
print("Error: Results div not found in HTML content")
return 0
except Exception as e:
print(f"Error extracting records: {e}")
return 0
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
app.run()