rss-feeder-mobilizon/utils.ts

225 lines
8.6 KiB
TypeScript
Raw Normal View History

2021-01-05 11:48:41 +01:00
// @ts-ignore
import parserConfig from "./config.ts";
import {v4 as uuidv4} from "uuid";
import {Client} from "pg";
import {htmlEscape} from "escape-goat";
2021-01-03 20:32:27 +01:00
const moment = require("moment");
2021-01-05 11:17:54 +01:00
const fs = require("fs");
class utils {
/**
* postgres functions
*/
2022-01-10 10:29:30 +01:00
client:any;
2021-01-05 11:48:41 +01:00
makeQuery = () => {
2022-01-10 10:29:30 +01:00
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};`;
2021-01-05 13:26:42 +01:00
this.writeFile("event_creation_query.psql", this.createEventQueries, "psql");
2021-01-05 11:48:41 +01:00
}
runCreationQuery = async () => {
if (this.createEventQueries) {
console.log(" ");
console.log(" ⚙️⚙️⚙️ ");
console.log(" ");
console.log(" createEventQueries");
console.log(this.createEventQueries);
const res = await this.client.query(this.createEventQueries);
console.log("res", res);
return res;
2021-01-05 11:17:54 +01:00
} else {
2021-01-05 11:48:41 +01:00
console.log(" DISABLED createEventQueries");
}
};
/**
* memorizing properties
*/
2021-01-05 11:48:41 +01:00
createEventQueries = "";
counterOfEventsToAdd = 0;
2022-01-10 10:29:30 +01:00
localMobilizonEventsByTitle : Array<string> = [];
2021-01-05 11:48:41 +01:00
/**
* converters
*/
2021-01-05 11:48:41 +01:00
2022-01-10 10:29:30 +01:00
geocoderNominatim(coords: any) {
// https://nominatim.openstreetmap.org/reverse?lat=<value>&lon=<value>&<params>
2022-01-10 10:29:30 +01:00
console.log('https://nominatim.openstreetmap.org/reverse?lat=' + coords[0] + '&lon=' + coords[0]) + '&format=json'
}
2022-01-10 10:29:30 +01:00
convertRssDate(rssDate: any) {
2021-01-05 11:48:41 +01:00
let converted = moment(rssDate)
.format("YYYY-MM-DD LTS")
.slice(0, -3)
.concat(".000000"); // in js format like 2021-03-12T19:00:00Z
console.log("converted", converted);
// like 2021-01-03 15:31:02.918940
return converted;
}
2022-01-10 10:29:30 +01:00
convertCoordinateLinkOsmCal(coords: any) {
this.geocoderNominatim(coords);
return ` <div class='coordinates'>
<a href='https://www.openstreetmap.org/directions?from=&to=${coords[0]}%2C${coords[1]}'>voir le lieu sur une carte
</a>
</div> `
}
2021-01-05 11:48:41 +01:00
testdateconvert() {
let converted = this.convertRssDate("2021-03-12T19:00:00Z");
console.log("converted", converted);
}
/**
* file management
*/
2022-01-10 10:29:30 +01:00
writeFile = (fileName: string, data: any, formatData: any) => {
2021-01-05 13:26:42 +01:00
let dataToSave = data;
if (formatData == 'json') {
dataToSave = JSON.stringify(data, null, 4)
}
2021-01-05 11:48:41 +01:00
// write file to disk
fs.writeFile(
`./sources_examples/${fileName}`,
2021-01-05 13:26:42 +01:00
dataToSave,
2021-01-05 11:48:41 +01:00
"utf8",
2022-01-10 10:29:30 +01:00
(err:any) => {
2021-01-05 11:48:41 +01:00
if (err) {
console.log(`Error writing file: ${err}`);
} else {
console.log(`File ${fileName} is written successfully!`);
}
}
);
};
/** ==============================
* importation sources
*/
2021-01-05 11:48:41 +01:00
osmcal = {
queryToAdd: "",
counterOfEventsToAdd: 0,
2022-01-10 10:29:30 +01:00
getTitle: (event: any) => {
return event.name;
//+ ' '+ event.location.short
},
2022-01-10 10:29:30 +01:00
doesEventExists: (event: any) => {
const eventAlreadyExists =
-1 !== this.localMobilizonEventsByTitle.indexOf(this.osmcal.getTitle(event));
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log('ajouter l event ', htmlEscape(this.osmcal.getTitle(event)));
}
this.osmcal.addQuery(event);
}
2021-01-05 11:48:41 +01:00
},
2022-01-10 10:29:30 +01:00
addQuery: (event: any) => {
2021-01-05 11:48:41 +01:00
2021-01-05 13:26:42 +01:00
if (this.osmcal.queryToAdd) {
this.osmcal.queryToAdd += ` , `;
}
let title = "'" + htmlEscape(this.osmcal.getTitle(event)) + "'";
let content = "'" + htmlEscape(`${event.date.human} <br/>${+event.url} <br/> `) + "'";
if (event.location) {
content += `${event.location.detailed} `
if (event.location.venue) {
content += ` <br/>
${event.location.venue}`
}
if (event.location.coords) {
content += ` <br/> ` + this.convertCoordinateLinkOsmCal(event.location.coords)
}
}
2021-01-05 13:26:42 +01:00
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++;
2021-01-05 11:48:41 +01:00
this.counterOfEventsToAdd++;
},
};
agendadulibre = {
queryToAdd: "",
counterOfEventsToAdd: 0,
2022-01-10 10:29:30 +01:00
doesEventExists: (event: any) => {
if (this.localMobilizonEventsByTitle.length) {
const eventAlreadyExists =
-1 !== this.localMobilizonEventsByTitle.indexOf(event.title);
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log('ajouter l event ', htmlEscape(event.title));
}
this.agendadulibre.addQuery(event);
2021-01-05 13:26:42 +01:00
}
2022-01-10 10:29:30 +01:00
return eventAlreadyExists;
2021-01-05 13:26:42 +01:00
}
2022-01-10 10:29:30 +01:00
return false;
2021-01-05 11:48:41 +01:00
},
2022-01-10 10:29:30 +01:00
addQuery: (event: any) => {
2021-01-05 11:48:41 +01:00
if (this.agendadulibre.queryToAdd) {
this.agendadulibre.queryToAdd += ` , `;
}
2021-01-05 13:26:42 +01:00
let title = "'" + htmlEscape(event.title) + "'";
let content = "'" + htmlEscape(event.content) + "'";
2021-01-05 11:48:41 +01:00
let uuid = uuidv4();
let uuidString = "'" + uuid + "'";
let eventUrl =
"'" + parserConfig.mobilizon_public_url + "/events/" + uuid + "'";
2021-01-05 13:26:42 +01:00
let begins_on = "'" + this.convertRssDate(event.date) + "'";
let ends_on = "'" + this.convertRssDate(event.date) + "'";
2021-01-05 11:48:41 +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}';
// begins_on , ends_on expecting date format like this: "2020-12-17 23:00:00"
this.agendadulibre.queryToAdd += `( ${title}, ${content}, ${parserConfig.feeder_mobilizon_user_id}, 'now()','now()', ${uuidString}, ${eventUrl}, 'confirmed' , 'meeting', ${baseOptions}, ${baseStats}, ${begins_on} , ${ends_on} )`;
this.agendadulibre.counterOfEventsToAdd++;
this.counterOfEventsToAdd++;
2021-01-05 11:17:54 +01:00
}
2021-01-05 11:48:41 +01:00
};
setupClientPostgresql = () => {
this.client = new Client({
host: "localhost",
user: parserConfig.db_user,
password: parserConfig.db_pass,
database: parserConfig.db_name,
});
}
2021-01-03 20:32:27 +01:00
}
2021-01-05 11:17:54 +01:00
2021-01-03 20:32:27 +01:00
export default utils;