rss-feeder-mobilizon/index.js

172 lines
6.0 KiB
JavaScript
Raw Normal View History

2021-01-03 20:32:27 +01:00
// @author tykayn contact@cipherbliss.com www.cipherbliss.com
// imports
2021-01-05 11:17:54 +01:00
import utils from "./utils.ts";
import parserConfig from "./config.ts";
import { v4 as uuidv4 } from "uuid";
import { htmlEscape } from "escape-goat";
import Parser from "rss-parser";
import { Client } from "pg";
const fetch = require("node-fetch");
2021-01-03 20:32:27 +01:00
let parser = new Parser();
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ===============================================".blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ==== Rss Feeder for mobilizon - by tykayn ====".blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ===============================================".blue);
2021-01-03 20:32:27 +01:00
if (parserConfig.debug) {
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log("configMobilizon".blue, config);
2021-01-03 20:32:27 +01:00
}
const utilsTools = new utils();
2021-01-03 20:32:27 +01:00
let localMobilizonEventsByTitle = [];
// 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) {
2021-01-05 11:17:54 +01:00
console.log("ajouter l event ", htmlEscape(rssEvent.title));
2021-01-03 20:32:27 +01:00
}
addEventQuery(rssEvent);
}
};
2021-01-05 11:17:54 +01:00
let createEventQueries = "";
2021-01-03 20:32:27 +01:00
let counterOfEventsToAdd = 0;
const addEventQuery = (rssEvent) => {
if (!createEventQueries) {
createEventQueries =
2021-01-05 11:17:54 +01:00
"INSERT INTO events(title, description, organizer_actor_id,inserted_at,updated_at, uuid, url, status, category, options,participants_stats, begins_on, ends_on) VALUES ";
2021-01-03 20:32:27 +01:00
}
if (counterOfEventsToAdd) {
createEventQueries += ` , `;
}
2021-01-05 11:17:54 +01:00
let title = "'" + htmlEscape(rssEvent.title) + "'";
let content = "'" + htmlEscape(rssEvent.content) + "'";
2021-01-03 20:32:27 +01:00
let uuid = uuidv4();
2021-01-05 11:17:54 +01:00
let uuidString = "'" + uuid + "'";
let eventUrl =
"'" + parserConfig.mobilizon_public_url + "/events/" + uuid + "'";
let begins_on = "'" + utilsTools.convertRssDate(rssEvent.date) + "'";
let ends_on = "'" + utilsTools.convertRssDate(rssEvent.date) + "'";
2021-01-03 20:32:27 +01:00
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"
2021-01-05 11:17:54 +01:00
createEventQueries += `( ${title}, ${content}, ${parserConfig.feeder_mobilizon_user_id}, 'now()','now()', ${uuidString}, ${eventUrl}, 'confirmed' , 'meeting', ${baseOptions}, ${baseStats}, ${begins_on} , ${ends_on} )`;
2021-01-03 20:32:27 +01:00
counterOfEventsToAdd++;
};
const runCreationQuery = async () => {
2021-01-05 11:17:54 +01:00
createEventQueries = createEventQueries + ";";
utilsTools.writeFile("event_creation_query.psql", createEventQueries);
2021-01-03 20:32:27 +01:00
if (createEventQueries) {
2021-01-05 11:17:54 +01:00
console.log(" ");
console.log(" ⚙️⚙️⚙️ ");
console.log(" ");
console.log(" createEventQueries");
2021-01-03 20:32:27 +01:00
console.log(createEventQueries);
const res = await client.query(createEventQueries);
2021-01-05 11:17:54 +01:00
console.log("res", res);
2021-01-03 20:32:27 +01:00
return res;
}
};
const client = new Client({
2021-01-05 11:17:54 +01:00
host: "localhost",
user: parserConfig.db_user,
2021-01-03 20:32:27 +01:00
password: parserConfig.db_pass,
database: parserConfig.db_name,
});
(async () => {
2021-01-05 11:17:54 +01:00
console.log("⌛ interroger la BDD mobilizon postgresql");
2021-01-03 20:32:27 +01:00
await client.connect();
2021-01-05 11:17:54 +01:00
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!
2021-01-03 20:32:27 +01:00
2021-01-05 11:17:54 +01:00
utilsTools.postgresEventsExisting = res.rows;
2021-01-03 20:32:27 +01:00
if (parserConfig.askAgendaDuLibre) {
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ====================================================".blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ==== demander à l'agenda du libre son flux RSS ====".blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ====================================================".blue);
2021-01-03 20:32:27 +01:00
2021-01-05 11:17:54 +01:00
console.log("⌛ lecture du flux rss : " + parserConfig.rss_feed_url);
2021-01-03 20:32:27 +01:00
let feed = await parser.parseURL(parserConfig.rss_feed_url);
2021-01-05 11:17:54 +01:00
console.log("✅ flux rss obtenu");
utilsTools.writeFile("agenda_du_libre_feed.xml", feed);
2021-01-03 20:32:27 +01:00
console.log(feed.title);
2021-01-05 11:17:54 +01:00
console.log("⚙️ interprétation des évènements");
2021-01-03 20:32:27 +01:00
console.log(
2021-01-05 11:17:54 +01:00
"⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql"
2021-01-03 20:32:27 +01:00
);
2021-01-05 11:17:54 +01:00
feed.items.forEach((eventFound) => {
console.log(eventFound.title);
2021-01-03 20:32:27 +01:00
// console.log('item', item)
2021-01-05 11:17:54 +01:00
utilsTools.agendadulibre.doesEventExists(eventFound);
2021-01-03 20:32:27 +01:00
});
}
if (parserConfig.askOsmCal) {
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ====================================================".blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ==== demander à OSM cal ====".blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ====================================================".blue);
fetch(parserConfig.osmcal_url, { method: "GET" })
.then((res) => res.json())
.then((arrayOfEvents) => {
console.log("json osmcal count", arrayOfEvents.length);
utilsTools.writeFile("osmcal.json", arrayOfEvents);
arrayOfEvents.forEach((eventFound) => {
console.log(eventFound.name);
// console.log('item', item)
utilsTools.osmCal.doesEventExists(eventFound);
});
});
2021-01-03 20:32:27 +01:00
}
if (parserConfig.runAddQueriesToMobilizon) {
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(
" rajouter les évènements manquants à la base mobilizon".blue
);
2021-01-03 20:32:27 +01:00
await runCreationQuery();
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ---------- ".green);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(counterOfEventsToAdd, " évènements ajoutés ".green);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log(" ---------- ".green);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 11:17:54 +01:00
console.log("✅ ça c'est fait ".green);
2021-01-03 20:32:27 +01:00
}
await client.end();
})();
exports = () => {
2021-01-05 11:17:54 +01:00
console.log("hey ho", this);
2021-01-03 20:32:27 +01:00
};