Skip to content

Instantly share code, notes, and snippets.

@jkjustjoshing
Last active July 23, 2018 20:02
Show Gist options
  • Save jkjustjoshing/20ad214b89b0d39bac8733bd3c082f0e to your computer and use it in GitHub Desktop.
Save jkjustjoshing/20ad214b89b0d39bac8733bd3c082f0e to your computer and use it in GitHub Desktop.
This scrapes the HTML on Hal Higdon's training pages, with a race date, and exports a .ics file to import the workouts into Google Calendar, iCal, etc.
(function () {
function hashCode (string){
var hash = 0;
if (string.length == 0) return hash;
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
var training = document.querySelectorAll('.training-schedule #miles tr td');
var items = Array.from(training).filter(function (item, index) {
return index % 8 !== 0;
}).map(function (item) {
return item.textContent;
});
var marathonDate = new Date(prompt('Marathon date? (assumes Sunday)'));
var finalEvents = Array.from(items).reverse().map(function (item) {
var month = (marathonDate.getMonth() + 1);
if (month < 10) {
month = '0' + month;
}
var date = (marathonDate.getDate() + 1);
if (date < 10) {
date = '0' + date;
}
var toReturn = {
text: item,
date: marathonDate.getFullYear() + '-' + month + '-' + date
};
marathonDate.setDate(marathonDate.getDate() - 1);
return toReturn;
}).reverse().map(function (object) {
return `BEGIN:VEVENT
UID:${hashCode(JSON.stringify(object))}
ORGANIZER;CN=Josh Kramer:MAILTO:jkjustjoshing@gmail.com
DTSTART:${object.date.replace(/-/g, '')}
DTEND:${object.date.replace(/-/g, '')}
SUMMARY:${object.text}
END:VEVENT`;
});
var vcal = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
${finalEvents.join(`\n`)}
END:VCALENDAR`;
var link = document.createElement('a');
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(vcal));
link.setAttribute('download', 'trainingPlan.ics');
link.setAttribute('style', 'font-size: 30px; color: red;');
link.appendChild(document.createTextNode('Download Training Plan'));
document.body.appendChild(link);
return link;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment