Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FokkeZB/617f32dcde946395f2f5 to your computer and use it in GitHub Desktop.
Save FokkeZB/617f32dcde946395f2f5 to your computer and use it in GitHub Desktop.
Preparing restored iCloud Calendar Events to be imported in a new Calendar
var fs = require('fs'),
path = require('path');
var glob = require('glob'),
uuid = require('node-uuid');
var input = process.argv[2] || '';
var output = process.argv[3] || '';
fs.exists(input, function it(exists) {
if (!exists) {
console.error('Input path not found: ' + input);
} else {
fs.exists(output, function it(exists) {
if (!exists) {
console.error('Output path not found: ' + output);
} else {
glob('**/*.ics', {
cwd: input
}, function handle(err, files) {
if (err) {
console.error(err);
} else {
files.forEach(function forEach(file, fileNr) {
var inputFilePath = path.join(input, file);
fs.readFile(inputFilePath, 'utf8', function handle(err, inputData) {
if (err) {
console.error(inputFilePath + ': ' + err);
} else {
var outputFilePath = path.join(output, path.basename(file));
var inputLines = inputData.split('\r\n');
var outputLines = [];
inputLines.forEach(function forEach(inputLine, index) {
var outputLine;
if (inputLine.substr(0, 2) === 'X-') {
return;
} else {
var parts = inputLine.split(':');
if (parts[0] === 'UID') {
var newUid = uuid.v4().toUpperCase();
if (path.basename(file) === parts[1] + '.ics') {
outputFilePath = path.join(output, newUid + '.ics');
}
parts[1] = newUid;
inputLine = parts.join(':');
}
if (outputLine) {
console.log(inputLine + ' > ' + outputLine);
outputLines.push(outputLine);
} else {
outputLines.push(inputLine);
}
}
});
var outputData = outputLines.join('\r\n');
// console.log(inputFilePath);
// console.log(inputData);
// console.log(outputFilePath);
// console.log(outputData);
fs.writeFile(outputFilePath, outputData, function(err) {
if (err) {
console.error(inputFilePath + ': ' + err);
}
});
}
});
});
}
});
}
});
}
});
{
"name": "ics",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"glob": "^4.3.3",
"node-uuid": "^1.4.2"
}
}
@mjpost
Copy link

mjpost commented Aug 20, 2015

I couldn't get node.js to work, so I rewrote this in Python. Thanks for the article; I have no idea how I deleted my calendar, but i was an important one, and this worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment