This repository was 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 pathhelpers.py
94 lines (78 loc) · 2.29 KB
/
helpers.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
"""Utilities for interpreting data that arrives in impractical formats.
This module stores helper functions that transform data for the controllers.
"""
import config
import requests
class NotFoundError(Exception):
def __init__(self, id):
self.message = f"Entity could not be found with id {id}"
def get_traffic(connection, id):
"""Collects data about a single paper's download statistics.
Arguments:
- connection: a database connection object.
- id: the ID given to the article being queried.
Returns:
- A two-element tuple. The first element is the number of views of
the paper's abstract; the second is total PDF downloads.
"""
traffic = connection.read("SELECT SUM(abstract), SUM(pdf) FROM article_traffic WHERE article=%s;", (id,))
if len(traffic) == 0:
raise NotFoundError(id)
return traffic[0] # "traffic" is an array of tuples; we only want a tuple.
def num_to_month(monthnum):
"""Converts a (1-indexed) numerical representation of a month
of the year into a three-character string for printing. If
the number is not recognized, it returns an empty string.
"""
monthnum = int(monthnum)
months = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec"
}
if monthnum is None or monthnum < 1 or monthnum > 12:
return ""
return months[monthnum]
def formatDate(datestring):
# yyyy-mm-dd to dd MMM yyyy
x = datestring.split("-")
if len(x) < 3:
return ""
return "{} {} {}".format(x[2], num_to_month(x[1]), x[0])
def formatCategory(cat):
return cat.replace("-", " ")
def formatNumber(val):
if val is None:
return "None"
return format(val, ",d")
def formatRepo(val):
printables = {
'biorxiv': 'bioRxiv',
'medrxiv': 'medRxiv',
'all': 'all'
}
return printables[val]
def findCategory(needle, haystack):
for x in haystack:
if x["category"] == needle:
return x
def rxapi(uri, headers=False):
# The "headers" flag controls whether the response headers are returned
get = requests.get(
"{}{}".format(config.rxapi, uri),
headers={"Referer": "rxivist.org"}
)
if get.status_code == 404:
raise NotFoundError(uri)
if headers:
return (get.json(), get.headers)
return get.json()