compare events date

This commit is contained in:
Tykayn 2022-01-13 14:30:45 +01:00 committed by tykayn
parent 7228429013
commit bea58afa1e
4 changed files with 89 additions and 91 deletions

View File

@ -20,10 +20,12 @@ const parserConfig = {
organizerActorId: 3, // ID of the user who imports events in the mobilizon instance. 3 is the first admin user created, usually. organizerActorId: 3, // ID of the user who imports events in the mobilizon instance. 3 is the first admin user created, usually.
runAddQueriesToMobilizonAPI: true, runAddQueriesToMobilizonAPI: true,
enableFetch: true, // enable persisting of new events via API enableFetch: true, // enable persisting of new events via API
// enableFetch: false,
dev_mode: true, // dev mode uses localhost instance of mobilizon running on port 4000 dev_mode: true, // dev mode uses localhost instance of mobilizon running on port 4000
limit_persistence_of_new_events:true, limit_persistence_of_new_events:true,
max_new_events: 1, max_new_events_in_scrapping: 10,
bearer_token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJtb2JpbGl6b24iLCJleHAiOjE2NDIwNzIyMTQsImlhdCI6MTY0MjA3MTMxNCwiaXNzIjoibW9iaWxpem9uIiwianRpIjoiMjBiNGMyNGEtODViYi00MWFlLTk2MjYtMjZhZjFlYThlMTliIiwibmJmIjoxNjQyMDcxMzEzLCJzdWIiOiJVc2VyOjEiLCJ0eXAiOiJhY2Nlc3MifQ.N-QBR0lfj8KX2icUjOVImahIKvZu1rVJrocCqWvC3Z9LnEaNJ5JEqSdtqnMZBD_v5zK0zfddhBMoa8Iv-KhkaA", max_new_events: 2,
bearer_token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJtb2JpbGl6b24iLCJleHAiOjE2NDIwODA2NTAsImlhdCI6MTY0MjA3OTc1MCwiaXNzIjoibW9iaWxpem9uIiwianRpIjoiYmZjNWFkNjMtODA2NS00Y2FhLWJiY2EtYzA2MDZkNDEwNDdlIiwibmJmIjoxNjQyMDc5NzQ5LCJzdWIiOiJVc2VyOjEiLCJ0eXAiOiJhY2Nlc3MifQ.Ar5oUTs0cFOEkznYmhXgrFkGRQv4yJm34lN_gkNSrCTrvXfRBTID4YzIrbzvKOkYvIZmw0dHb7gu1WO_tATBkQ",
ccpl: "https://www.cc-paysdelimours.fr/agenda" ccpl: "https://www.cc-paysdelimours.fr/agenda"

View File

@ -25,37 +25,53 @@ async function runImportEvents() {
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); console.log('💾 évènements enregistrés dans mobilizon : ', res.rows.length);
res.rows.forEach((bdd_event: any) => { res.rows.forEach((bdd_event: any) => {
if (utilsTools.localMobilizonEventsByTitle.indexOf(utilsTools.agendadulibre.uniqTitleBDD(bdd_event)) === -1) {
utilsTools.localMobilizonEventsByTitle.push(utilsTools.agendadulibre.uniqTitleBDD(bdd_event)); utilsTools.localMobilizonEventsByTitle.push(utilsTools.agendadulibre.uniqTitleBDD(bdd_event));
}
}) })
console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle); console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle.length);
// get json file for ADL // get json file for ADL
await fs.readFile(filepath, 'utf8', function (err, data) { await fs.readFile(filepath, 'utf8', function (err, data) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
filecontent = JSON.parse(data) filecontent = JSON.parse(data)
// console.log('events in the scrapped json', filecontent.length); filecontent = filecontent.slice(0,parserConfig.max_new_events_in_scrapping)
console.log('events in the scrapped json', filecontent.length);
let ii = 0; let ii = 0;
filecontent.forEach((event: any) => { filecontent.forEach((event: any) => {
ii++ ii++
// compare events with existing events // compare events with existing events
// let date = new Date(event.start_time); let eventAlreadyExists = utilsTools.agendadulibre.doesEventExistsFromJsonScrap(event);
// console.log(ii, date.toLocaleDateString('fr-fr', { weekday:"long", year:"numeric", month:"short", day:"numeric"}) , event.title ); if (!eventAlreadyExists) {
utilsTools.agendadulibre.doesEventExistsFromJsonScrap(event); if (parserConfig.debug) {
console.log('ajouter l event ', utilsTools.agendadulibre.uniqTitle(event));
}
let newQuery = utilsTools.agendadulibre.addQueryFromJsonScrap(event);
utilsTools.newEvents.push({
event,
newQuery
});
}
}) })
// console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle);
console.log('end looking for events'); console.log('end looking for events');
// import only new events // import only new events
console.log('nouveaux évènements à ajouter: ', utilsTools.agendadulibre.queryToAdd.length); console.log('nouveaux évènements à ajouter: ', utilsTools.newEvents.length);
let limiter = parserConfig.limit_persistence_of_new_events; let limiter = parserConfig.limit_persistence_of_new_events;
let counter = 0; let counter = 0;
utilsTools.agendadulibre.queryToAdd.forEach((graphQLquery: any) => { console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle);
if (limiter && counter <= parserConfig.max_new_events) { utilsTools.newEvents.forEach((pair: any) => {
let graphQLquery = pair.newQuery;
if (limiter && counter < parserConfig.max_new_events) {
counter++; counter++;
console.log(counter, ' * ', graphQLquery.variables.beginsOn, graphQLquery.variables.title); console.log(counter, ' * ', utilsTools.agendadulibre.uniqTitle(pair.event));
const body = { const body = {
operationName: "createEvent", operationName: "createEvent",
@ -73,7 +89,12 @@ async function runImportEvents() {
"authorization": "Bearer " + parserConfig.bearer_token, "authorization": "Bearer " + parserConfig.bearer_token,
} }
} }
if(!utilsTools.agendadulibre.doesEventExistsFromJsonScrap(pair.event)){
console.log('ajouter');
fetchEvent(url, options) fetchEvent(url, options)
}else{
console.log('nope');
}
} else { } else {
console.log('---- le fetch est désactivé'); console.log('---- le fetch est désactivé');
} }
@ -107,7 +128,6 @@ const fetchEvent = (theUrl, theOptions) => {
// fs.stat(filepath, function (err, stat) { // fs.stat(filepath, function (err, stat) {
// if (err == null) { // if (err == null) {
runImportEvents(); runImportEvents();
console.log('fetch', fetch);
// } else if (err.code === 'ENOENT') { // } else if (err.code === 'ENOENT') {
// // file does not exist // // file does not exist
// console.log('Scrapped json file does not exist. Run a scraper like "ts-node scrapers/adl.ts" before using this importer: ', err.code); // console.log('Scrapped json file does not exist. Run a scraper like "ts-node scrapers/adl.ts" before using this importer: ', err.code);

View File

@ -1,14 +0,0 @@
const mongoose = require('mongoose');
let EventMobilizonSchema = new mongoose.Schema({
title: String,
date_start: Date,
date_end: Date,
description: Date,
url: Date,
dateCrawled: Date
});
let EventMobilizon = mongoose.model('EventMobilizon', userSchema);
module.exports = EventMobilizon;

View File

@ -4,6 +4,7 @@ import {v4 as uuidv4} from "uuid";
import {Client} from "pg"; import {Client} from "pg";
import {htmlEscape} from "escape-goat"; import {htmlEscape} from "escape-goat";
import fetch from 'node-fetch'; import fetch from 'node-fetch';
const moment = require("moment"); const moment = require("moment");
const fs = require("fs"); const fs = require("fs");
@ -51,7 +52,7 @@ class utils {
*/ */
counterOfEventsToAdd = 0; counterOfEventsToAdd = 0;
localMobilizonEventsByTitle: Array<string> = []; localMobilizonEventsByTitle: Array<string> = [];
newEvents: Array<any> = [];
/** /**
* converters * converters
@ -99,9 +100,6 @@ class utils {
</div> ` </div> `
} }
testdateconvert() {
let converted = this.convertRssDate("2021-03-12T19:00:00Z");
}
/** /**
* file management * file management
@ -199,12 +197,7 @@ class utils {
if (this.localMobilizonEventsByTitle.length) { if (this.localMobilizonEventsByTitle.length) {
const eventAlreadyExists = const eventAlreadyExists =
-1 !== this.localMobilizonEventsByTitle.indexOf(this.agendadulibre.uniqTitle(event)); -1 !== this.localMobilizonEventsByTitle.indexOf(this.agendadulibre.uniqTitle(event));
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log('ajouter l event ', htmlEscape(this.agendadulibre.uniqTitle(event)));
}
this.agendadulibre.addQuery(event);
}
return eventAlreadyExists; return eventAlreadyExists;
} else { } else {
console.log('aucun évènement dans localMobilizonEventsByTitle'); console.log('aucun évènement dans localMobilizonEventsByTitle');
@ -216,29 +209,27 @@ class utils {
* @param event * @param event
*/ */
uniqTitle(event: any): string { uniqTitle(event: any): string {
return event.start_date + ' ' + event.title
return moment(new Date(event.start_time))
.format("YYYY-MM-DD") + ' ' + event.title
}, },
/** /**
* convert events from bdd to a string used as a comparison with new events * convert events from bdd to a string used as a comparison with new events
* @param event * @param event
*/ */
uniqTitleBDD(event: any): string { uniqTitleBDD(event: any): string {
return event.begins_on + ' ' + event.title return moment(event.begins_on)
.format("YYYY-MM-DD") + ' ' + event.title
}, },
doesEventExistsFromJsonScrap: (event: any): boolean => { doesEventExistsFromJsonScrap: (event: any): boolean => {
console.log('this.agendadulibre.uniqTitle(event)', this.agendadulibre.uniqTitle(event));
const eventAlreadyExists = const eventAlreadyExists =
-1 !== this.localMobilizonEventsByTitle.indexOf(htmlEscape(event.start_date + ' ' + event.title)); -1 !== this.localMobilizonEventsByTitle.indexOf(this.agendadulibre.uniqTitle(event));
if (!eventAlreadyExists) { console.log('eventAlreadyExists', eventAlreadyExists);
if (parserConfig.debug) {
console.log('ajouter l event ', htmlEscape(event.start_date + ' ' + event.title));
}
this.agendadulibre.addQueryFromJsonScrap(event);
}
return eventAlreadyExists; return eventAlreadyExists;
}, },
addQueryFromJsonScrap: (event: any) => { addQueryFromJsonScrap: (event: any) => {
this.agendadulibre.queryToAdd.push( let newQuery = {
{
operationName: "createEvent", operationName: "createEvent",
query: createEventQueryMobilizon, query: createEventQueryMobilizon,
variables: { variables: {
@ -275,9 +266,8 @@ class utils {
visibility: "PUBLIC" visibility: "PUBLIC"
} }
} }
);
return this.agendadulibre.queryToAdd; return newQuery;
}, },
addQuery: (event: any) => { addQuery: (event: any) => {
console.log('event', event.title); console.log('event', event.title);