-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
128 lines (112 loc) · 3.45 KB
/
parser.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
'use strict';
const et = require('elementtree');
const { Element } = et;
const isDetailed = entries => entries.every(node => node.tag === 'plant');
const pascalCase = str => str[0].toUpperCase() + str.substring(1).toLowerCase();
const withLabel = (obj, label) => Object.assign(obj, { label });
const withReferences = obj => Object.assign(obj, { refs: getReferences(obj) });
const withXmlWritter = (obj, xmltree, attr = {}) => Object.assign(obj, {
toXML(options = {}) {
options.indent = options.indent || 2;
const rootNode = xmltree.getroot();
Object.keys(attr).forEach(key => rootNode.set(key, attr[key]));
return xmltree.write(options).trim();
}
});
const Level = {
High: Symbol('high'),
Moderate: Symbol('moderate'),
Low: Symbol('low'),
fromId(id) {
const level = pascalCase(id.replace(/ALLERGY_INDICATOR_LEVEL_/, ''));
return Level[level];
}
};
const Type = {
Combined: 'combined',
Detailed: 'detailed'
};
module.exports = {
Level,
Type,
parseCities,
parsePollenData
};
function parseCities(xml) {
const xmltree = et.parse(xml);
const cities = xmltree.findall('.//city').map(node => ({
name: node.findtext('./name'),
url: node.findtext('./link')
}));
return withXmlWritter(cities, xmltree);
}
function parsePollenData(xml) {
const xmltree = et.parse(xml);
const entries = xmltree.getroot().getchildren();
if (isDetailed(entries)) {
const data = entries.map(node => ({
id: parseInt(node.get('id'), 10),
name: node.findtext('./name'),
records: parseDaily(node.find('./daily'))
}));
const result = { type: Type.Detailed, data };
return withXmlWritter(withReferences(result), xmltree, { type: result.type });
}
const tree = withLabel(parseCategory(xmltree.find('./tree')), 'Drveće');
const grass = withLabel(parseCategory(xmltree.find('./grass')), 'Trava');
const weed = withLabel(parseCategory(xmltree.find('./weed')), 'Korovi');
const data = { tree, weed, grass };
const result = { type: Type.Combined, data };
return withXmlWritter(withReferences(result), xmltree, { type: Type.Combined });
}
function parseCategory(category) {
category = category || new Element();
return {
records: parseDaily(category.find('./daily')),
prevalent: parsePrevalent(category.find('./prevails'))
};
}
function parseDaily(days) {
days = days || new Element();
return days.findall('./day').map(node => parseData(node));
}
function parseData(day) {
day = day || new Element();
const date = day.findtext('./date');
const type = day.findtext('./type');
const level = day.find('./level') || day.find('./value');
const value = parseValue(day.find('./value'));
const data = {
date,
level: {
id: level.get('id'),
value: Level.fromId(level.get('id')),
label: level.text
}
};
if (value) data.value = value;
if (type) data.type = type;
return data;
}
function parseValue(value) {
if (!value || value.get('id')) return null;
return parseFloat(value.text);
}
function parsePrevalent(prevalent) {
prevalent = prevalent || new Element();
return prevalent.getchildren().map(node => ({
id: parseInt(node.get('id'), 10),
name: node.text
}));
}
function getReferences({ type, data }) {
const sortById = (a, b) => a.id > b.id;
if (type === Type.Detailed) {
return data.map(({ id, name }) => ({ id, name })).sort(sortById);
}
return [
...data.tree.prevalent,
...data.weed.prevalent,
...data.grass.prevalent
].sort(sortById);
}