Permalink
Browse files

vanity me

  • Loading branch information...
1 parent 57a0a87 commit 33f8c68cf5c15b69348995aa2acb5bfddbf8d8a8 @maddox maddox committed Oct 26, 2011
Showing with 58 additions and 0 deletions.
  1. +58 −0 src/scripts/vanity.coffee
View
@@ -0,0 +1,58 @@
+# Race to the bottom.
+#
+# Battle it out with your mates to see who is the
+# most important/coolest/sexiest/funniest/smartest of them all solely
+# based on the clearly scientific number of twitter followers.
+#
+# Vanity will check all the users that a specific twitter account, like say maybe
+# your company's twitter account, follows and display them in order by followers.
+#
+# Be sure to set the env var.
+#
+# HUBOT_VANITY_TWITTER_ACCOUNT
+#
+# vanity me - list peeps ordered by twitter followers
+#
+
+Path = require "path"
+sprintf = require(Path.resolve(Path.join(__dirname, "..", "lib", "sprintf"))).sprintf
+
+countFollowers = (msg, ids, cb) ->
+ counts = []
+
+ ids.forEach (id) ->
+ console.log id
+ msg.http("http://api.twitter.com/1/users/show.json?user_id=#{id}")
+ .get() (err, res, body) ->
+ user = JSON.parse body
+
+ keptUser =
+ followers: user.followers_count
+ screen_name: user.screen_name
+
+ console.log keptUser
+ counts.push keptUser
+ if counts.length == ids.length
+ last = 0
+ response = ""
+ counts.sort (x, y) ->
+ y.followers - x.followers
+ counts.forEach (user) ->
+ if last > 0
+ diff = last - user.followers
+ response += sprintf("%15s : %5d ( %4d to go)\n", user.screen_name, user.followers, diff)
+ else
+ response += sprintf("%15s : %5d\n", user.screen_name, user.followers)
+ last = user.followers
+ cb response
+
+module.exports = (robot) ->
+ robot.respond /vanity me$/i, (msg) ->
+ msg.http("http://api.twitter.com/1/friends/ids.json?screen_name=" + process.env.HUBOT_VANITY_TWITTER_ACCOUNT)
+ .get() (err, res, body) ->
+ if res.statusCode == 200
+ countFollowers msg, JSON.parse(body), (output) ->
+ msg.send output
+ else
+ msg.reply "Sorry, not right now. Twitter's returning a #{res.statusCode} error"
+

0 comments on commit 33f8c68

Please sign in to comment.