-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtracteurUnpaywall.js
292 lines (248 loc) · 9.97 KB
/
ExtracteurUnpaywall.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Extracteur Unpaywall, v1.2, 2019-06-13
Évaluer la proportion des publications en libre accès parmi une liste de DOI
Copyright (C) 2018-2019 - Romain Boistel, Frédérique Bordignon, Philippe Gambette
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
"use strict";
$(document).ready(function(){
// DOI list
var doi = [];
// Index of the latest DOI we tried to test
var lastSentQuery = -1;
// Index of the latest DOI we managed to test
var lastReceivedQuery = -1;
// Information about the latest DOI we tested
var info = new Object();
// Number of publications in open access
var oa = 0;
// Timer to regularly launch queries
var timer = "";
// Result
var result = [];
var oaByYear = new Object();
var publicationsByYear = new Object();
var graphDisplayed = false;
$("#send").on("click",function(){
// Save values extracted from the DOI list in the table doi
doi = $("#doiList").val().split("\n");
// Reinitialize lastSentQuery, lastReceivedQuery and info
lastSentQuery = -1;
lastReceivedQuery = -1;
oa = 0;
// Reinitialize the table
result = [["DOI","bilan OA","article en OA d'après Unpaywall ?","revue en OA d'après Unpaywall ?","revue dans le DOAJ d'après Unpaywall ?","Meilleure source du texte intégral selon Unpaywall","Identifiant HAL du dépôt","nb de notices HAL pour ce DOI","article en OA via HAL + ISTEX ?","URL article OA via HAL + ISTEX","texte intégral dans HAL ?","Année de publication"]];
$("#results").html('<tr id="titleRow"></tr>');
result[0].forEach(function(col){
$("#titleRow").append("<th>"+col+"</th>");
});
oaByYear = new Object();
publicationsByYear = new Object();
graphDisplayed = false;
// Test a new DOI every second, in case the previous test was finished
timer = setInterval(sendQuery,1000);
})
function sendQuery(){
// Test the next DOI if the previous one was already tested
if(lastSentQuery==lastReceivedQuery){
// Reinitialize the object to store information about the next publication
info = new Object();
// Send a new query
lastSentQuery += 1;
if(lastSentQuery == doi.length){
// All DOI have been sent, stop sending queries
clearInterval(timer);
$("#progress").html("100");
} else {
// Send the next DOI to Unpaywall
$.get("https://api.unpaywall.org/v2/"+doi[lastSentQuery]+"?email=" + $("#email").val())
.always(receiveUnpaywall);
}
}
}
// Save data received from Unpaywall
function receiveUnpaywall(data){
if (data != undefined){
// Unpaywall has found the DOI
info.doi = data.doi;
info.is_oa = data.is_oa;
info.journal_is_oa = data.journal_is_oa;
info.journal_is_in_doaj = data.journal_is_in_doaj;
info.year = data.year;
if (data.best_oa_location != undefined){
info.best_oa_location = "<a href=\""+data.best_oa_location.url+"\">"+data.best_oa_location.evidence+"</a>";
}
}
// Send query about the DOI to HAL
$.get("https://api.archives-ouvertes.fr/search/?wt=json&fq=doiId_s:("
+ doi[lastSentQuery].replace(/\(/g,"\\(").replace(/\)/g,"\\)").replace(/:/g,"\\:")
+ ")&fl=halId_s,fileMain_s,linkExtId_s,linkExtUrl_s,producedDateY_i,uri_s")
.done(receiveHal);
}
// Save data received from HAL
function receiveHal(data){
if(data.response.docs.length>0){
// HAL has found the DOI
info.halNb = data.response.numFound;
info.halId = '<a href="'+data.response.docs[0].uri_s+'">'+data.response.docs[0].halId_s+"</a>";
info.linkExtId = data.response.docs[0].linkExtId_s;
info.linkExtUrl = data.response.docs[0].linkExtUrl_s;
if(data.response.docs[0].linkExtUrl_s!=undefined){
info.linkExtUrl = "<a href=\""+data.response.docs[0].linkExtUrl_s+"\">"+data.response.docs[0].linkExtUrl_s+"</a>";
}
if(data.response.docs[0].fileMain_s!=undefined){
info.fileMain = "<a href=\""+data.response.docs[0].fileMain_s+"\">"+data.response.docs[0].fileMain_s+"</a>";
}
if(info.year==undefined){
info.year = data.response.docs[0].producedDateY_i;
}
}
// Prepare values for the next table row to be added
var col3 = "non";
if(info.is_oa){col3 = "oui";}
var col4 = "non";
if(info.journal_is_oa){col4 = "oui";}
var col5 = "non";
if(info.journal_is_in_doaj){col5 = "oui";}
var col2 = "non-OA";
if((col3 == "oui")||(col4 == "oui")||(col5 == "oui")||(info.best_oa_location != undefined)||((info.linkExtId != undefined)&&(info.linkExtId != "istex"))||(info.fileMain != undefined)){
col2 = "OA";
// increase the number of open access publications found
oa += 1;
}
// Add a new row to the table
$("#results").find("tbody").append("\n<tr>"
+"<td><small><a href=\"http://dx.doi.org/"+doi[lastSentQuery]+"\">"+doi[lastSentQuery]+"</a></small></td>"
+"<td>"+col2+"</td>"
+"<td>"+col3+"</td>"
+"<td>"+col4+"</td>"
+"<td>"+col5+"</td>"
+"<td>"+info.best_oa_location+"</td>"
+"<td>"+info.halId+"</td>"
+"<td>"+info.halNb+"</td>"
+"<td>"+info.linkExtId+"</td>"
+"<td><small>"+info.linkExtUrl+"</small></td>"
+"<td><small>"+info.fileMain+"</small></td>"
+"<td>"+info.year+"</td>"
+"</tr>");
var resultRow = [doi[lastSentQuery],col2, col3, col4, col5,info.best_oa_location, info.halId, info.halNb,info.linkExtId,info.linkExtUrl,info.fileMain,info.year];
result[lastSentQuery+1] = resultRow;
// Go the next DOI query
lastReceivedQuery += 1;
// Update the progress rate and the open access rate
$("#progress").html(parseInt(1000.0*(lastReceivedQuery+1)/doi.length)/10);
$("#oa").html(parseInt(1000.0*oa/(lastReceivedQuery+1))/10);
// Update the graph and the table of number of publications per year used to build it
if(info.year == undefined){
info.year = "undefined";
}
if(publicationsByYear[info.year] == undefined){
publicationsByYear[info.year] = 1;
oaByYear[info.year] = 0;
} else {
publicationsByYear[info.year] += 1;
}
if(col2 == "OA"){
oaByYear[info.year] += 1;
}
// Build the graph displaying the open access evolution by year
var years = [];
var oaPercentage = [];
var i = 0;
Object.keys(publicationsByYear).forEach(function(year){
// Consider only years with at least 5 publications
if(year != "undefined" && publicationsByYear[year]>4){
years[i] = year;
oaPercentage[i] = parseInt((100.0*oaByYear[year])/(publicationsByYear[year]));
i += 1;
}
});
if(years.length>0){
if(graphDisplayed){
Plotly.react( oaEvolution, [{
x: years,
y: oaPercentage }], {
margin: { t: 0 },
xaxis: { autotick: false, ticks: 'outside', tick0: 0, dtick: 1, ticklen: 5, tickwidth: 1, tickcolor: 'gray'}
}, {showSendToCloud:true} );
Plotly.restyle(oaEvolution, {'marker.color': ['lightgreen']});
} else {
Plotly.plot( oaEvolution, [{
x: years,
y: oaPercentage }], {
margin: { t: 0 },
xaxis: { autotick: false, ticks: 'outside', tick0: 0, dtick: 1, ticklen: 5, tickwidth: 1, tickcolor: 'gray'}
}, {showSendToCloud:true} );
graphDisplayed = true;
}
}
}
/************************************/
/* code below used to download the CSV file
/* derived from the code by A.H. Bitubekk
/* from https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
/************************************/
// Download the table containing the results
$("#download").on("click",function(e){
// CSV file
var csvContent = '';
e.preventDefault();
// Building the CSV from the Data two-dimensional array
// Each column is separated by ";" and new line "\n" for next row
result.forEach(function(infoArray,index) {
var dString = "";
infoArray.forEach(function(str){
if(str==undefined){
str="";
} else {
str=""+str;
}
dString += '"'+str.replace(/"/g,'""')+'";';
})
csvContent += index < result.length ? dString + '\n' : dString;
});
// Build date
var m = new Date(Date.now()).getMonth()+1;
if(m<10){
m="0"+m;
}
var d = new Date(Date.now()).getDate();
if(d<10){
d="0"+d;
}
var date = new Date(Date.now()).getFullYear()+"-"+m+"-"+d+"_"+new Date(Date.now()).getHours()+"h"+new Date(Date.now()).getMinutes();
// Save file
download(csvContent, 'download-'+date+'.csv', 'text/csv;encoding:utf-8');
})
// The download function takes a CSV string, the filename and mimeType as parameters
// Scroll/look down at the bottom of this snippet to see how download is called
function download(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) { //html5 A[download]
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
}
/************************************/
}
})