-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_metrics.py
37 lines (30 loc) · 1.12 KB
/
get_metrics.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
from urllib.request import Request, urlopen
from urllib.parse import urlencode
import json
import os
import sys
token = os.getenv("ADS_DEV_KEY", "")
def get_metrics(clobber=False):
if clobber or not os.path.exists("metrics.json"):
url = "https://api.adsabs.harvard.edu/v1/metrics"
bibcodes = []
with open("bibcodes.txt", "r") as f:
for line in f.readlines():
bibcodes.append(line.replace("\n", ""))
bibcodes = '"' + '", "'.join(bibcodes) + '"'
data = '{"bibcodes": [%s], "types":["timeseries"]}' % bibcodes
req = Request(url, data.encode("ascii"))
req.add_header("Authorization", "Bearer %s" % token)
req.add_header("Content-Type", "application/json")
content = urlopen(req).read()
metrics = json.loads(content)
with open("metrics.json", "w") as json_file:
json.dump(metrics, json_file)
else:
print("Using cached metrics.")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "--clobber":
clobber = True
else:
clobber = False
get_metrics(clobber=clobber)