Skip to content

Instantly share code, notes, and snippets.

@cades

cades/transformer.js

Last active Jun 5, 2020
Embed
What would you like to do?
讀取 google calendar api v3 / Events: list , 轉換成 FullCalendar 的格式
var util = require('util'),
request = require('request'),
parser = require('xml2json'),
calendar_id = 'fgj3gh1p2chvsb0vge2sjrejag@group.calendar.google.com',
api_key = 'AIzaSyBQal2rNhP5SRkU5hZytY7Yb8nYc5Q1nrc',
url = util.format('https://www.googleapis.com/calendar/v3/calendars/%s/events?key=%s', calendar_id, api_key);
request({url: url}, function(error, response, json){
var data = JSON.parse(json),
transformed = data.items.map(function(event){
return {
id: event.id,
title: event.summary || '忙碌',
start: event.start.dateTime || event.start.date,
end: event.end.dateTime || event.start.date, // because end.date may be the next day, cause a '2-all-day' event, we use start.date here.
allDay: !!event.start.date
};
});
console.log(JSON.stringify(transformed));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment