forked from Inist-CNRS/meta-doi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (153 loc) · 4.77 KB
/
index.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
'use strict';
var request = require('request');
var pid;
exports.resolve = function(doi, options, cb) {
var r = {};
exports.APIquery(doi, function(err, response) {
if (err) {
return cb(err);
}
if (response === null) {
return cb(null, {});
}
if (!response) {
return cb(new Error('no response'));
}
if (typeof response !== 'object') {
return cb(new Error('response is not a valid object'));
}
if (options.auth) {
return cb(null, response);
} else {
if (!response.message) {
return cb(new Error('response object has no message'));
}
if (response['message-type'] === 'work') {
return cb(null, exports.APIgetInfo(response.message, options.extended))
}
if (response['message-type'] === 'work-list' && Array.isArray(response.message.items)) {
var list = response.message.items.map(function(item) {
return exports.APIgetInfo(item, options.extended);
});
return cb(null, list);
}
return cb(null, {});
}
}, options.auth);
};
/**
* Query Crossref and get results
* @param {Object} doi : doi to search metadata for
* @param {Function} callback(err, result)
* @param {Array} auth : [ login, password ]
*/
exports.APIquery = function(doi, callback, auth) {
var url = 'http://api.crossref.org/works';
if (auth && !Array.isArray(doi)) {
url = 'https://doi.crossref.org/search/doi?pid=' + auth.join(':') + '&format=json&doi=' + encodeURIComponent(doi)
} else {
if (Array.isArray(doi)) {
url += '?rows=' + doi.length + '&filter=doi:' + doi.join(',doi:');
} else {
url += '/' + encodeURIComponent(doi);
}
}
request.get(url, {
'timeout': 120000
}, function(err, res, body) {
if (err) {
return callback(err);
}
if (res.statusCode === 404) {
// doi not found
return callback(null, null);
} else if (res.statusCode !== 200) {
var error = new Error('Unexpected status code : ' + res.statusCode);
error.url = url;
error.response = res;
return callback(error);
}
var info;
try {
info = JSON.parse(body);
} catch (e) {
return callback(e);
}
if (auth) {
return callback(null, info);
}
// if an error is thrown, the json should contain the status code and a detailed message
if (info.status !== 'ok') {
var error = new Error('got an unknown error from the API');
error.message = info.message;
return callback(error);
}
callback(null, info);
});
};
exports.APIgetPublicationDateYear = function(apiResult) {
if (apiResult.message !== undefined && apiResult.message.issued !== undefined) {
return apiResult.message.issued['date-parts'][0][0];
}
return {};
};
exports.APIgetPublicationTitle = function(apiResult) {
if (apiResult.message !== undefined && typeof apiResult.message['container-title'] !== undefined) {
return apiResult.message['container-title'][0];
}
return {};
};
exports.APIgetInfo = function(doc, extended) {
var info = {
'doi-publication-title': '',
'doi-publication-date-year': '',
'doi-publisher': '',
'doi-type': '',
'doi-ISSN': '',
'doi-subject': '',
'doi-author': '',
};
if (extended) {
info['doi-license-content-version'] = '';
info['doi-license-URL'] = '';
}
if (typeof doc !== 'object' || doc === null) {
return info;
}
// search standard information
info['doi-publication-title'] = doc['container-title'];
info['doi-publisher'] = doc['publisher'];
info['doi-type'] = doc['type'];
info['doi-ISSN'] = doc['ISSN'];
info['doi-DOI'] = doc['DOI'];
info['doi-subject'] = doc['subject'];
info['doi-author'] = doc['author'];
if (typeof doc.issued === 'object' &&
doc.issued['date-parts'] &&
doc.issued['date-parts'][0] &&
doc.issued['date-parts'][0][0]) {
info['doi-publication-date-year'] = doc.issued['date-parts'][0][0];
}
// search licence informations
if (extended && doc['license'] && doc['license'][0]) {
info['doi-license-content-version'] = doc['license'][0]['content-version'];
info['doi-license-URL'] = doc['license'][0]['URL'];
}
return info;
};
exports.DOIgetPublicationDateYear = function(doc) {
var publication_date = doc.crossref_result.query_result.body.query.doi_record.crossref.journal.journal_article.publication_date;
var publication_date_year;
if (typeof publication_date === 'object') {
if (typeof publication_date[0] === 'object') {
publication_date_year = publication_date[0].year;
} else if (publication_date.year === undefined) {
publication_date_year = "unknown";
} else {
publication_date_year = publication_date.year;
}
} else {
publication_date_year = "unknown";
}
return publication_date_year;
};