-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchData.js
62 lines (54 loc) · 1.65 KB
/
fetchData.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
/**
* Script to fetch data from google sheet
* Pulls data as well as images
* Changes image paths to relative file paths
* */
const fs = require("fs");
const axios = require("axios");
const { csvParse, csvFormat, autoType } = require("d3");
const {
appConfig: { makeImgPath },
} = require("./src/utils/constants");
const sheetOpt = {
id: "1jg1quJkA0nngDk6mu-DBl7kv0ZYtU4XmLEmrbewTuuI",
gid: 1307304957,
};
const sheetURL = `https://docs.google.com/spreadsheets/u/1/d/${sheetOpt.id}/export?format=csv&id=${sheetOpt.id}&gid=${sheetOpt.gid}`;
axios
.get(sheetURL)
.then(
({ data }) => csvParse(data, autoType),
(err) => console.log("err", err)
)
.then((data) => {
console.log("data", data);
const imagesToFetch = data
.reduce((acc, { name, img1, img2 }) => {
return [
...acc,
img1 ? [makeImgPath(name, "img1"), img1] : [],
img2 ? [makeImgPath(name, "img2"), img2] : [],
];
}, [])
.filter(([d]) => d);
// console.log("imagesToFetch", imagesToFetch);
let errors = [];
imagesToFetch.forEach(
async ([path, url]) =>
await axios({ method: "get", url: url, responseType: "stream" }).then(
(response) => {
response.data.pipe(fs.createWriteStream(path));
},
(err) => {
console.log("err", path, err);
errors.push([path, err]);
}
)
);
const newData = data.map((d) => ({
...d,
img1: d.img1 ? makeImgPath(d.name, "img1") : null,
img2: d.img2 ? makeImgPath(d.name, "img2") : null,
}));
fs.writeFileSync("./public/siteData.csv", csvFormat(newData));
});