This code snippet correlates Google Calendar events with nearby locations based on location names, event summaries, and event locations, likely for analyzing travel patterns or daily routines. It identifies matching events and locations, logging the results for further analysis.
npm run import -- "Reconcile timeline data with calendar"
var unidecode = require('unidecode');
var importer = require('../Core');
var {
getDaysEvents,
getNearby,
getOauthClient
} = importer.import("days events",
"import google calendar api",
"cache locations nearby");
function filterEvents(events, locations) {
const unmatched = [], matches = [];
for (const l of locations) {
const matching = events.filter(e => {
return ((l.name + '').indexOf('Gainey') > -1
&& e.event.summary == 'Drive to work')
|| ((l.name + '').indexOf('Swiftpage') > -1
&& e.event.summary == 'Drive to work')
|| ((l.name + '').indexOf('6934') > -1
&& e.event.summary == 'Drive home')
|| ((l.name + '').indexOf('6934') > -1
&& e.event.summary == 'Work from home')
|| (typeof e.event.location !== 'undefined'
&& ((e.event.location + '').indexOf(l.location + '') > -1
|| (e.event.summary + '').indexOf(l.name + '') > -1
|| (e.event.location + '').indexOf(l.name + '') > -1
|| (e.event.location + '').indexOf((l.name + '').split(/[,:-]/ig)[0]) > -1
|| (e.event.summary + '').indexOf((l.name + '').split(/[,:-]/ig)[0]) > -1
|| (e.event.summary + '').indexOf((l.location + '').split(/[,:-]/ig)[0]) > -1
|| (e.event.location + '').indexOf((l.location + '').split(/[,:-]/ig)[0]) > -1
|| (e.event.location + '').indexOf(unidecode(l.name + '').split(/[,:-]/ig)[0]) > -1))
});
if (matching.length == 0) {
unmatched.push(l);
} else {
const result = {};
Object.assign(result, l);
Object.assign(result, matching[0]);
matches.push(result);
Object.assign(result.event, l);
}
}
console.log(events.map(e => e.event.summary + ' - ' + e.event.location));
console.log('Unmatched ' + unmatched.length
+ ' - out of ' + matches.length
+ ' - ' + unmatched.map(u => u.name + '').join(', '));
return events;
}
var options = {
calendarId: 'primary'
};
function reconcileTimelineLocations(destinations, date) {
var locations, events;
return getOauthClient(options)
.then(() => getDaysEvents(date, options))
.then(r => {
events = r;
return getNearby(destinations);
})
.then(locs => {
locations = locs;
return filterEvents(events, locations);
})
.catch(e => console.log(e))
.then(() => events.map(e => e.event))
}
module.exports = reconcileTimelineLocations;
const unidecode = require('unidecode');
const { getDaysEvents, getNearby, getOauthClient } = require('../Core');
/**
* Filter events based on locations.
*
* @param {Array} events - List of events.
* @param {Array} locations - List of locations.
* @returns {Object} Updated events with matched locations.
*/
function filterEvents(events, locations) {
const unmatched = locations.filter(location =>!events.some(event => matchesEvent(event, location)));
const matches = locations.reduce((acc, location) => {
const matching = events.find(event => matchesEvent(event, location));
return matching? [...acc, {...location,...matching, event: location }] : acc;
}, []);
console.log(events.map(e => `${e.event.summary} - ${e.event.location}`));
console.log(`Unmatched ${unmatched.length} - out of ${matches.length} - ${unmatched.map(u => u.name).join(', ')}`);
return events;
}
/**
* Check if an event matches a location.
*
* @param {Object} event - Event object.
* @param {Object} location - Location object.
* @returns {Boolean} Whether the event matches the location.
*/
function matchesEvent(event, location) {
const name = location.name.toLowerCase();
const locationStr = location.location.toLowerCase();
return (
(name.includes('gainey') && event.event.summary === 'Drive to work') ||
(name.includes('swiftpage') && event.event.summary === 'Drive to work') ||
(name.includes('6934') && (event.event.summary === 'Drive home' || event.event.summary === 'Work from home')) ||
(typeof event.event.location!== 'undefined' &&
(locationStr.includes(event.event.location.toLowerCase()) ||
name.includes(event.event.summary.toLowerCase()) ||
locationStr.includes(name) ||
locationStr.includes(name.split(/[,:-]/ig)[0]) ||
name.includes(locationStr.split(/[,:-]/ig)[0]) ||
name.includes(locationStr.split(/[,:-]/ig)[0]) ||
name.includes(unidecode(locationStr).split(/[,:-]/ig)[0])))
);
}
/**
* Reconcile timeline locations.
*
* @param {Array} destinations - List of destinations.
* @param {Date} date - Date to fetch events for.
* @returns {Promise} Updated events with matched locations.
*/
function reconcileTimelineLocations(destinations, date) {
const options = { calendarId: 'primary' };
return getOauthClient(options)
.then(() => getDaysEvents(date, options))
.then(r => getNearby(destinations))
.then(([events, locations]) => filterEvents(events, locations))
.catch(e => console.log(e))
.then(() => events.map(e => e.event));
}
module.exports = reconcileTimelineLocations;
This code snippet aims to match events from a Google Calendar with nearby locations.
Here's a breakdown:
Dependencies:
unidecode
: A library for converting Unicode characters to ASCII.importer
: A custom module likely containing functions for interacting with Google Calendar and retrieving nearby locations.Importing Functions:
getDaysEvents
, getNearby
, and getOauthClient
from the importer
module.filterEvents
Function:
events
(an array of calendar events) and locations
(an array of nearby locations).unmatched
to store locations without matching events and matches
to store matched locations and events.locations
.events
array to find matching events based on:
unidecode
for case-insensitive comparison)matches
array.unmatched
array.Overall, this code snippet aims to correlate calendar events with nearby locations based on various criteria, likely for analyzing travel patterns or generating insights about daily routines.