This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch_albatross.js
104 lines (99 loc) · 2.61 KB
/
fetch_albatross.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
// Based on
// 1. https://gist.github.com/jayperryworks/5fbe47b1f3c14c826eee5d25eda2015f
// 2. https://moonhighway.com/fetching-data-from-a-graphql-api
// https://github.com/node-fetch/node-fetch
const fetch = require("node-fetch");
const fs = require("fs");
!fs.existsSync("source/data/albatross") &&
fs.mkdirSync("source/data/albatross");
const url = "https://api.pacific-child.org/graphql";
const countryArray = [
"FJI",
"KIR",
"NIU",
"COK",
"FSM",
"WSM",
"NRU",
"PLW",
"PNG",
"MHL",
"SLB",
"TKL",
"TON",
"TUV",
"VUT",
];
const queryTopics = `
query {
topics {
id
}
}
`;
const optsTopics = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: queryTopics }),
};
(async () => {
const topics = await fetch(url, optsTopics)
.then((res) => res.json())
.then((res) => {
return res.data.topics;
})
.catch(console.error);
const forLoop = async (_) => {
console.log("Start");
for (let cindex = 0; cindex < countryArray.length; cindex++) {
console.log(countryArray[cindex]);
const restructuredData = [];
for (let index = 0; index < topics.length; index++) {
await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: `
query {
dataPointValuesForTopic(countryCode: "${countryArray[cindex]}", topicId: ${topics[index].id}){
country
dataPoint {
id
bodyEn
bodyEs
bodyPt
csvColumnName
name
}
}
}
`,
}),
})
.then((res) => res.json())
.then((res) => {
const data = res.data.dataPointValuesForTopic;
for (let index = 0; index < data.length; index++) {
const obj = data[index];
restructuredData.push({
value: obj.country,
dataPointName: obj.dataPoint.csvColumnName,
dataPoint: obj.dataPoint,
});
}
return restructuredData;
})
.catch(console.error);
}
fs.writeFile(
`source/data/albatross/countries_${countryArray[cindex]}.json`,
JSON.stringify(restructuredData),
(error) => {
if (error) throw error;
}
);
}
console.log("End");
};
forLoop();
})();