I'm currently in the process of integrating JSON feeds from multiple Google calendars to organize upcoming events and showcase the next X number of events in an "Upcoming Events" list.
While I initially achieved this using Yahoo! Pipes, I aim to eliminate reliance on third-party aggregators. Despite being close, I'm encountering challenges with creating the JSON objects correctly. The data gets stored in an array, but not in JSON format, hindering manipulation.
Attempts like
var myJsonString = JSON.stringify(JSONData);
with https://github.com/douglascrockford/JSON-js proved error-prone, possibly due to the variable's incorrect initial format. Likewise, simple feed calls such as $.getJSON(url);
coupled with a function like function concant1()
to perform JSONData=JSONData.concat(data);
do not execute, leading to likely similar outcomes. Various other methods tried yield mixed results. Here's the closest I've gotten:
var JSONData = new Array();
var urllist = ["URL1","URL2","URL3"];
urllist.forEach(function addFeed(url){
alert("Using URL: "+ url);
if (void 0 != JSONData){JSONData=JSONData.concat($.getJSON(url));}
else{JSONData = $.getJSON(url);}
alert("The count from concatenated JSONData: "+JSONData.length);
});
document.write("Final JSONData count: "+JSONData.length+"<p>");
console.log(JSONData)
UPDATE: Now fully functional source code!! :) Suggestions for enhancing code efficiency are welcome. I hope this proves helpful to others.:
// GCal MFA - Google Calendar Multiple Feed Aggregator
// Usage: GCalMFA(CIDs,n);
// Where 'CIDs' represents comma-separated Google calendar IDs like <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="731a173314011c06035d10121f161d1712015d141c1c141f165d101c1e">[email protected]</a>, and 'n' indicates the number of results to display.
// Eliminate this error-checking code for IE, and the console.log statements if not required for regular use
// Author: Jeramy Kruser - http://jeramy.kruser.me
// Add a tag to your page to identify it as js-enabled for CSS purposes
document.body.className += ' js-enabled';
// Global variables
var urllist = [];
var maxResults = 3;
var JSONData = {};
var eventCount = 0;
// Prototype forEach necessary for IE
if ( !Array.prototype.forEach ) {
Array.prototype.forEach = function(fn, scope) {
for(var i = 0, len = this.length; i < len; ++i) {
fn.call(scope, this[i], i, this);
}
}
}
// Function to parse dates
function parse (str) {
str = str.match (/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$/);
if (str) {
str[0] = new Date ( + str[1], + str[2] - 1, + str[3]);
if (str[0].getMonth () === + str[2] - 1) {
return str[0];
}
}
return undefined;
}
// Function for HTML output
function output() {
// For output HTML
}
// Function for sorting events
function sortFeed(event) {
// For sorting events
}
// Function to complete aggregation
function complete(result) {
// Complete aggregation
}
// Main function for multiple feed aggregation
function GCalMFA(list,results){
// Main function
}