add conversion of osmcal

This commit is contained in:
tykayn 2021-01-05 13:26:42 +01:00 committed by Baptiste Lemoine
parent e5eea3f803
commit 0bb0d27ead
4 changed files with 929 additions and 67 deletions

View File

@ -12,8 +12,8 @@ const parserConfig = {
db_user: "mobilizon", db_user: "mobilizon",
db_pass: "mobilizon", db_pass: "mobilizon",
// other configs // other configs
askAgendaDuLibre: true, // should we fetch the Agenda du libre feed ? askAgendaDuLibre: false, // should we fetch the Agenda du libre feed ?
askOsmCal: false, askOsmCal: true,
runAddQueriesToMobilizon: false, runAddQueriesToMobilizon: false,
debug: false, debug: false,
}; };

107
index.js
View File

@ -1,75 +1,74 @@
// @author tykayn contact@cipherbliss.com www.cipherbliss.com // @author tykayn contact@cipherbliss.com www.cipherbliss.com
// imports // imports
import utils from "./utils.ts"; import utils from './utils.ts';
import parserConfig from "./config.ts"; import parserConfig from './config.ts';
import { htmlEscape } from "escape-goat"; import { htmlEscape } from 'escape-goat';
import Parser from "rss-parser"; import Parser from 'rss-parser';
const fetch = require("node-fetch");
const fetch = require('node-fetch');
let parser = new Parser(); let parser = new Parser();
// @ts-ignore // @ts-ignore
console.log(" ===============================================".blue); console.log(' ==============================================='.blue);
// @ts-ignore // @ts-ignore
console.log(" ==== Rss Feeder for mobilizon - by tykayn ====".blue); console.log(' ==== Rss Feeder for mobilizon - by tykayn ===='.blue);
// @ts-ignore // @ts-ignore
console.log(" ===============================================".blue); console.log(' ==============================================='.blue);
if (parserConfig.debug) { if (parserConfig.debug) {
// @ts-ignore // @ts-ignore
console.log("configMobilizon".blue, config); console.log('configMobilizon'.blue, config);
} }
const utilsTools = new utils(); const utilsTools = new utils();
let localMobilizonEventsByTitle = [];
// trouver si un évènement existe déjà par son titre // trouver si un évènement existe déjà par son titre
const doesThisEventAlreadyExistInLocalEvents = (rssEvent) => {
const eventAlreadyExists =
-1 !== localMobilizonEventsByTitle.indexOf(rssEvent.title);
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log("ajouter l event ", htmlEscape(rssEvent.title));
}
addEventQuery(rssEvent);
}
};
let createEventQueries = ""; let createEventQueries = '';
let counterOfEventsToAdd = 0; let counterOfEventsToAdd = 0;
const addEventQuery = utilsTools.agendadulibre.addQuery; const addEventQuery = utilsTools.agendadulibre.addQuery;
const runCreationQuery = utilsTools.runCreationQuery; const runCreationQuery = utilsTools.runCreationQuery;
(async () => { (async () => {
console.log("⌛ interroger la BDD mobilizon postgresql"); console.log('⌛ interroger la BDD mobilizon postgresql');
utilsTools.setupClientPostgresql() utilsTools.setupClientPostgresql();
await utilsTools.client.connect(); await utilsTools.client.connect();
console.log("✅ OK connecté à postgresql"); console.log('✅ OK connecté à postgresql');
console.log(" "); console.log(' ');
const res = await utilsTools.client.query("SELECT * from events"); const res = await utilsTools.client.query('SELECT * from events');
console.log("💾 évènements enregistrés dans mobilizon : ", res.rows.length); // Hello world! console.log('💾 évènements enregistrés dans mobilizon : ', res.rows.length); // Hello world!
utilsTools.postgresEventsExisting = res.rows; utilsTools.localMobilizonEventsByTitle = res.rows;
if (parserConfig.askAgendaDuLibre) { if (parserConfig.askAgendaDuLibre) {
// @ts-ignore // @ts-ignore
console.log(" ====================================================".blue); console.log(' ===================================================='.blue);
// @ts-ignore // @ts-ignore
console.log(" ==== demander à l'agenda du libre son flux RSS ====".blue); console.log(' ==== demander à l\'agenda du libre son flux RSS ===='.blue);
// @ts-ignore // @ts-ignore
console.log(" ====================================================".blue); console.log(' ===================================================='.blue);
console.log('⌛ lecture du flux rss : ' + parserConfig.rss_feed_url);
fetch(parserConfig.rss_feed_url, { method: 'GET' })
.then((arrayOfEvents) => {
console.log('xml rss_feed_url count', arrayOfEvents.length);
arrayOfEvents.forEach((eventFound) => {
console.log(eventFound.name);
// console.log('item', item)
utilsTools.osmcal.doesEventExists(eventFound);
});
});
console.log("⌛ lecture du flux rss : " + parserConfig.rss_feed_url);
let feed = await parser.parseURL(parserConfig.rss_feed_url); let feed = await parser.parseURL(parserConfig.rss_feed_url);
console.log("✅ flux rss obtenu"); console.log('✅ flux rss obtenu');
utilsTools.writeFile("agenda_du_libre_feed.xml", feed);
console.log(feed.title); console.log(feed.title);
console.log("⚙️ interprétation des évènements"); console.log('⚙️ interprétation des évènements');
console.log( console.log(
"⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql" '⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql',
); );
feed.items.forEach((eventFound) => { feed.items.forEach((eventFound) => {
console.log(eventFound.title); console.log(eventFound.title);
@ -79,18 +78,18 @@ const runCreationQuery = utilsTools.runCreationQuery;
} }
if (parserConfig.askOsmCal) { if (parserConfig.askOsmCal) {
// @ts-ignore // @ts-ignore
console.log(" ====================================================".blue); console.log(' ===================================================='.blue);
// @ts-ignore // @ts-ignore
console.log(" ==== demander à OSM cal ====".blue); console.log(' ==== demander à OSM cal ===='.blue);
// @ts-ignore // @ts-ignore
console.log(" ====================================================".blue); console.log(' ===================================================='.blue);
fetch(parserConfig.osmcal_url, { method: "GET" }) fetch(parserConfig.osmcal_url, { method: 'GET' })
.then((res) => res.json()) .then((res) => res.json())
.then((arrayOfEvents) => { .then((arrayOfEvents) => {
console.log("json osmcal count", arrayOfEvents.length); console.log('json osmcal count', arrayOfEvents.length);
utilsTools.writeFile("osmcal.json", arrayOfEvents); utilsTools.writeFile('osmcal.json', arrayOfEvents, 'json');
arrayOfEvents.forEach((eventFound) => { arrayOfEvents.forEach((eventFound) => {
console.log(eventFound.name); console.log(eventFound.name);
@ -103,23 +102,25 @@ const runCreationQuery = utilsTools.runCreationQuery;
if (parserConfig.runAddQueriesToMobilizon) { if (parserConfig.runAddQueriesToMobilizon) {
// @ts-ignore // @ts-ignore
console.log( console.log(
" rajouter les évènements manquants à la base mobilizon".blue ' rajouter les évènements manquants à la base mobilizon'.blue,
); );
utilsTools.makeQuery(); utilsTools.makeQuery();
utilsTools.runCreationQuery(); utilsTools.runCreationQuery();
// @ts-ignore // @ts-ignore
console.log(" ---------- ".green); console.log(' ---------- '.green);
// @ts-ignore // @ts-ignore
console.log(counterOfEventsToAdd, " évènements ajoutés ".green); console.log(counterOfEventsToAdd, ' évènements ajoutés '.green);
// @ts-ignore // @ts-ignore
console.log(" ---------- ".green); console.log(' ---------- '.green);
// @ts-ignore // @ts-ignore
console.log("✅ ça c'est fait ".green); console.log('✅ fermer la connec postgresql');
await utilsTools.client.end();
console.log('✅ ça c\'est fait '.green);
} }
await utilsTools.client.end();
})(); })();
exports = () => { exports = () => {
console.log("hey ho", this); console.log('✅ hey ho', this);
}; };

File diff suppressed because one or more lines are too long

View File

@ -12,7 +12,7 @@ class utils {
client; client;
makeQuery = () => { makeQuery = () => {
this.createEventQueries = `INSERT INTO events(title, description, organizer_actor_id,inserted_at,updated_at, uuid, url, status, category, options,participants_stats, begins_on, ends_on) VALUES ${this.agendadulibre.queryToAdd} ${this.osmcal.queryToAdd};`; this.createEventQueries = `INSERT INTO events(title, description, organizer_actor_id,inserted_at,updated_at, uuid, url, status, category, options,participants_stats, begins_on, ends_on) VALUES ${this.agendadulibre.queryToAdd} ${this.osmcal.queryToAdd};`;
this.writeFile("event_creation_query.psql", this.createEventQueries); this.writeFile("event_creation_query.psql", this.createEventQueries, "psql");
} }
runCreationQuery = async () => { runCreationQuery = async () => {
if (this.createEventQueries) { if (this.createEventQueries) {
@ -31,6 +31,7 @@ class utils {
}; };
createEventQueries = ""; createEventQueries = "";
counterOfEventsToAdd = 0; counterOfEventsToAdd = 0;
localMobilizonEventsByTitle=[];
convertRssDate(rssDate) { convertRssDate(rssDate) {
@ -49,12 +50,15 @@ class utils {
console.log("converted", converted); console.log("converted", converted);
} }
postgresEventsExisting = []; writeFile = (fileName, data, formatData) => {
writeFile = (fileName, data) => { let dataToSave = data;
if (formatData == 'json') {
dataToSave = JSON.stringify(data, null, 4)
}
// write file to disk // write file to disk
fs.writeFile( fs.writeFile(
`./sources_examples/${fileName}`, `./sources_examples/${fileName}`,
JSON.stringify(data), dataToSave,
"utf8", "utf8",
(err) => { (err) => {
if (err) { if (err) {
@ -72,7 +76,31 @@ class utils {
}, },
addQuery: (event) => { addQuery: (event) => {
this.agendadulibre.counterOfEventsToAdd++; if (this.osmcal.queryToAdd) {
this.osmcal.queryToAdd += ` , `;
}
let title = "'" + htmlEscape(event.name+ ' '+ event.location.short) + "'";
let content = "'" + htmlEscape(event.date.human+ ' <br/>'+ event.location.detailed + ' <br/>' + event.location.venue + ' <br/>' +event.url + ' <br/>' + event.coords[0]+','+event.coords[1]) + "'";
console.log(' ')
console.log(' title', title)
console.log(' content', content)
let uuid = uuidv4();
let uuidString = "'" + uuid + "'";
let eventUrl =
"'" + parserConfig.mobilizon_public_url + "/events/" + uuid + "'";
let begins_on = "'" + this.convertRssDate(event.date.start) + "'";
let ends_on = "'" + this.convertRssDate(event.date.end) + "'";
let baseOptions =
'{"offers": [], "program": null, "attendees": [], "show_end_time": true, "show_start_time": true, "comment_moderation": "allow_all", "anonymous_participation": true, "participation_condition": [], "show_participation_price": false, "maximum_attendee_capacity": 0, "remaining_attendee_capacity": 0, "hide_organizer_when_group_event": false, "show_remaining_attendee_capacity": false}';
let baseStats =
'{"creator": 1, "rejected": 0, "moderator": 0, "participant": 0, "not_approved": 0, "administrator": 0, "not_confirmed": 0}';
// begins_on , ends_on expecting date format like this: "2020-12-17 23:00:00"
this.osmcal.queryToAdd += `( ${title}, ${content}, ${parserConfig.feeder_mobilizon_user_id}, 'now()','now()', ${uuidString}, ${eventUrl}, 'confirmed' , 'meeting', ${baseOptions}, ${baseStats}, ${begins_on} , ${ends_on} )`;
this.osmcal.counterOfEventsToAdd++;
this.counterOfEventsToAdd++; this.counterOfEventsToAdd++;
}, },
@ -80,21 +108,29 @@ class utils {
agendadulibre = { agendadulibre = {
queryToAdd: "", queryToAdd: "",
counterOfEventsToAdd: 0, counterOfEventsToAdd: 0,
doesEventExists: (event) => { doesEventExists: (rssEvent) => {
const eventAlreadyExists =
-1 !== this.localMobilizonEventsByTitle.indexOf(rssEvent.title);
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log('ajouter l event ', htmlEscape(rssEvent.title));
}
this.agendadulibre.addQuery(rssEvent);
}
}, },
addQuery: (rssEvent) => { addQuery: (event) => {
if (this.agendadulibre.queryToAdd) { if (this.agendadulibre.queryToAdd) {
this.agendadulibre.queryToAdd += ` , `; this.agendadulibre.queryToAdd += ` , `;
} }
let title = "'" + htmlEscape(rssEvent.title) + "'"; let title = "'" + htmlEscape(event.title) + "'";
let content = "'" + htmlEscape(rssEvent.content) + "'"; let content = "'" + htmlEscape(event.content) + "'";
let uuid = uuidv4(); let uuid = uuidv4();
let uuidString = "'" + uuid + "'"; let uuidString = "'" + uuid + "'";
let eventUrl = let eventUrl =
"'" + parserConfig.mobilizon_public_url + "/events/" + uuid + "'"; "'" + parserConfig.mobilizon_public_url + "/events/" + uuid + "'";
let begins_on = "'" + this.convertRssDate(rssEvent.date) + "'"; let begins_on = "'" + this.convertRssDate(event.date) + "'";
let ends_on = "'" + this.convertRssDate(rssEvent.date) + "'"; let ends_on = "'" + this.convertRssDate(event.date) + "'";
let baseOptions = let baseOptions =
'{"offers": [], "program": null, "attendees": [], "show_end_time": true, "show_start_time": true, "comment_moderation": "allow_all", "anonymous_participation": true, "participation_condition": [], "show_participation_price": false, "maximum_attendee_capacity": 0, "remaining_attendee_capacity": 0, "hide_organizer_when_group_event": false, "show_remaining_attendee_capacity": false}'; '{"offers": [], "program": null, "attendees": [], "show_end_time": true, "show_start_time": true, "comment_moderation": "allow_all", "anonymous_participation": true, "participation_condition": [], "show_participation_price": false, "maximum_attendee_capacity": 0, "remaining_attendee_capacity": 0, "hide_organizer_when_group_event": false, "show_remaining_attendee_capacity": false}';
let baseStats = let baseStats =