-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
114 lines (90 loc) · 2.85 KB
/
fetch.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
import sys
import requests
import json
import csv
import os
'''
url = 'https://prereview.org/api/v2/preprints?limit=1'
url = 'https://prereview.org/api/v2/preprints/doi-10.1101/2021.03.04.433973v1'
url = 'https://prereview.org/api/v2/preprints/doi-10.1101-2021.09.09.459577/full-reviews'
'''
url = 'https://prereview.org/api/v2/preprints/'
headers = {"Accept": "application/json","Content-Type": "application/json"}
doi = "doi-10.1101-2021.07.28.21260814" #Random DOI that contains a couple of rapid reviews
input_data = []
def save_json(name,data):
file_name = str(name)+".txt"
with open(file_name, 'w+') as outfile:
json.dump(data, outfile)
def create_directory(name):
if not os.path.exists(name):
os.makedirs(name)
'''
Function to prepare the doi for the curl request
Not sure how they modify DOIs for their API, seems like they only replace "/" by "-", but there might be other things.
'''
def prepare_doi_string(input_doi):
#We need to find and replace "/" by "-"
input_doi = input_doi.replace("/", "-")
#If we only needed to replace the fist one, we just use this line instead.
#input_doi = input_doi.replace("/", "-",1)
doi = "doi-"+input_doi
arxiv = "arxiv-"+input_doi
return [doi,arxiv]
def read_command_line_input():
if len(sys.argv) <= 1:
print("You need to provide an argument to the program. A list of dois in a csv")
return -1
else:
return sys.argv[1]
def parse_command_line(input):
global input_data
if ".csv" in input:
print("CSV Input provided")
with open(input, newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
input_data.append(row[0])
else:
print("You need to provide an argument to the program. The path of the csv containing DOIs")
input_data = -1
def process_request(request_return,doi):
create_directory(str(doi))
dir_name="./"+str(doi)
os.chdir(dir_name)
request_return = json.loads(request_return.text)
if "data" in request_return:
data = request_return["data"][0]
if "rapidReviews" in data:
rreviews = data["rapidReviews"]
i = 0
for rr in rreviews:
i+=1
json_object = json.dumps(rr, indent = 4)
save_json(i,json_object)
print("Rapid Review:")
print(rr)
print("-----------")
print()
os.chdir("../")
def send_request(doi):
#First we create a directory for that DOI
final_url = url+doi[0]
print(final_url)
r = requests.get(final_url, headers=headers)
print(r.status_code)
if r.status_code == 200:
process_request(r,doi[0])
elif r.status_code == 404: #document not found, we try arxiv instead
final_url = url+doi[1]
r = requests.get(final_url, headers=headers)
if r.status_code == 200:
process_request(r,doi[1])
command_line = read_command_line_input()
if command_line != -1:
parse_command_line(command_line)
if command_line!=-1:
print(len(input_data))
for doi in input_data:
doi = prepare_doi_string(doi)
send_request(doi)