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.
runAddQueriesToMobilizonAPI: true,
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
limit_persistence_of_new_events:true,
max_new_events: 1,
bearer_token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJtb2JpbGl6b24iLCJleHAiOjE2NDIwNzIyMTQsImlhdCI6MTY0MjA3MTMxNCwiaXNzIjoibW9iaWxpem9uIiwianRpIjoiMjBiNGMyNGEtODViYi00MWFlLTk2MjYtMjZhZjFlYThlMTliIiwibmJmIjoxNjQyMDcxMzEzLCJzdWIiOiJVc2VyOjEiLCJ0eXAiOiJhY2Nlc3MifQ.N-QBR0lfj8KX2icUjOVImahIKvZu1rVJrocCqWvC3Z9LnEaNJ5JEqSdtqnMZBD_v5zK0zfddhBMoa8Iv-KhkaA",
max_new_events_in_scrapping: 10,
max_new_events: 2,
bearer_token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJtb2JpbGl6b24iLCJleHAiOjE2NDIwODA2NTAsImlhdCI6MTY0MjA3OTc1MCwiaXNzIjoibW9iaWxpem9uIiwianRpIjoiYmZjNWFkNjMtODA2NS00Y2FhLWJiY2EtYzA2MDZkNDEwNDdlIiwibmJmIjoxNjQyMDc5NzQ5LCJzdWIiOiJVc2VyOjEiLCJ0eXAiOiJhY2Nlc3MifQ.Ar5oUTs0cFOEkznYmhXgrFkGRQv4yJm34lN_gkNSrCTrvXfRBTID4YzIrbzvKOkYvIZmw0dHb7gu1WO_tATBkQ",
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');
console.log('💾 évènements enregistrés dans mobilizon : ', res.rows.length);
res.rows.forEach((bdd_event: any) => {
if (utilsTools.localMobilizonEventsByTitle.indexOf(utilsTools.agendadulibre.uniqTitleBDD(bdd_event)) === -1) {
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
await fs.readFile(filepath, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
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;
filecontent.forEach((event: any) => {
ii++
// compare events with existing events
// let date = new Date(event.start_time);
// console.log(ii, date.toLocaleDateString('fr-fr', { weekday:"long", year:"numeric", month:"short", day:"numeric"}) , event.title );
utilsTools.agendadulibre.doesEventExistsFromJsonScrap(event);
let eventAlreadyExists = utilsTools.agendadulibre.doesEventExistsFromJsonScrap(event);
if (!eventAlreadyExists) {
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');
// 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 counter = 0;
utilsTools.agendadulibre.queryToAdd.forEach((graphQLquery: any) => {
if (limiter && counter <= parserConfig.max_new_events) {
console.log('utilsTools.localMobilizonEventsByTitle', utilsTools.localMobilizonEventsByTitle);
utilsTools.newEvents.forEach((pair: any) => {
let graphQLquery = pair.newQuery;
if (limiter && counter < parserConfig.max_new_events) {
counter++;
console.log(counter, ' * ', graphQLquery.variables.beginsOn, graphQLquery.variables.title);
console.log(counter, ' * ', utilsTools.agendadulibre.uniqTitle(pair.event));
const body = {
operationName: "createEvent",
@ -73,7 +89,12 @@ async function runImportEvents() {
"authorization": "Bearer " + parserConfig.bearer_token,
}
}
if(!utilsTools.agendadulibre.doesEventExistsFromJsonScrap(pair.event)){
console.log('ajouter');
fetchEvent(url, options)
}else{
console.log('nope');
}
} else {
console.log('---- le fetch est désactivé');
}
@ -107,7 +128,6 @@ const fetchEvent = (theUrl, theOptions) => {
// fs.stat(filepath, function (err, stat) {
// if (err == null) {
runImportEvents();
console.log('fetch', fetch);
// } else if (err.code === 'ENOENT') {
// // 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);

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 {htmlEscape} from "escape-goat";
import fetch from 'node-fetch';
const moment = require("moment");
const fs = require("fs");
@ -51,7 +52,7 @@ class utils {
*/
counterOfEventsToAdd = 0;
localMobilizonEventsByTitle: Array<string> = [];
newEvents: Array<any> = [];
/**
* converters
@ -99,9 +100,6 @@ class utils {
</div> `
}
testdateconvert() {
let converted = this.convertRssDate("2021-03-12T19:00:00Z");
}
/**
* file management
@ -199,12 +197,7 @@ class utils {
if (this.localMobilizonEventsByTitle.length) {
const eventAlreadyExists =
-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;
} else {
console.log('aucun évènement dans localMobilizonEventsByTitle');
@ -216,29 +209,27 @@ class utils {
* @param event
*/
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
* @param event
*/
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 => {
console.log('this.agendadulibre.uniqTitle(event)', this.agendadulibre.uniqTitle(event));
const eventAlreadyExists =
-1 !== this.localMobilizonEventsByTitle.indexOf(htmlEscape(event.start_date + ' ' + event.title));
if (!eventAlreadyExists) {
if (parserConfig.debug) {
console.log('ajouter l event ', htmlEscape(event.start_date + ' ' + event.title));
}
this.agendadulibre.addQueryFromJsonScrap(event);
}
-1 !== this.localMobilizonEventsByTitle.indexOf(this.agendadulibre.uniqTitle(event));
console.log('eventAlreadyExists', eventAlreadyExists);
return eventAlreadyExists;
},
addQueryFromJsonScrap: (event: any) => {
this.agendadulibre.queryToAdd.push(
{
let newQuery = {
operationName: "createEvent",
query: createEventQueryMobilizon,
variables: {
@ -275,9 +266,8 @@ class utils {
visibility: "PUBLIC"
}
}
);
return this.agendadulibre.queryToAdd;
return newQuery;
},
addQuery: (event: any) => {
console.log('event', event.title);