Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calendar parser for codemotion
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('data.json', 'utf8'));
var result = "";
var writer = {
log : function(params){
result += params + "\r\n";
console.log(params);
},
end: function(){
fs.writeFile('codemotion.ics',result);
}
}
function pad(num) {
var zero = 2 - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
//var writer = console;
writer.log("BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN")
obj.days.forEach(function(day) {
day.tracks.forEach(function(track) {
track.slots.forEach(function(slot) {
if(!slot.contents){
return;
}
if(!slot.contents.title){
return;
}
writer.log("BEGIN:VEVENT");
if(slot.contents.type == "BREAK"){
writer.log("SUMMARY:" + slot.contents.title);
} else {
writer.log("SUMMARY:" + "[" + track.name + "] " + slot.contents.title);
}
var dayName = day.name.substring(0,2);
writer.log("DTSTART:201511" + dayName + "T" + pad(parseInt(slot.start.substring(0,2)) - 1) + slot.start.substring(3,5) + "00Z");
writer.log("DTEND:201511" + dayName + "T" + pad(parseInt(slot.end.substring(0,2)) - 1) + slot.end.substring(3,5) + "00Z");
var description = "";
if(slot.contents.description){
description += "DESCRIPTION:" + slot.contents.description.replace(/(\r\n|\n|\r)/gm,"\\n");
}
if(slot.contents.authors){
for(var i = 0; i < slot.contents.authors.length; i++){
var author = slot.contents.authors[i];
description += "\\n\\nAutor: " + author.name + " (" + author.uuid + ") \\n:" + author.description.replace(/(\r\n|\n|\r)/gm,"");
}
}
if(slot.contents.description){
writer.log(description);
}
writer.log("END:VEVENT");
}, this);
}, this);
}, this);
writer.log("END:VCALENDAR");
if(writer.end){
writer.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.