86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
|
|
//import fetch from 'node-fetch'; // For ES Modules
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { parse } = require('json2csv');
|
|
const striptags = require('striptags');
|
|
const dayjs = require('dayjs');
|
|
const he = require('he');
|
|
const jsonTojsonl = require('json-to-jsonl');
|
|
|
|
const fetch = require('node-fetch'); // For CommonJS
|
|
const { start } = require('repl');
|
|
const request = require('request');
|
|
let { render } = require("mustache");
|
|
|
|
let jsonlData = '';
|
|
|
|
const DATA_INPUT_FOLDER = './input';
|
|
const DATA_OUTPUT_FOLDER = './output';
|
|
const TEMPLATES_FOLDER = './templates';
|
|
const DIRECTUS_BUCKET = 'https://ewr1.vultrobjects.com/lgcc-conferences/'
|
|
const LOCALE = 'pt'
|
|
|
|
function isUndefined(variable){
|
|
if( typeof(variable) === 'undefined'){
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
async function generateFiles( type ) {
|
|
let options = { json: true };
|
|
|
|
let url = `http://conferences.local:10003/v3/conferencias/?f=id_list&cache=true&locale=${LOCALE}`;
|
|
|
|
request(url, options, (error, res, body) => {
|
|
if (error) {
|
|
return console.log(error)
|
|
};
|
|
|
|
if (!error && res.statusCode == 200) {
|
|
// do something with JSON, using the 'body' variable
|
|
const items = body.result.conferencias;
|
|
|
|
let nitems = items.map((item) => {
|
|
let nitem = {}
|
|
|
|
nitem.locale = LOCALE
|
|
nitem.code = `${LOCALE}-${dayjs(item.date).format('YYYYMMDD')}-${item.activity}`
|
|
nitem.id = item.wpid
|
|
nitem.title = item.title
|
|
nitem.date = item.date;
|
|
nitem.activity = item.activity;
|
|
|
|
nitem.thumbnail = !isUndefined(item.thumbnail) ? DIRECTUS_BUCKET + item.thumbnail : ''
|
|
|
|
nitem.video = !isUndefined(item.video) ? item.video : ''
|
|
nitem.audio = !isUndefined(item.audio) ? item.audio : ''
|
|
nitem.booklet = !isUndefined(item.pdf) ? item.pdf : ''
|
|
nitem.simple = !isUndefined(item.pdf_simple) ? item.pdf_simple : ''
|
|
|
|
return nitem;
|
|
})
|
|
|
|
if( nitems.length > 0 ){
|
|
let csv = parse(nitems);
|
|
jsonlData += csv
|
|
writeFile(jsonlData, type)
|
|
}
|
|
};
|
|
});
|
|
|
|
}
|
|
|
|
function writeFile(jsonlData, type) {
|
|
fs.writeFile(`./${DATA_INPUT_FOLDER}/conferences_${LOCALE}_files.csv`, jsonlData, (err) => {
|
|
if (err) {
|
|
console.error("Error writing file:", err);
|
|
return;
|
|
}
|
|
console.log("File has been written successfully! : " + type);
|
|
});
|
|
}
|
|
|
|
generateFiles( 'conferences' ); |