rss-feeder-mobilizon/importers/adl.ts

151 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-01-13 11:18:44 +01:00
import * as fs from "fs";
import utils from "../utils";
2022-01-13 12:02:51 +01:00
import parserConfig from "../config";
import fetch from 'node-fetch';
2022-01-13 11:18:44 +01:00
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() {
2022-01-13 12:02:51 +01:00
console.log('File exists');
// build list of existing events in mobilizon database
2022-01-13 11:18:44 +01:00
2022-01-13 12:02:51 +01:00
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) => {
2022-01-13 14:30:45 +01:00
if (utilsTools.localMobilizonEventsByTitle.indexOf(utilsTools.agendadulibre.uniqTitleBDD(bdd_event)) === -1) {
utilsTools.localMobilizonEventsByTitle.push(utilsTools.agendadulibre.uniqTitleBDD(bdd_event));
}
2022-01-13 12:02:51 +01:00
})
2022-01-13 11:18:44 +01:00
2022-01-13 14:30:45 +01:00
console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle.length);
2022-01-13 12:02:51 +01:00
// get json file for ADL
await fs.readFile(filepath, 'utf8', function (err, data) {
if (err) {
2022-01-13 15:27:44 +01:00
return console.log('readFile ERROR', err);
2022-01-13 12:02:51 +01:00
}
filecontent = JSON.parse(data)
2022-01-13 14:52:27 +01:00
filecontent = filecontent.slice(0, parserConfig.max_new_events_in_scrapping)
2022-01-13 14:30:45 +01:00
console.log('events in the scrapped json', filecontent.length);
2022-01-13 12:02:51 +01:00
let ii = 0;
filecontent.forEach((event: any) => {
ii++
// compare events with existing events
2022-01-13 14:30:45 +01:00
let eventAlreadyExists = utilsTools.agendadulibre.doesEventExistsFromJsonScrap(event);
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log('ajouter l event ', utilsTools.agendadulibre.uniqTitle(event));
}
let newQuery = utilsTools.agendadulibre.addQueryFromJsonScrap(event);
utilsTools.newEvents.push({
event,
newQuery
});
}
2022-01-13 12:02:51 +01:00
})
2022-01-13 14:30:45 +01:00
// console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle);
2022-01-13 12:02:51 +01:00
console.log('end looking for events');
// import only new events
2022-01-13 14:30:45 +01:00
console.log('nouveaux évènements à ajouter: ', utilsTools.newEvents.length);
2022-01-13 12:02:51 +01:00
let limiter = parserConfig.limit_persistence_of_new_events;
let counter = 0;
2022-01-13 15:34:01 +01:00
let counter_max = Math.max(parserConfig.max_new_events,utilsTools.newEvents.length);
2022-01-13 12:02:51 +01:00
2022-01-13 14:30:45 +01:00
utilsTools.newEvents.forEach((pair: any) => {
let graphQLquery = pair.newQuery;
if (limiter && counter < parserConfig.max_new_events) {
2022-01-13 12:02:51 +01:00
counter++;
2022-01-13 14:30:45 +01:00
console.log(counter, ' * ', utilsTools.agendadulibre.uniqTitle(pair.event));
2022-01-13 12:02:51 +01:00
const body = {
operationName: "createEvent",
query: graphQLquery.query,
variables: graphQLquery.variables
2022-01-13 11:18:44 +01:00
}
2022-01-13 12:02:51 +01:00
if (parserConfig.enableFetch) {
let url: string = parserConfig.dev_mode ? parserConfig.dev_url : parserConfig.mobilizon_public_url;
let options: any = {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"authorization": "Bearer " + parserConfig.bearer_token,
}
}
2022-01-13 14:52:27 +01:00
if (!utilsTools.agendadulibre.doesEventExistsFromJsonScrap(pair.event)) {
2022-01-13 14:30:45 +01:00
console.log('ajouter');
2022-01-13 14:52:27 +01:00
// add a little delay between creations
2022-01-13 15:27:44 +01:00
fetchEvent(url, options, counter, counter_max, pair.event)
2022-01-13 14:52:27 +01:00
} else {
2022-01-13 14:30:45 +01:00
console.log('nope');
}
2022-01-13 12:02:51 +01:00
} else {
console.log('---- le fetch est désactivé');
}
2022-01-13 11:18:44 +01:00
2022-01-13 12:02:51 +01:00
}
})
// end running queries
});
2022-01-13 11:18:44 +01:00
2022-01-13 12:02:51 +01:00
}
2022-01-13 15:27:44 +01:00
let timeout;
const fetchEvent = (theUrl, theOptions, counter, counter_max, event) => {
let BearerIsOK = true;
timeout = setTimeout(
2022-01-13 14:52:27 +01:00
function () {
2022-01-13 15:27:44 +01:00
console.log(counter,'/', counter_max,' fetch start - ' + event.title + ' ' + event.start_time);
if(BearerIsOK){
2022-01-13 12:02:51 +01:00
2022-01-13 14:52:27 +01:00
fetch(theUrl, theOptions)
.then((res: any) => {
let status = res.status;
2022-01-13 15:27:44 +01:00
console.log(' status', status);
2022-01-13 14:52:27 +01:00
if (status === 401) {
console.error(' /!\\ ------------------ ERROR: Bearer token invalid ------------------')
2022-01-13 15:27:44 +01:00
2022-01-13 14:52:27 +01:00
clearTimeout(timeout);
} else if (status === 200) {
2022-01-13 15:27:44 +01:00
console.log(' succès - ' + event.title + ' ' + event.start_time);
2022-01-13 14:52:27 +01:00
}
2022-01-13 15:27:44 +01:00
// console.log(' res', res);
return res.json()
2022-01-13 14:52:27 +01:00
})
2022-01-13 15:27:44 +01:00
.then((json: any) => console.log(counter,'/', counter_max, 'DONE'))
.catch((err: any) => {
console.log(err)
BearerIsOK = false;
})
}
}.bind(this)
2022-01-13 14:52:27 +01:00
,
1000 * counter
)
2022-01-13 12:02:51 +01:00
2022-01-13 11:18:44 +01:00
}
2022-01-13 12:02:51 +01:00
runImportEvents();