172 lines
6.0 KiB
JavaScript
172 lines
6.0 KiB
JavaScript
// @author tykayn contact@cipherbliss.com www.cipherbliss.com
|
||
// imports
|
||
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");
|
||
let parser = new Parser();
|
||
|
||
// @ts-ignore
|
||
console.log(" ===============================================".blue);
|
||
// @ts-ignore
|
||
console.log(" ==== Rss Feeder for mobilizon - by tykayn ====".blue);
|
||
// @ts-ignore
|
||
console.log(" ===============================================".blue);
|
||
if (parserConfig.debug) {
|
||
// @ts-ignore
|
||
console.log("configMobilizon".blue, config);
|
||
}
|
||
const utilsTools = new utils();
|
||
|
||
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) {
|
||
console.log("ajouter l event ", htmlEscape(rssEvent.title));
|
||
}
|
||
addEventQuery(rssEvent);
|
||
}
|
||
};
|
||
|
||
let createEventQueries = "";
|
||
let counterOfEventsToAdd = 0;
|
||
const addEventQuery = (rssEvent) => {
|
||
if (!createEventQueries) {
|
||
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 ";
|
||
}
|
||
if (counterOfEventsToAdd) {
|
||
createEventQueries += ` , `;
|
||
}
|
||
|
||
let title = "'" + htmlEscape(rssEvent.title) + "'";
|
||
let content = "'" + htmlEscape(rssEvent.content) + "'";
|
||
let uuid = uuidv4();
|
||
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) + "'";
|
||
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}, ${parserConfig.feeder_mobilizon_user_id}, 'now()','now()', ${uuidString}, ${eventUrl}, 'confirmed' , 'meeting', ${baseOptions}, ${baseStats}, ${begins_on} , ${ends_on} )`;
|
||
|
||
counterOfEventsToAdd++;
|
||
};
|
||
const runCreationQuery = async () => {
|
||
createEventQueries = createEventQueries + ";";
|
||
utilsTools.writeFile("event_creation_query.psql", createEventQueries);
|
||
|
||
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;
|
||
}
|
||
};
|
||
|
||
const client = new Client({
|
||
host: "localhost",
|
||
user: parserConfig.db_user,
|
||
password: parserConfig.db_pass,
|
||
database: parserConfig.db_name,
|
||
});
|
||
|
||
(async () => {
|
||
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!
|
||
|
||
utilsTools.postgresEventsExisting = res.rows;
|
||
|
||
if (parserConfig.askAgendaDuLibre) {
|
||
// @ts-ignore
|
||
console.log(" ====================================================".blue);
|
||
// @ts-ignore
|
||
console.log(" ==== demander à l'agenda du libre son flux RSS ====".blue);
|
||
// @ts-ignore
|
||
console.log(" ====================================================".blue);
|
||
|
||
console.log("⌛ lecture du flux rss : " + parserConfig.rss_feed_url);
|
||
let feed = await parser.parseURL(parserConfig.rss_feed_url);
|
||
console.log("✅ flux rss obtenu");
|
||
utilsTools.writeFile("agenda_du_libre_feed.xml", feed);
|
||
console.log(feed.title);
|
||
|
||
console.log("⚙️ interprétation des évènements");
|
||
console.log(
|
||
"⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql"
|
||
);
|
||
feed.items.forEach((eventFound) => {
|
||
console.log(eventFound.title);
|
||
// console.log('item', item)
|
||
utilsTools.agendadulibre.doesEventExists(eventFound);
|
||
});
|
||
}
|
||
if (parserConfig.askOsmCal) {
|
||
// @ts-ignore
|
||
console.log(" ====================================================".blue);
|
||
// @ts-ignore
|
||
console.log(" ==== demander à OSM cal ====".blue);
|
||
// @ts-ignore
|
||
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);
|
||
});
|
||
});
|
||
}
|
||
|
||
if (parserConfig.runAddQueriesToMobilizon) {
|
||
// @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);
|
||
}
|
||
await client.end();
|
||
})();
|
||
|
||
exports = () => {
|
||
console.log("hey ho", this);
|
||
};
|