This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
vanity.coffee
63 lines (57 loc) · 1.96 KB
/
vanity.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Description:
# 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.
#
# Dependencies:
# "sprintf": "0.1.1"
#
# Configuration:
# HUBOT_VANITY_TWITTER_ACCOUNT
#
# Commands:
# hubot vanity me - list peeps ordered by twitter followers
#
# Author:
# maddox
Path = require "path"
sprintf = require("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"