2021-01-03 14:57:29 +01:00
|
|
|
|
// @author tykayn contact@cipherbliss.com www.cipherbliss.com
|
|
|
|
|
// imports
|
|
|
|
|
var fs = require("fs");
|
|
|
|
|
let Parser = require("rss-parser");
|
|
|
|
|
let parser = new Parser();
|
|
|
|
|
const { Client } = require("pg");
|
2021-01-03 16:39:27 +01:00
|
|
|
|
var colors = require("colors");
|
|
|
|
|
const { v4: uuidv4 } = require("uuid");
|
2021-01-03 14:57:29 +01:00
|
|
|
|
|
|
|
|
|
// configuration of the parser
|
|
|
|
|
const config = {
|
|
|
|
|
rss_feed_url: "https://www.agendadulibre.org/events.rss?tag=openstreetmap",
|
|
|
|
|
feeder_mobilizon_user_id: "3", // organizer_actor_id pour rattacher les nouveaux évènements à un utilisateur. 3 est le premier admin créé.
|
|
|
|
|
mobilizon_public_url: "https://mobilizon.openstreetmap.fr", // url publique de l'instance mobilizon pour créer les url d'évènements
|
|
|
|
|
db_name: "mobilizon_dev",
|
|
|
|
|
db_user: "mobilizon",
|
|
|
|
|
db_pass: "mobilizon",
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-03 16:39:27 +01:00
|
|
|
|
console.log(" ===============================================".blue);
|
|
|
|
|
console.log(" ==== Rss Feeder for mobilizon - by tykayn ====".blue);
|
|
|
|
|
console.log(" ===============================================".blue);
|
|
|
|
|
console.log("configMobilizon".blue, config);
|
2021-01-03 14:57:29 +01:00
|
|
|
|
|
2021-01-03 15:42:10 +01:00
|
|
|
|
let localMobilizonEventsByTitle = [];
|
2021-01-03 14:57:29 +01:00
|
|
|
|
|
2021-01-03 15:42:10 +01:00
|
|
|
|
// trouver si un évènement existe déjà par son titre
|
|
|
|
|
// TODO à affiner au delà du titre
|
2021-01-03 14:57:29 +01:00
|
|
|
|
const doesThisEventAlreadyExistInLocalEvents = (rssEvent) => {
|
2021-01-03 15:42:10 +01:00
|
|
|
|
const eventAlreadyExists =
|
|
|
|
|
-1 !== localMobilizonEventsByTitle.indexOf(rssEvent.title);
|
|
|
|
|
if (!eventAlreadyExists) {
|
2021-01-03 16:39:27 +01:00
|
|
|
|
console.log("ajouter l event ", htmlEscape(rssEvent.title));
|
|
|
|
|
// console.log(" ", escape(rssEvent.content ));
|
2021-01-03 15:42:10 +01:00
|
|
|
|
addEventQuery(rssEvent);
|
|
|
|
|
}
|
2021-01-03 14:57:29 +01:00
|
|
|
|
};
|
|
|
|
|
|
2021-01-03 15:42:10 +01:00
|
|
|
|
let createEventQueries = "";
|
|
|
|
|
let counterOfEventsToAdd = 0;
|
2021-01-03 16:39:27 +01:00
|
|
|
|
const { htmlEscape } = require("escape-goat");
|
|
|
|
|
|
2021-01-03 14:57:29 +01:00
|
|
|
|
const addEventQuery = (rssEvent) => {
|
2021-01-03 15:42:10 +01:00
|
|
|
|
if (!createEventQueries) {
|
|
|
|
|
createEventQueries =
|
2021-01-03 16:39:27 +01:00
|
|
|
|
"INSERT INTO events(title, description, organizer_actor_id,inserted_at,updated_at, uuid, url, status, category, options,participants_stats) VALUES ";
|
2021-01-03 15:42:10 +01:00
|
|
|
|
}
|
|
|
|
|
if (counterOfEventsToAdd) {
|
|
|
|
|
createEventQueries += ` , `;
|
|
|
|
|
}
|
2021-01-03 16:39:27 +01:00
|
|
|
|
|
|
|
|
|
let title = "'" + htmlEscape(rssEvent.title) + "'";
|
|
|
|
|
let content = "'" + htmlEscape(rssEvent.content) + "'";
|
|
|
|
|
let uuid = uuidv4();
|
|
|
|
|
let uuidString = "'" + uuid + "'";
|
|
|
|
|
let eventUrl = "'" + config.mobilizon_public_url + "/events/" + uuid + "'";
|
|
|
|
|
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}';
|
|
|
|
|
// TODO complete with date conversion
|
|
|
|
|
// begins_on , ends_on expecting date format like this: "2020-12-17 23:00:00"
|
|
|
|
|
|
|
|
|
|
createEventQueries += `( ${title}, ${content}, ${config.feeder_mobilizon_user_id}, 'now()','now()', ${uuidString}, ${eventUrl}, 'confirmed' , 'meeting', ${baseOptions}, ${baseStats} )`;
|
2021-01-03 15:42:10 +01:00
|
|
|
|
|
|
|
|
|
counterOfEventsToAdd++;
|
2021-01-03 14:57:29 +01:00
|
|
|
|
};
|
|
|
|
|
const runCreationQuery = async () => {
|
2021-01-03 16:39:27 +01:00
|
|
|
|
createEventQueries = createEventQueries + ";";
|
2021-01-03 15:42:10 +01:00
|
|
|
|
|
2021-01-03 16:39:27 +01:00
|
|
|
|
if (createEventQueries) {
|
|
|
|
|
console.log(" ");
|
|
|
|
|
console.log(" ⚙️⚙️⚙️ ");
|
|
|
|
|
console.log(" ");
|
|
|
|
|
console.log(" createEventQueries");
|
|
|
|
|
console.log(createEventQueries);
|
|
|
|
|
|
|
|
|
|
const res = await client.query(createEventQueries);
|
|
|
|
|
console.log("res", res);
|
|
|
|
|
return res;
|
2021-01-03 14:57:29 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const client = new Client({
|
|
|
|
|
host: "localhost",
|
|
|
|
|
user: config.db_user,
|
|
|
|
|
password: config.db_pass,
|
|
|
|
|
database: config.db_name,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
(async () => {
|
2021-01-03 15:42:10 +01:00
|
|
|
|
console.log("⌛ interroger la BDD mobilizon postgresql");
|
|
|
|
|
|
|
|
|
|
await client.connect();
|
|
|
|
|
console.log("✅ OK connecté à postgresql");
|
|
|
|
|
console.log(" ");
|
|
|
|
|
const res = await client.query("SELECT * from events");
|
|
|
|
|
console.log("💾 évènements enregistrés dans mobilizon : ", res.rows.length); // Hello world!
|
|
|
|
|
|
|
|
|
|
res.rows.forEach((item) => {
|
|
|
|
|
localMobilizonEventsByTitle.push(item.title);
|
|
|
|
|
|
|
|
|
|
console.log(" 🟢 ", item.begins_on, item.ends_on, item.title, item.url);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log("⌛ lecture du flux rss : " + config.rss_feed_url);
|
2021-01-03 14:57:29 +01:00
|
|
|
|
let feed = await parser.parseURL(config.rss_feed_url);
|
2021-01-03 15:42:10 +01:00
|
|
|
|
console.log("✅ flux rss obtenu");
|
2021-01-03 14:57:29 +01:00
|
|
|
|
console.log(feed.title);
|
|
|
|
|
|
2021-01-03 15:42:10 +01:00
|
|
|
|
console.log("⚙️ interprétation des évènements");
|
|
|
|
|
console.log(
|
|
|
|
|
"⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql"
|
|
|
|
|
);
|
2021-01-03 14:57:29 +01:00
|
|
|
|
feed.items.forEach((rssEvent) => {
|
|
|
|
|
console.log(rssEvent.title);
|
|
|
|
|
// console.log('item', item)
|
|
|
|
|
doesThisEventAlreadyExistInLocalEvents(rssEvent);
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-03 16:39:27 +01:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
console.log("➕ rajouter les évènements manquants à la base mobilizon".blue);
|
|
|
|
|
|
|
|
|
|
await runCreationQuery();
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
console.log(" ---------- ".green);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
console.log(counterOfEventsToAdd, " évènements ajoutés ".green);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
console.log(" ---------- ".green);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
console.log("✅ ça c'est fait ".green);
|
2021-01-03 14:57:29 +01:00
|
|
|
|
|
|
|
|
|
await client.end();
|
|
|
|
|
})();
|