Skip to content

Instantly share code, notes, and snippets.

@rapha
Created March 11, 2009 12:26
Show Gist options
  • Save rapha/77444 to your computer and use it in GitHub Desktop.
Save rapha/77444 to your computer and use it in GitHub Desktop.
Twitter reader in Rhino Javascript
// 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)
}
@newacct
Copy link

newacct commented May 6, 2011

Thread.currentThread().sleep(1000 * config.period) should just be written as Thread.sleep(1000 * config.period) since sleep() is a static method

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