generated from technologiestiftung/template-repo-citylab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_get_ckan_data.js
66 lines (56 loc) · 1.56 KB
/
1_get_ckan_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
"use strict";
const fs = require("fs");
function getCKANData(mainCallback) {
console.log("1. getCKANData ...");
const baseUrl = "https://datenregister.berlin.de/api/3/action/package_search";
const rowsPerPage = 1000;
let start = 0;
let allEntries = [];
const fetchAllEntries = async () => {
while (true) {
const url = `${baseUrl}?start=${start}&rows=${rowsPerPage}`;
console.log("fetching", start, " ");
try {
const response = await fetch(url);
const data = await response.json();
if (data.success) {
const packages = data.result.results;
if (packages.length === 0) {
// No more results, break the loop
break;
}
// Add the current page of results to the allEntries array
allEntries = allEntries.concat(packages);
start += rowsPerPage;
} else {
console.error("API request failed:", data.error);
break;
}
} catch (error) {
console.error("Error making API request:", error);
break;
}
}
return allEntries;
};
async function sumFileFormats() {
fetchAllEntries()
.then((data) => {
fs.writeFile(
"./scraper/data/ckan_data.json",
JSON.stringify(data),
{
encoding: "utf8",
},
(err) => {
mainCallback();
}
);
})
.catch((error) => {
console.error("Error fetching entries:", error);
});
}
sumFileFormats();
}
module.exports = { getCKANData };