feat: Import des photos

This commit is contained in:
Simon 2022-04-05 12:05:12 +02:00
parent fab13bd9d0
commit 2ab3ecd9ef
2 changed files with 26 additions and 3 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ coverage
# app
src/data.json
public/answers

View File

@ -1,5 +1,8 @@
import {createWriteStream, existsSync, mkdirSync} from 'fs';
import fs from "fs/promises";
import {pipeline} from 'stream';
import {promisify} from 'util'
const streamPipeline = promisify(pipeline);
const apiUrl = "https://admin.ceiba-conseil.com";
async function fetchJSONApi(path) {
@ -21,6 +24,11 @@ async function fetchJSONApi(path) {
return response.json();
}
async function fetchAsset(uuid) {
const url = `${apiUrl}/assets/${uuid}`;
return fetch(url)
}
async function fetchData() {
const fields = [
"*",
@ -38,8 +46,22 @@ async function fetchData() {
const url = `/items/scores?${fields
.map((item) => `fields[]=${item}`)
.join("&")}`;
const data = await fetchJSONApi(url);
await fs.writeFile("./src/data.json", JSON.stringify(data.data), "utf8");
const data = (await fetchJSONApi(url)).data;
await fs.writeFile("./src/data.json", JSON.stringify(data), "utf8");
const folder = 'public/answers'
if (!existsSync(folder)) mkdirSync(folder);
for (const score of data) {
for (const question of score.questions) {
for (const answer of question.questions_id.answers) {
const uuid = answer.answers_id.image
if (uuid) {
const response = await fetchAsset(uuid)
await streamPipeline(response.body, createWriteStream(`${folder}/${uuid}.png`));
}
}
}
}
}
fetchData();