-
Notifications
You must be signed in to change notification settings - Fork 1
/
getdata.js
82 lines (70 loc) · 1.84 KB
/
getdata.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
var querystream = require('rest-collection-stream')
var through = require('through2')
var pumpify = require('pumpify')
var ProgressBar = require('progress')
var fs = require('fs')
var API_URL = 'http://api.crossref.org/v1/members'
var doi2pub = {}
var n = 0
var progress
var query = {
offset: 0,
rows: 1000
}
pumpify.obj(resultstream(), storestream())
function resultstream () {
console.error('Syncing CrossRef DOI data')
return querystream(API_URL, {
qs: query,
data: getdata,
next: getnext
})
}
function storestream () { return through.obj(storeone, done) }
function getdata (res, body) {
handleErrors(res, body)
doProgress(body)
var items = body.message.items
n += items.length
return items
}
function getnext (res, body) {
return n < body.message['total-results'] ? { offset: body.message.query['start-index'] + 1000 } : null
}
function doProgress (body) {
if (progress) {
progress.tick(body.message.items.length)
} else {
progress = new ProgressBar('Results downloaded: :percent :bar (:current/:total)', {
width: 100,
total: body.message['total-results']
})
}
}
function handleErrors (res, body) {
if (body.status === 'failed') {
console.error('Received an error from the CrossRef API:')
body.message.forEach(function (error) {
console.error(error.message)
})
process.exit(1)
}
}
function storeone (data, enc, done) {
if (data.prefix) {
data.prefix.forEach(function (entry) {
doi2pub[entry.value] = {
publisher: data['primary-name'],
owner: entry.name
}
})
}
done()
}
function done(cb) {
console.error('')
console.error('Successfully retrieved', n, 'entries from CrossRef')
fs.writeFileSync('./data.json', JSON.stringify(doi2pub))
console.error('Wrote', Object.keys(doi2pub).length, 'DOI prefix mappings to ./data.json')
cb()
}