-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
183 lines (125 loc) · 5 KB
/
extract.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
import os
import pdb
import json
import pandas as pd
import time
import urllib.parse
import requests
import xmltodict
import json
from datetime import datetime
session = requests.Session()
import http.client
http.client.HTTPConnection.debuglevel = 1
debug = True
if debug:
# keep this off as it displays your credentials as plain text in the output.
import logging
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
headers = {"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"}
# Read a specific environment variable
user = os.environ.get('CMIP')
pwd = os.environ.get('CROSSPWD')
role = 'heso'
def get_info(doi):
# Encode the username and password
encoded_username = urllib.parse.quote(user, safe='')
encoded_password = urllib.parse.quote(pwd, safe='')
# Construct the combined URL with encoded username and password
url = f'https://doi.crossref.org/servlet/getForwardLinks?doi={doi}&usr={encoded_username}/{role}&pwd={encoded_password}'
if debug:
print(url)
# Make the HTTP request and retrieve the XML data
print(datetime.now())
try:
# each of these seem to take a minute.
response = session.get(url,headers=headers)
except ConnectionError:
assert False, 'You must be connected to the internet for this to work.'
print('--done-- ',datetime.now())
xml_data = response.text
# Convert XML to JSON
# json_data = json.dumps(xmltodict.parse(xml_data), indent=4)
return xmltodict.parse(xml_data)['crossref_result']['query_result']['body']['forward_link']
fail = []
def parse_result(data):
for k in data.keys():
if '_cite' in k:
data = data[k]
title = k.replace('cite','title').replace('book','chapter').replace('conf','paper')
medium = k.replace('cite','title').replace('book','volume').replace('conf','volume').replace('report','volume')
break
# ignore condition for places where the year is missing does not exist e.g. 'Forest Service Research Data Archive'
if 'year' not in data:
return
try:
# Extracting the journal title
journal_title = data.get(title,k.split('_')[0])
# Extracting the article title
article_title = data.get(medium) or data.get('series_title') or data['title']
# Extracting the contributors
contributors = []
if 'contributors' in data:
for _,contributor in data['contributors'].items():
if isinstance(contributor,list):
for author in contributor:
# print(author)
name = f"{author.get('given_name','')} {author['surname']}"
contributors.append(name)
else:
name = f"{contributor.get('given_name','')} {contributor['surname']}"
# print(contributor,name)
contributors.append(name)
else:
contributors = ['various']
# contributors = [ f"{contributor['given_name']} {contributor['surname']}" for _,contributor in data['contributors'].items()]
# Extracting the year
year = int(data['year'])
# Extracting the DOI text
doi = data['doi']['#text']
# Returning a dictionary with the extracted values
return {
'journal_title': journal_title,
'article_title': article_title,
'contributors': contributors,
'year': year,
'doi': doi
}
except KeyError as e :
print()
print(e)
print('keyerror',data)
assert False
# except TypeError as e:
# print()
# print(e)
# print([
# fail.append(f"{contributor['given_name']} {contributor['surname']}") for _,contributor in data['contributors'].items()])
# print('fail' , data[title])
# return None
except AssertionError:
...
# doi = '10.5194/gmd-9-2853-2016'
# # test = pd.DataFrame(filter(bool,map(parse_result, get_info(doi))))
papers = pd.read_csv('list.csv')
total = {}
for paper in papers.iterrows():
paper = paper[1]
name = f"{paper.NAME}__{paper.YEAR}__{paper.LEAD}__{paper.DOI}".replace(' ','+').replace('.','(dot)').replace(';','\;').replace('/','(slash)')
filename = f'citation_data/{name}.csv'
if os.path.exists(filename):
data =pd.read_csv(filename)
print('read existing file:',filename)
else:
result = get_info(paper.DOI)
data = pd.DataFrame(filter(bool,map(parse_result,result )))
data.to_csv(filename)
total[paper.NAME] = len(data)
time.sleep(1)
pd.Series(total).to_csv('total_summary.csv')
# groupby year count