feat: Import des photos
This commit is contained in:
parent
fab13bd9d0
commit
2ab3ecd9ef
|
@ -29,3 +29,4 @@ coverage
|
|||
|
||||
# app
|
||||
src/data.json
|
||||
public/answers
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue