This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
371 lines (321 loc) · 11.8 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# Rxivist, a system for indexing biology preprints
# and organizing them in ways that make it easier to find new
# or interesting research. Includes a web application for
# the display of data.
# Copyright (C) 2020 Regents of the University of Minnesota
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, version 3.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Any inquiries about this software or its use can be directed to
# Dr Ran Blekhman at the University of Minnesota:
# email: [email protected]
# mail: MCB 6-126, 420 Washington Avenue SE, Minneapolis, MN 55455
# http://blekhmanlab.org/
"""Definition of web service and initialization of the server.
This is the entrypoint for the application, the script called
when the server is started and the router for all user requests.
"""
import re
import bottle
import requests
import helpers
import config
import docs
# - ROUTES -
# WEB PAGES
# Homepage / search results
@bottle.get('/')
def index():
error = ""
view = bottle.request.query.view
if view is None or view not in ["standard", "print"]:
view = "standard"
entity = bottle.request.query.entity
if entity is None or entity == "":
entity = "papers"
category_list = helpers.rxapi("/v1/data/categories")["results"]
stats = helpers.rxapi("/v1/data/stats")
results = {} # a list of articles for the current page
respheaders = None
try:
if entity == "authors":
category_filter = bottle.request.query.getall('category')
if len(category_filter) == 0:
category = ""
else:
category = category_filter[0] # just one category for author ranks for now
category_filter = [category_filter[0]]
results = helpers.rxapi(f"/v1/authors?category={category}")
elif entity == "papers":
results, respheaders = helpers.rxapi(f"/v2/papers?{bottle.request.query_string}", headers=True)
except Exception as e:
print(e)
error = f"There was a problem with the submitted query: {e}"
bottle.response.status = 500
# Take the current query string and turn it into a template that any page
# number can get plugged into:
if "page=" in bottle.request.query_string:
pagelink = "/?{}".format(re.sub(r"page=\d*", "page=", bottle.request.query_string))
else:
pagelink = f"/?{bottle.request.query_string}&page="
metric = ""
timeframe = ""
page = ""
page_size = ""
totalcount = ""
query = ""
if entity == "papers":
try:
metric = results["query"]["metric"]
timeframe = results["query"]["timeframe"]
category_filter = results["query"]["categories"]
page = results["query"]["current_page"]
page_size = results["query"]["page_size"]
totalcount = results["query"]["total_results"]
query = results["query"]["text_search"]
repo = results["query"]["repository"]
except Exception as e:
if "error" in results.keys():
error = results["error"]
else:
error = str(e) # TODO: this is bad
return error
results = results["results"]
# figure out the page title
if entity == "papers":
title = "Most "
if metric == "twitter":
title += "tweeted"
elif metric == "downloads":
title += "downloaded"
if query != "":
title += f" preprints related to \"{query},\" "
else:
if repo == 'all':
title += " biology preprints, "
else:
title += f" {helpers.formatRepo(repo)} preprints, "
printable_times = {
"alltime": "all time",
"ytd": "year to date",
"lastmonth": "since beginning of last month",
"day": "last 24 hours",
"week": "last 7 days",
"month": "last 30 days",
"year": "last 365 days"
}
title += printable_times[timeframe]
elif entity == "authors":
title = "Authors with most downloads, all-time"
if bottle.request.query_string == "":
bottle.response.set_header("Cache-Control", f"max-age={config.front_page_cache}, stale-while-revalidate=172800")
# use whatever cache-control headers are sent to us from the API
if respheaders is not None and "Cache-Control" in respheaders.keys():
bottle.response.set_header("Cache-Control", respheaders["Cache-Control"])
temp = 'print' if view == "print" else 'index'
return bottle.template(temp, results=results,
query=query, category_filter=category_filter, title=title,
error=error, stats=stats, category_list=category_list, view=view,
timeframe=timeframe, metric=metric, entity=entity, google_tag=config.google_tag,
page=page, page_size=page_size, totalcount=totalcount, pagelink=pagelink,
querystring=bottle.request.query_string, repo=repo)
# Author details page
@bottle.get('/authors/<id:int>')
@bottle.view('author_details')
def display_author_details(id):
try:
get = requests.get(f"{config.rxapi}/v1/authors/{id}")
if get.status_code == 404:
raise helpers.NotFoundError(id)
# make sure the URL we ended at matches the URL we asked for:
new_id = re.search('/(\d+)$', get.url)
if new_id and len(new_id.groups()) > 0:
try:
new_id = int(new_id.group(1))
except Exception:
bottle.response.status = 500
return {"error": "Server errror."}
if new_id != id: # if we got redirected to a new URL
return bottle.redirect(f"{config.host}/authors/{new_id}", 301)
author = get.json()
except helpers.NotFoundError as e:
bottle.response.status = 404
return e.message
except ValueError as e:
bottle.response.status = 500
print(e)
return {"error": "Server error."}
distro = helpers.rxapi("/v1/data/distributions/author/downloads")
download_distribution = distro["histogram"]
averages = distro["averages"]
#stats = helpers.rxapi("/v1/data/stats")
return bottle.template('author_details', author=author,
download_distribution=download_distribution, averages=averages,
google_tag=config.google_tag)
# Paper details page
@bottle.get('/papers/<paper_id:path>')
@bottle.view('paper_details')
def display_paper_details(paper_id):
try:
paper = requests.get(f"{config.rxapi}/v1/papers/{paper_id}")
except helpers.NotFoundError as e:
bottle.response.status = 404
return e.message
except ValueError as e:
bottle.response.status = 500
print(e)
return {"error": "Server error."}
# make sure the URL we ended at matches the URL we asked for:
new_id = re.search('/(\d+)$', paper.url)
if new_id and len(new_id.groups()) > 0:
try:
new_id = str(new_id.group(1))
paper_id = str(paper_id)
except Exception:
bottle.response.status = 500
return {"error": "Server error."}
if new_id != paper_id: # if we got redirected to a new URL
return bottle.redirect(f"{config.host}/papers/{new_id}", 301)
paper = paper.json()
downloads = helpers.rxapi(f"/v1/downloads/{paper_id}")["results"]
# Convert the API response into something we can pass to the generic
# template for the graph:
traffic = [{
'month': x['month'], 'year': x['year'], 'count': x['downloads']
} for x in downloads]
distro = helpers.rxapi("/v1/data/distributions/paper/downloads")
download_distribution = distro["histogram"]
averages = distro["averages"]
#stats = helpers.rxapi("/v1/data/stats")
return bottle.template('paper_details', paper=paper, traffic=traffic,
download_distribution=download_distribution, averages=averages,
google_tag=config.google_tag)
# Annual results
@bottle.get('/top/<year:int>')
def topyear(year):
error = ''
respheaders = None
results = None
if year < 2013:
error = "Lists currently available only for 2013 through 2018."
bottle.response.status = 404
try:
results, respheaders = helpers.rxapi(f"/v1/top/{year}", headers=True)
except Exception as e:
print(e)
error = f"Sorry—there was a problem retrieving the results: {e}"
bottle.response.status = 500
# if respheaders is not None and "Cache-Control" in respheaders.keys():
# bottle.response.set_header("Cache-Control", respheaders["Cache-Control"])
bottle.response.set_header("Cache-Control", 'max-age=600, stale-while-revalidate=172800')
stats = helpers.rxapi("/v1/data/stats")
yearstats = { # TODO: Put this in the API
2013: {
"papers": 109,
"downloads": 17268
},
2014: {
"papers": 886,
"downloads": 311046
},
2015: {
"papers": 1774,
"downloads": 679159
},
2016: {
"papers": 4718,
"downloads": 2182836
},
2017: {
"papers": 11342,
"downloads": 3995108
},
2018: {
"papers": 20050,
"downloads": 5730324
}
}
return bottle.template('top_year', results=results['results'],
year=year, yearpapers=yearstats[year]['papers'], yeardownloads = yearstats[year]['downloads'],
error=error, stats=stats, google_tag=config.google_tag)
# Summary charts
@bottle.get('/stats')
def summary():
error = ''
respheaders = None
results = None
try:
results, respheaders = helpers.rxapi(f"/v1/data/summary", headers=True)
except Exception as e:
print(e)
error = f"Sorry—there was a problem retrieving the results: {e}"
bottle.response.status = 500
stats = helpers.rxapi("/v1/data/stats")
return bottle.template('summary', results=results,
error=error, stats=stats, google_tag=config.google_tag)
# Getting email code:
@bottle.get('/emailcode')
def emailcode():
results, _ = helpers.rxapi(f"/v1/papers?timeframe=week&page_size=20", headers=True)
results = results["results"]
bottle.response.set_header("Cache-Control", f"max-age=0, stale-while-revalidate=0")
return bottle.template('emailtemplate', results=results)
@bottle.route('/privacy')
@bottle.view('privacy')
def privacy():
#stats = helpers.rxapi("/v1/data/stats")
return bottle.template("privacy", google_tag=config.google_tag)
@bottle.route('/about')
@bottle.view('about')
def about():
#stats = helpers.rxapi("/v1/data/stats")
return bottle.template("about", google_tag=config.google_tag)
@bottle.route('/newsletter')
@bottle.view('newsletter')
def newsletter():
#stats = helpers.rxapi("/v1/data/stats")
return bottle.template("newsletter", google_tag=config.google_tag)
@bottle.route('/docs')
@bottle.view('api_docs')
def api_docs():
#stats = helpers.rxapi("/v1/data/stats")
documentation = docs.build_docs()
return bottle.template("api_docs", google_tag=config.google_tag, docs=documentation)
@bottle.route('/healthcheck')
@bottle.view('healthcheck')
def healthcheck():
return bottle.template("healthcheck")
# Search engine stuff
@bottle.route(f'/{config.google_validation_file}')
def callback():
return bottle.static_file(filename=config.google_validation_file, root='./static/')
@bottle.route('/robots.txt')
def callback():
return bottle.static_file(filename='robots.txt', root='./static/')
@bottle.route('/favicon.ico')
def callback():
return bottle.static_file(filename='favicon.ico', root='./static/')
# ---- Errors
@bottle.error(404)
@bottle.view('error')
def error404(error):
return bottle.template("error", google_tag=config.google_tag)
@bottle.error(500)
@bottle.view('error')
def error500(error):
return bottle.static_file('500.html', root='./static/')
# - SERVER -
@bottle.route('/static/<path:path>')
def callback(path):
return bottle.static_file(path, root='./static/')
if config.use_prod_webserver:
bottle.run(host='0.0.0.0', port=80, server="gunicorn")
else:
print("NOT PROD")
bottle.run(host='0.0.0.0', port=80, debug=True, reloader=True)