65 lines
2.4 KiB
TypeScript
65 lines
2.4 KiB
TypeScript
|
import * as fs from "fs";
|
||
|
import utils from "../utils";
|
||
|
|
||
|
let utilsTools = new utils();
|
||
|
|
||
|
console.log('importation depuis le fichier local de l\' agenda du libre');
|
||
|
let filepath = './output/adl_json.json'
|
||
|
let filecontent;
|
||
|
let counterOfEventsToAdd = 0;
|
||
|
const addEventQuery = utilsTools.agendadulibre.addQuery;
|
||
|
const runCreationQuery = utilsTools.runCreationQuery;
|
||
|
|
||
|
async function runImportEvents() {
|
||
|
|
||
|
|
||
|
console.log('File exists');
|
||
|
// build list of existing events in mobilizon database
|
||
|
|
||
|
await utilsTools.setupClientPostgresql();
|
||
|
await utilsTools.client.connect();
|
||
|
console.log('✅ OK connecté à postgresql');
|
||
|
console.log(' ');
|
||
|
const res = await utilsTools.client.query('SELECT * from events');
|
||
|
console.log('💾 évènements enregistrés dans mobilizon : ', res.rows.length);
|
||
|
res.rows.forEach((bdd_event: any) => {
|
||
|
console.log('eventObject', bdd_event);
|
||
|
utilsTools.localMobilizonEventsByTitle.push(utilsTools.agendadulibre.uniqTitleBDD(bdd_event));
|
||
|
})
|
||
|
|
||
|
console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle);
|
||
|
// get json file for ADL
|
||
|
fs.readFile(filepath, 'utf8', function (err, data) {
|
||
|
if (err) {
|
||
|
return console.log(err);
|
||
|
}
|
||
|
filecontent = JSON.parse(data)
|
||
|
// console.log('events in the scrapped json', filecontent.length);
|
||
|
let ii=0;
|
||
|
filecontent.forEach((event:any)=>{
|
||
|
ii++
|
||
|
let date = new Date(event.start_time);
|
||
|
|
||
|
// console.log(ii, date.toLocaleDateString('fr-fr', { weekday:"long", year:"numeric", month:"short", day:"numeric"}) , event.title );
|
||
|
utilsTools.agendadulibre.doesEventExistsFromJsonScrap(event);
|
||
|
utilsTools.agendadulibre.addQueryFromJsonScrap(event);
|
||
|
})
|
||
|
// compare events
|
||
|
// import only new events
|
||
|
|
||
|
});
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
fs.stat(filepath, function (err, stat) {
|
||
|
if (err == null) {
|
||
|
runImportEvents();
|
||
|
} else if (err.code === 'ENOENT') {
|
||
|
// file does not exist
|
||
|
console.log('Scrapped json file does not exist. Run a scraper like "ts-node scrapers/adl.ts" before using this importer: ', err.code);
|
||
|
} else {
|
||
|
console.log('Some other error: ', err.code);
|
||
|
}
|
||
|
});
|