-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
200 lines (160 loc) · 6.97 KB
/
start.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const fs = require('fs');
const axios = require('axios').default;
const cheerio = require('cheerio');
const jsondiffpatch = require('jsondiffpatch');
const { IncomingWebhook } = require('@slack/webhook');
require('dotenv').config();
const SOURCES = [
{
key: 'bevoelkerung-demokratie',
title: 'Bevölkerung & Demographie',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12810.1&La=1&'
},
{
key: 'bildung-kultur-sport',
title: 'Bildung, Kultur & Sport',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12811.1&La=1&'
},
{
key: 'freizeit-tourismus',
title: 'Freizeit & Tourismus',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12814.1&La=1&'
},
{
key: 'geographie-geobasisdaten-stadtplanung',
title: 'Geographie, Geobasisdaten & Stadtplanung',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12815.1&La=1&'
},
{
key: 'gesundheit-soziales',
title: 'Gesundheit & Soziales',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12816.1&La=1&'
},
{
key: 'haushalt-steuern',
title: 'Haushalt & Steuern',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12812.1&La=1&'
},
{
key: 'infrastruktur-bauen-wohnen',
title: 'Infrastruktur, Bauen & Wohnen',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12817.1&La=1&'
},
{
key: 'oeffentliche-verwaltung',
title: 'Öffentliche Verwaltung',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12813.1&La=1&'
},
{
key: 'politik-wahlen',
title: 'Politik & Wahlen',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12818.1&La=1&'
},
{
key: 'transport-verkehr',
title: 'Transport & Verkehr',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12821.1&La=1&'
},
{
key: 'umwelt-klima',
title: 'Umwelt & Klima',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12819.1&La=1&'
},
{
key: 'wirtschaft-arbeit',
title: 'Wirtschaft & Arbeit',
url: 'https://www.magdeburg.de/Start/B%C3%BCrger-Stadt/Verwaltung-Service/Offene-Verwaltungsdaten/index.php?NavID=37.906&object=tx|37.12820.1&La=1&'
}
];
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
function forceTargetDirectory(source) {
if (!fs.existsSync(`./generated/${source.key}`)) {
fs.mkdirSync(`./generated/${source.key}`, { recursive: true });
}
}
async function fetchSnapshot(source) {
return axios
.get(source.url)
.then(response => response.data);
}
async function parseSnapshot(htmlSnapshot) {
const $ = cheerio.load(htmlSnapshot);
return $('.ovd_element')
.map((index, elem) => {
const titleElem = $('.toggler_titel .ovd_title', elem);
const title = titleElem.text().trim();
const subtitleElem = titleElem.next();
const subtitle = subtitleElem.text().trim();
const detailsElem = $('.toggler_container', elem);
const descriptionElem = detailsElem.children().first();
const description = descriptionElem.text().trim();
const detailsRowElems = $('.row', detailsElem);
const details = detailsRowElems
.map((index, rowElem) => {
const labelElem = $('label', rowElem);
const spanElem = $('span', rowElem);
return { label: labelElem.text().trim(), value: spanElem.html().trim() };
})
.toArray();
const downloadLinkElement = $('a[title="Dokument anzeigen"]', elem);
const downloadLink = downloadLinkElement.attr('href');
return { title, subtitle, description, details, downloadLink };
})
.toArray();
}
function loadLatestSnapshot(source) {
const filename = `./generated/${source.key}/latest-snapshot.json`;
if (fs.existsSync(filename)) {
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
}
return null;
}
function checkChanges(oldSnapshot, newSnapshot) {
return jsondiffpatch.diff(oldSnapshot, newSnapshot);
}
function saveDiff(oldSnapshot, newSnapshot, diff, source) {
const now = new Date();
const json = { date: now.toISOString(), oldSnapshot, newSnapshot, diff };
const filename = `./generated/${source.key}/diff-${now.toISOString()}.json`;
fs.writeFileSync(filename, JSON.stringify(json, null, 2));
}
function saveLatestSnapshot(snapshot, source) {
const filename = `./generated/${source.key}/latest-snapshot.json`;
fs.writeFileSync(filename, JSON.stringify(snapshot, null, 2));
}
async function sendDiffNotificationViaSlack(source) {
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);
return webhook.send({ text: `Updates found in "${source.title}"` });
}
(async () => {
/*
const filename = '/Users/jens/git/CodeForMD/md-verwaltungsdaten-scraper/generated/politik-wahlen/diff-2023-02-04T15:28:03.238Z.json';
const delta = JSON.parse(fs.readFileSync(filename, 'utf-8'));
const output = jsondiffpatch.formatters.console.format(delta.diff);
console.log(output);
return;
*/
if (!SLACK_WEBHOOK_URL) {
console.error('Stopping process. Slack WebHook url is missing.');
process.exit(1);
}
for (const source of SOURCES) {
console.log(`Processing "${source.title}"`);
forceTargetDirectory(source);
const htmlSnapshot = await fetchSnapshot(source);
const snapshot = await parseSnapshot(htmlSnapshot);
const latestSnapshot = await loadLatestSnapshot(source) || [];
const diff = checkChanges(latestSnapshot, snapshot);
if (diff) {
console.log('Found updates!');
saveDiff(latestSnapshot, snapshot, diff, source);
saveLatestSnapshot(snapshot, source);
sendDiffNotificationViaSlack(source)
.then(console.log)
.catch(console.error)
} else {
console.log('No updates.');
}
}
console.log('Done.');
})();