-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.js
69 lines (67 loc) · 2.09 KB
/
data.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
const request = require('request')
const unzip = require('unzipper')
const { parseString } = require('xml2js')
var legacy = require('legacy-encoding')
module.exports = new Promise((resolve, reject) => {
let alimentTable = ''
let compositionTable = ''
let constantTable = ''
let nbFinishedFiles = 0
request('https://ciqual.anses.fr/cms/sites/default/files/inline-files/TableCiqual2017_XML_2017%2011%2021.zip')
.pipe(unzip.Parse())
.on('entry', entry => {
const fileName = entry.path
if (!['alim_2017 11 21.xml', 'compo_2017 11 21.xml', 'const_2017 11 21.xml'].includes(fileName)) {
entry.on('data', () => {})
return
}
entry.on('end', () => {
nbFinishedFiles += 1
let tableToParse = null
switch (fileName) {
case 'alim_2017 11 21.xml':
tableToParse = alimentTable
break
case 'compo_2017 11 21.xml':
tableToParse = compositionTable
break
case 'const_2017 11 21.xml':
tableToParse = constantTable
break
}
parseString(tableToParse, (err, result) => {
if (err) {
reject (err)
}
switch (fileName) {
case 'alim_2017 11 21.xml':
alimentTable = result
break
case 'compo_2017 11 21.xml':
compositionTable = result
break
case 'const_2017 11 21.xml':
constantTable = result
break
}
if (nbFinishedFiles === 3) {
resolve({ alimentTable, compositionTable, constantTable })
}
})
})
const decode = value => legacy.decode(value, 'windows-1252')
entry.on('data', chunk => {
switch (fileName) {
case 'alim_2017 11 21.xml':
alimentTable += decode(chunk)
break
case 'compo_2017 11 21.xml':
compositionTable += decode(chunk)
break
case 'const_2017 11 21.xml':
constantTable += decode(chunk)
break
}
})
})
})