This code retrieves upcoming events from a Google Calendar, parses parameters from their descriptions, and prepares them for task execution.
npm run import -- "Run todays calendar events"
var importer = require('../Core');
var {JSDOM} = require('jsdom');
var {
listEvents,
storeResult
} = importer.import("list events",
"import google calendar api",
"store rpc result");
var options = {
calendarId: 'commands'
};
// test Google calendar API?
function runTodaysEvents(calendar) {
if(calendar) {
options.calendarId = calendar;
}
return listEvents({
auth: options.auth,
calendarId: options.calendarId,
timeMin: '12 AM today',
timeMax: 'next hour today'
})
// filter processed
.then(events => {
// TODO: create object with property "already" and filter out like other RPC methods?
// determine if the event has already been run today by matching it with the result
const commandEvent = events
.filter(e => {
const matches = events
.filter(m => 'Result: ' + e.event.summary === m.event.summary
&& e.event.start.dateTime === m.event.start.dateTime);
return e.event.summary.indexOf('Result:') === -1 && matches.length === 0
})[0]
if (typeof commandEvent === 'undefined') throw new Error('No events!');
// parse parameters from event description
var parameters;
try {
var dom = new JSDOM('<body>' + (commandEvent.event.description || '""').replace(/<br\/?>/igm, '\n') + '</body>');
parameters = JSON.parse(dom.window.document.body.textContent);
} catch ( e ) {
parameters = (commandEvent.event.description || '').trim().split(/\n/ig);
}
return {
already: false, // filtered out above
body: parameters,
name: 'Today\'s events',
command: commandEvent.event.summary.trim(),
date: new Date(commandEvent.event.start.dateTime),
id: commandEvent.event.summary.trim(),
circles: ['Function', 'Selenium'],
result: importer.interpret(commandEvent.event.summary.trim()),
allowed: true // TODO: use RPC filter because it's safer?
};
})
// use rpc
.then(f => storeResult(f, options.calendarId))
.catch(e => console.log(e))
}
module.exports = runTodaysEvents;
// node -e "require('/Users/briancullinan/jupytangular2/Core').import("heartbeat")('run todays calendar events')"
// echo "require('/Users/briancullinan/jupytangular2/Core').import("scrape facebook events").then(runner => runner()).then(e=>{console.log(e); process.exit(e) }).catch(e=>{console.log(e); process.exit(e) });" | node
const { JSDOM } = require('jsdom');
const { listEvents, storeResult } = require('./Core').import([
'list events',
'store rpc result'
]);
const runTodaysEvents = async (calendar = 'commands') => {
try {
const options = { calendarId: calendar };
const events = await listEvents({
auth: options.auth,
calendarId: options.calendarId,
timeMin: '12:00 AM today',
timeMax: '1:00 AM today'
});
const commandEvent = events
.find((event) =>!event.event.summary.startsWith('Result: ') &&!events.find((m) => m.event.summary === event.event.summary && m.event.start.dateTime === event.event.start.dateTime));
if (!commandEvent) {
throw new Error('No events!');
}
const dom = new JSDOM('' + (commandEvent.event.description || '').replace(/
/igm, '\n') + '');
const parameters = tryParseJson(dom.window.document.body.textContent);
if (parameters === null) {
parameters = (commandEvent.event.description || '').trim().split(/\n/ig);
}
const result = {
already: false,
body: parameters,
name: 'Today\'s events',
command: commandEvent.event.summary.trim(),
date: new Date(commandEvent.event.start.dateTime),
id: commandEvent.event.summary.trim(),
circles: ['Function', 'Selenium'],
result: importer.interpret(commandEvent.event.summary.trim()),
allowed: true
};
await storeResult(result, options.calendarId);
return result;
} catch (e) {
console.error(e);
throw e;
}
};
module.exports = runTodaysEvents;
// Helper function to attempt to parse JSON
function tryParseJson(str) {
try {
return JSON.parse(str);
} catch (e) {
return null;
}
}
This code snippet fetches upcoming events from a Google Calendar, extracts parameters from their descriptions, and prepares them for execution.
Here's a breakdown:
Imports:
JSDOM
for parsing HTML content.Configuration:
options
object with a default calendar ID.runTodaysEvents
Function:
calendar
argument to specify a different calendar.listEvents()
to retrieve events within a specific time range (today's events).JSDOM
to extract data.Purpose:
This code likely serves as part of a system that schedules and executes tasks based on events in a Google Calendar. It fetches upcoming events, extracts parameters from their descriptions, and prepares them for further processing or execution.