Created
March 11, 2009 12:26
-
-
Save rapha/77444 to your computer and use it in GitHub Desktop.
Twitter reader in Rhino Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependencies: rhino, growl, growlnotify, a twitter account (put your login info into the config object) | |
importClass(java.net.Authenticator) | |
importClass(java.net.PasswordAuthentication) | |
importClass(java.lang.Thread) | |
load('http://json.org/json2.js') | |
var config = { | |
email: 'my_username', | |
password: 'my_password', | |
period: '90' // in seconds | |
} | |
function fetchTweets(sinceId) { | |
var json = readUrl('http://twitter.com/statuses/friends_timeline.json?since_id='+sinceId) | |
return JSON.parse(json) | |
} | |
function notify(title, message) { | |
runCommand('growlnotify', title, ('-m '+message) ) | |
} | |
// setup for HTTP authentication | |
Authenticator.setDefault(new Authenticator({ | |
'getPasswordAuthentication': function() { | |
return new PasswordAuthentication(config.email, config.password.split('')) | |
} | |
})); | |
var lastTweetId = 1 | |
while (true) { | |
try { | |
var tweets = fetchTweets(lastTweetId) | |
if (tweets.length) { | |
print(tweets.length + ' new tweets') | |
lastTweetId = parseInt(tweets[0].id) | |
tweets.reverse().forEach(function(tweet) { | |
notify(tweet.user.name, tweet.text) | |
}) | |
} | |
} catch (e) { | |
print(e) | |
notify("TweetReader error", "Couldn't read from Twitter") | |
} | |
Thread.currentThread().sleep(1000 * config.period) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thread.currentThread().sleep(1000 * config.period)
should just be written asThread.sleep(1000 * config.period)
since sleep() is a static method