Skip to content

Commit 238b07d

Browse files
Merge pull request #20 from alex-fdias/main
#12 fixing missing local "data" folder
2 parents 7fd3d56 + 8432669 commit 238b07d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/scrapers/leipzig-statistik.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import fetch from 'node-fetch';
22
import fs from 'fs';
33
import slugify from '@sindresorhus/slugify';
44

5-
const __dirname = new URL('.', import.meta.url).pathname;
5+
import { dirname, join } from 'path';
6+
import { fileURLToPath } from 'url';
7+
8+
const save_dir = join(dirname(fileURLToPath(import.meta.url)), '../..', 'public/data-raw');
69

710
const prefix = `https://statistik.leipzig.de/opendata/api/kdvalues?format=json`;
8-
//const prefix = `https://statistik.leipzig.de/opendata/api/kdvalues?format=json`;
11+
912
const urls = [
1013
`${prefix}&kategorie_nr=2&rubrik_nr=5&periode=y`, // Einwohner mit Migrationshintergund
1114
`${prefix}&kategorie_nr=2&rubrik_nr=9&periode=y`, // Einwohnerdichte
@@ -24,21 +27,31 @@ const urls = [
2427
]
2528

2629
;(async () => {
30+
try {
31+
if (!fs.existsSync(save_dir)) {
32+
fs.mkdirSync(save_dir);
33+
}
34+
} catch (err) {
35+
console.error(err);
36+
}
37+
38+
const num_digits = Math.ceil(Math.log10(urls.length))
2739
for (let i = 0; i < urls.length; i++) {
2840
const response = await fetch(urls[i]);
41+
process.stdout.write('[' + String(i+1).padStart(num_digits, '0') + '/' + urls.length + '] \"' + urls[i] + '\": ')
2942
const data = await response.json();
3043
if (data && data.length > 0) {
3144
let filename = `leipzig-${slugify(data[0].name)}`
32-
console.log(filename, 'saved')
33-
fs.writeFileSync(`${__dirname}../../public/data-raw/${filename}.json`, JSON.stringify({
45+
fs.writeFileSync(join(save_dir, `${filename}.json`), JSON.stringify({
3446
meta: {
3547
source: urls[i],
3648
title: data[0].name
3749
},
3850
data
3951
}, null, 2))
52+
console.log(`data saved to ${filename}.json `)
4053
} else {
41-
console.log("No data found for " + urls[i])
54+
console.log("no data found ")
4255
}
4356
}
4457
})()

0 commit comments

Comments
 (0)