-
Notifications
You must be signed in to change notification settings - Fork 0
/
motie-cache2.js
177 lines (157 loc) · 5.04 KB
/
motie-cache2.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
/*
- Drop each motie per year in _data/{year}.json.
- Create a page in moties/{year}/{id}.html
*/
var fs = require('fs');
const topVal = process.argv[2];
let top = ''
if (typeof topVal !== 'undefined') {
top = `&$top=${topVal}`;
}
const urlApi = 'https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0';
const motieQuery = `
Besluit?
&$orderby=ApiGewijzigdOp desc
${top}
&$count=true
&$expand=Zaak(
$expand=ZaakActor(
$select=Id,ActorNaam,ActorFractie
;$filter=Relatie eq 'Indiener' or Relatie eq 'MedeIndiener'
)
;$select=Id,Nummer,Titel,Onderwerp,Kabinetsappreciatie,GestartOp,GewijzigdOp,ApiGewijzigdOp
),
Stemming(
$filter=Verwijderd eq false
;$select=Id,Soort,FractieGrootte,ActorNaam,ActorFractie
)
&$select=Id,BesluitSoort,StemmingsSoort,GewijzigdOp,ApiGewijzigdOp
&$filter=Verwijderd eq false and Zaak/any(a: a/Soort eq 'Motie') and StemmingsSoort ne null`
const url = `${urlApi}/${motieQuery}`
const motieCard = `---
---
{% assign year = page.path | split: "/" | slice: 1 | append: "" | slice: 2,4 %}
{% assign motieId = page.name | replace: ".html" %}
{% assign motie = site.data.moties[year][motieId] %}
{% include motie-card.html %}`;
// console.log(url);
(async () => {
let nextLink = url;
let motieTotal = undefined;
let motieCount = 0;
while (true) {
nextLink = await fetch(nextLink)
.then(response => {
if (response.status != 200) {
console.dir(response, {'depth': null});
return Promise.reject(response);
}
return response.json();
})
.then(odata => {
if (typeof odata.value === 'undefined') {
console.log(odata);
return Promise.reject(odata.error);
}
let nextLink = undefined;
if (typeof odata['@odata.nextLink'] !== 'undefined') {
// If there is more data to retrieve, nextLink is set with the correct $skip value
// Remove $count=true from it, only need that once
nextLink = odata['@odata.nextLink'].replace('$count=true', '');
}
if (typeof odata['@odata.count'] !== 'undefined') {
motieTotal = odata['@odata.count'];
}
return [odata.value, nextLink];
}).then(args => {
let [moties, nextLink] = args;
let motieCache = {};
moties.forEach(motie => {
const motieYear = motie.Zaak[0].GestartOp.slice(0,4);
if (typeof motieCache[motieYear] === 'undefined') {
motieCache[motieYear] = {};
}
motie.Stats = {
'Voor': 0,
'Tegen': 0,
'Anders': 0,
}
if (motie.StemmingsSoort == 'Hoofdelijk') {
motie.Stemming.forEach(stem => {
switch (stem.Soort) {
case 'Voor':
motie.Stats.Voor += 1;
break;
case 'Tegen':
motie.Stats.Tegen += 1;
break;
default:
motie.Stats.Anders += 1;
break;
}
});
} else {
motie.Stemming.forEach(stem => {
switch (stem.Soort) {
case 'Voor':
motie.Stats.Voor += stem.FractieGrootte;
break;
case 'Tegen':
motie.Stats.Tegen += stem.FractieGrootte;
break;
default:
motie.Stats.Anders += stem.FractieGrootte;
break;
}
});
}
motieCache[motieYear][motie.Id] = motie;
fs.writeFile(
`moties/${motieYear}/moties/${motie.Id}.html`,
motieCard, 'utf8', (err) => { if (err) throw err });
});
motieCount += moties.length;
console.log(`${motieCount} / ${motieTotal}`);
return [motieCache, nextLink];
})
.then(args => {
let [motieCache, nextLink] = args;
for (const motieYear in motieCache) {
new Promise((resolve,reject) => {
fs.readFile(`_data/moties/${motieYear}.json`, 'utf-8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// No file, return empty
resolve({});
} else {
reject(err);
}
} else {
resolve(JSON.parse(data));
}
});
})
.then(oldData => {
// console.log(oldData);
// console.log({...oldData, ...motieCache[motieYear]});
fs.writeFile(`_data/moties/${motieYear}.json`,
JSON.stringify({...oldData, ...motieCache[motieYear]}), 'utf8', (err) => { if (err) throw err });
});
}
return nextLink;
}).catch(error => {
// Catch it to retry, seems to happen every now and then
console.log(error);
if (typeof error.cause !== 'undefined' && error.cause.code === 'ECONNRESET') {
console.log(`Error while fetching ${url}, returning it to try again`)
return url;
} else {
throw error;
}
})
// Stop if there are no more odata links, or motieTotal has been reached
if (typeof nextLink == 'undefined' || motieCount > motieTotal) {
break;
}
}
})();