Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@AttraktorRem
var ical = require('ical'),
twit = require('twit'),
Twit = new twit({
'consumer_key': 'your_consumer_key_here',
'consumer_secret': 'your_consumer_secret_here',
'access_token': 'your_access_token_here',
'access_token_secret': 'your_access_token_secret_here'
}),
now = new Date();
function formTweet(ev, now, i, l) {
var tweet = '';
if (!now) {
//Enumerate the Events if there is more than one. e.g. (1/2)
if (l > 1) {
tweet += '(' + (i + 1) + '/' + l + ') ';
}
tweet += 'Um '
//time in format xx:xx
var time = ev.start.toTimeString();
time = time.replace(/^([0-9]{2}:[0-9]{2}).*$/ig, '$1');
tweet += time;
} else {
//event is starting right now.
tweet += 'Jetzt'
}
tweet += ':\n';
var desc = ev.description;
var diff = tweet.length + 25; //25 = ellipsis + newline + twitter link length(23)
if (diff + desc.length > 140) {
//shorten the description if it's too long for a tweet
desc = desc.length.substr(0, (140 - diff));
desc += '…';
}
tweet += desc + '\n';
tweet += ev.url;
return tweet;
}
function runAtDate(date, func) {
var now = (new Date()).getTime();
var then = date.getTime();
var diff = Math.max((then - now), 0);
if (diff > 0x7FFFFFFF) //setTimeout limit is MAX_INT32=(2^31-1)
setTimeout(function () {
runAtDate(date, func);
}, 0x7FFFFFFF);
else
setTimeout(func, diff);
}
ical.fromURL('http://wiki.attraktor.org/calendar.ics', {}, function (err, data) {
var events = [];
for (var k in data) {
if (data.hasOwnProperty(k)) {
var ev = data[k]
if (ev.start.getDate() === now.getDate() &&
ev.start.getMonth() === now.getMonth() &&
ev.start.getFullYear() === now.getFullYear()
) {
//event is today
events.push(ev);
}
}
}
events.sort(function (a, b) {
return (a.start.getTime() - b.start.getTime());
});
events.forEach(function (ev, i) {
//Tweet the event when the script is called
var tweet = formTweet(ev, false, i, events.length);
Twit.post('statuses/update', {
status: tweet
}, function (err) {
if (err) {
console.log(err)
}
});
//Tweet the event again when it's starting.
runAtDate(ev.start, function () {
var tweet = formTweet(ev, true);
Twit.post('statuses/update', {
status: tweet
}, function (err) {
if (err) {
console.log(err)
}
});
});
});
});
@remmythical

This comment has been minimized.

Copy link
Owner Author

@remmythical remmythical commented Sep 26, 2014

The script that's running @AttraktorRem.
Executed via cron daily at 0:00.
Twitter API keys not included :)

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