-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoi.py
166 lines (137 loc) · 5.37 KB
/
doi.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
import datetime
import requests
from app import db
from source import make_event_source
from event import CedEvent, MetadataCache, ManyEventsCache
from util import requests_retry_session
class Doi(object):
def __init__(self, doi):
self.doi = doi
self.metadata = CrossrefMetadata(self.doi)
self.open_access = OaDoi(self.doi)
self.altmetrics = AltmetricsForDoi(self.doi)
def get(self):
self.metadata.get()
self.open_access.get()
self.altmetrics.get()
def altmetrics_dict_including_unpaywall_views(self):
altmetrics_value = self.altmetrics.to_dict()
return altmetrics_value
def is_cached_not_expired(self):
cached_item = ManyEventsCache.query.get(self.doi)
expired = datetime.datetime.today() - datetime.timedelta(days=7)
if cached_item and cached_item.updated > expired:
return True
def cached_response(self):
cached_item = ManyEventsCache.query.get(self.doi)
return cached_item.api_raw
def save_to_cache(self, response):
num_events = 0
for item in response["altmetrics_sources"]:
num_events += item["events_count"]
# cache response if DOI has large number of events
if num_events > 1000:
existing_cache_item = ManyEventsCache.query.get(self.doi)
if existing_cache_item:
# cache expired, need to update
existing_cache_item.updated = datetime.datetime.utcnow()
existing_cache_item.api_raw = response
db.session.commit()
else:
# not in case, save as new
new_cache_item = ManyEventsCache(id=self.doi, api_raw=response)
db.session.add(new_cache_item)
db.session.commit()
def to_dict(self):
altmetrics = self.altmetrics.to_dict()
ret = {
"doi": self.doi,
"altmetrics_sources": altmetrics["sources"],
"crossref_event_data_url": altmetrics["crossref_event_data_url"],
"metadata": self.metadata.to_dict(),
"open_access": self.open_access.to_dict(),
}
return ret
class AltmetricsForDoi(object):
def __init__(self, doi):
self.doi = doi
self.ced_url = "https://api.eventdata.crossref.org/v1/events?rows=1000&filter=from-collected-date:1990-01-01,until-collected-date:2099-01-01,obj-id:{}".format(
self.doi
)
self.events = []
self.sources = []
def get(self):
# """
# Handy test data:
# 10.2190/EC.43.3.f # no events
# 10.1371/journal.pone.0000308 # many events, incl lots of wiki
# """
ced_events = CedEvent.query.filter(CedEvent.doi == self.doi).limit(2500).all()
for ced_event in ced_events:
self.add_event(ced_event)
def add_event(self, ced_event):
source_id = ced_event.source.id
# get the correct source for this event
my_source = None
for source in self.sources:
if source.id == source_id:
my_source = source
break
# we don't have this source for this DOI.
# make it and add it to the list
if my_source is None:
my_source = make_event_source(ced_event.source)
self.sources.append(my_source)
# this source exists now for sure because we either found it or made it
# add the event to the source.
my_source.add_event(ced_event.api_raw)
def to_dict(self):
ret = {
"crossref_event_data_url": self.ced_url,
"sources": [s.to_dict() for s in self.sources],
}
return ret
class OaDoi(object):
def __init__(self, doi):
self.doi = doi
self.url = "https://api.oadoi.org/v2/{}".format(doi)
self.data = {}
def get(self):
r = requests_retry_session(retries=2).get(
self.url + "?email=team+paperbuzz@ourresearch.org", timeout=10
)
if r.status_code == 200:
self.data = r.json()
def to_dict(self):
self.data["oadoi_url"] = self.url
return self.data
class CrossrefMetadata(object):
def __init__(self, doi):
self.doi = doi
self.url = "https://api.crossref.org/works/{}/transform/application/vnd.citationstyles.csl+json".format(
doi
)
self.data = {}
def get(self):
cached_item = MetadataCache.query.get(self.doi)
expired = datetime.datetime.today() - datetime.timedelta(6 * 365 / 12)
if cached_item and cached_item.updated > expired:
self.data = cached_item.api_raw
else:
r = requests.get(self.url, timeout=10)
if r.status_code == 200:
self.data = r.json()
self.save_to_cache()
def to_dict(self):
self.data["crossref_url"] = self.url
return self.data
def save_to_cache(self):
existing_cache_item = MetadataCache.query.get(self.doi)
if existing_cache_item:
existing_cache_item.updated = datetime.datetime.utcnow()
existing_cache_item.api_raw = self.data
db.session.commit()
else:
new_cache_item = MetadataCache(id=self.doi, api_raw=self.data)
db.session.add(new_cache_item)
db.session.commit()