From 2ab3ecd9ef6b099d4ffcc5f75f2c15e585ee5ee9 Mon Sep 17 00:00:00 2001 From: Simon C Date: Tue, 5 Apr 2022 12:05:12 +0200 Subject: [PATCH] feat: Import des photos --- .gitignore | 1 + scripts/fetchData.js | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 97e0795..29535ac 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ coverage # app src/data.json +public/answers diff --git a/scripts/fetchData.js b/scripts/fetchData.js index c2fcec1..977e631 100644 --- a/scripts/fetchData.js +++ b/scripts/fetchData.js @@ -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();