ceiba-scores/scripts/fetchData.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-04-05 16:30:16 +02:00
import { createWriteStream, existsSync, mkdirSync } from "fs";
2022-04-06 13:13:09 +02:00
import sharp from "sharp";
2022-03-29 23:30:23 +02:00
import fs from "fs/promises";
2022-04-05 16:30:16 +02:00
import { pipeline } from "stream";
import { promisify } from "util";
2022-04-05 12:05:12 +02:00
const streamPipeline = promisify(pipeline);
2022-03-29 23:30:23 +02:00
const apiUrl = "https://admin.ceiba-conseil.com";
async function fetchJSONApi(path) {
const url = `${apiUrl}${path}`;
const options = {
method: "GET",
headers: {
"content-type": "application/json",
},
};
console.log(`fetchJSONApi: ${url}`);
const response = await fetch(url, options);
if (!response.ok) {
const errors = await response.json();
throw errors.errors[0].message;
}
return response.json();
}
2022-04-05 12:05:12 +02:00
async function fetchAsset(uuid) {
const url = `${apiUrl}/assets/${uuid}`;
2022-04-05 16:30:16 +02:00
return fetch(url);
2022-04-05 12:05:12 +02:00
}
2022-03-29 23:30:23 +02:00
async function fetchData() {
const fields = [
"*",
"translations.*",
"questions.sort",
"questions.questions_id.*",
"questions.questions_id.translations.*",
"questions.questions_id.answers.*",
"questions.questions_id.answers.answers_id.*",
"questions.questions_id.answers.answers_id.translations.*",
"results.*",
"results.results_id.*",
"results.results_id.translations.*",
];
const url = `/items/scores?${fields
.map((item) => `fields[]=${item}`)
.join("&")}`;
2023-04-24 23:15:13 +02:00
const scores = (await fetchJSONApi(url)).data;
await fs.writeFile("./src/data.json", JSON.stringify(scores), "utf8");
2022-04-05 12:05:12 +02:00
2022-04-05 16:30:16 +02:00
const folder = "public/answers";
2022-04-05 12:05:12 +02:00
if (!existsSync(folder)) mkdirSync(folder);
2023-04-24 23:15:13 +02:00
for (const score of scores) {
const uuid_score = score.image;
if (uuid_score) {
console.log(`Score image : ${folder}/${uuid_score}`);
const response = await fetchAsset(uuid_score);
try {
const thumbnail = sharp().resize({ height: 200 }).webp();
await streamPipeline(
response.body,
thumbnail,
createWriteStream(`${folder}/${uuid_score}.webp`)
);
} catch (err) {
console.log(err);
}
}
2022-04-05 12:05:12 +02:00
for (const question of score.questions) {
for (const answer of question.questions_id.answers) {
2022-04-05 16:30:16 +02:00
const uuid = answer.answers_id.image;
2022-04-05 12:05:12 +02:00
if (uuid) {
2022-04-06 13:13:09 +02:00
console.log(`${folder}/${uuid}`);
2022-04-05 16:30:16 +02:00
const response = await fetchAsset(uuid);
2022-04-06 13:13:09 +02:00
try {
const thumbnail = sharp().resize({ height: 200 }).webp();
await streamPipeline(
response.body,
thumbnail,
createWriteStream(`${folder}/${uuid}.webp`)
);
} catch (err) {
console.log(err);
}
2022-04-05 12:05:12 +02:00
}
}
}
}
2022-04-06 13:13:09 +02:00
// await sharp('src/assets/arbre.png').resize({ width: 440, height: 690 }).webp().toFile('public/arbre.webp')
2022-03-29 23:30:23 +02:00
}
fetchData();