Permalink
Browse files

updated script to use env var HUBOT_KLOUT_API_KEY, and made competito…

…rs list dynamic
  • Loading branch information...
1 parent 8472a39 commit 755c0c8ced4449dc190814e332dd3f4e0a30bf0e lkang committed Sep 7, 2012
Showing with 35 additions and 63 deletions.
  1. +35 −63 src/scripts/klout.coffee
View
@@ -1,73 +1,42 @@
# Description
-# Reports Klout score for a twitter handle. If that person is part of the competitors
-# list, the players whose score is greater will be reported. Default competitors are @attachmentsme
+# Reports Klout score for a twitter handle. The players in the competitors list
+# whose score is greater will be reported. The last 9 unique requested names
+# are kept in the competitors list. More than that will trigger the klout api's
+# per second rate limit.
#
# Dependencies:
-# You'll have to put in your own klout api key. Signup for one at http://klout.com/s/developers/home
+# A klout api key is needed. Signup for one at http://klout.com/s/developers/home
#
# Configuration:
-# The competitors list's name and id parameters can be modified to include 10 members.
-# More than 10 will potentially cause trigger the per second rate limit with klout.
-# <name> is a twitter name
-# <id> is a klout id, not a twitter id. Use the interactive console to get this. http://developer.klout.com/io-docs
+# HUBOT_KLOUT_API_KEY must be set in the environment.
+# The competitors list is automatically created and includes the last 9 valid twitter handles.
#
# Commands:
-# klout <twitter name without @>
+# hubot klout <twitter name with or without @> - report klout score for twitter handle, and compare to competitors
#
# Author:
# l_kang
-klout_api_key = "your_klout_api_key"
+competitors = []
+klout_api_key = () ->
+ return process.env.HUBOT_KLOUT_API_KEY
+
module.exports = (robot) ->
robot.respond /.*(klout) [@]?(.+)$/i, (msg) ->
- if klout_api_key == "your_klout_api_key"
- msg.send "I wont report any scores until you get your own Klout api key"
+ unless klout_api_key()
+ msg.send "I wont report any scores until your environment HUBOT_KLOUT_API_KEY is set"
return
-
name = msg.match[2]
- done = checkList name, msg, (result)->
- msg.send result
- return if done
-
- getKloutScoreByName name, msg, (p)->
- if p.id == 0
- msg.send "Sorry, I cant figure out who @#{name} is"
- else
- msg.send "Klout for @#{p.name} is #{p.score}"
-
-competitors = [{
- "name": "l_kang"
- "id": "1065822"
- "score": 0
-},
-{
- "name": "danreedx86"
- "id": "37154701691656080"
- "score": 0
-},
-{
- "name": "benjamincoe"
- "id": "85568398087782958"
- "score": 0
-},
-{
- "name": "jesse_miller"
- "id": "529139"
- "score": 0
-},
-{
- "name": "joshkowaleski"
- "id": "524476"
- "score": 0
-},
-{
- "name": "rissem"
- "id": "41376826341708013"
- "score": 0
-}]
+ player = p for p in competitors when p.name == name
+ if player == undefined
+ getKloutScoreByName name, msg, (result)->
+ msg.send result
+ else
+ checkList name, msg, (result)->
+ msg.send result
checkList = (pname, msg, callback)->
player = p for p in competitors when p.name == pname
@@ -80,39 +49,42 @@ checkList = (pname, msg, callback)->
else if leaderNames.length == 1
tag = "#{leaderNames.join(',')} is ahead though."
else
- tag = "#{leaderNames.join(',')} are ahead though."
+ tag = "#{leaderNames.join(',')} are ahead though. "
callback "@#{player.name}'s Klout is #{player.score}. #{tag}"
- true
getKloutScores = ( hashlist, index, msg, doneCallback )->
return if hashlist.length == index
thisPlayer = hashlist[index]
- url = "http://api.klout.com/v2/user.json/#{thisPlayer.id}/score?key=#{klout_api_key}"
+ url = "http://api.klout.com/v2/user.json/#{thisPlayer.id}/score?key=#{klout_api_key()}"
msg.http(url)
.get() (error, response, body)->
scoreRecord = JSON.parse( body )
- # console.log "scoreRecord: "
- # console.log scoreRecord
thisPlayer.score = scoreRecord.score
if hashlist.length == index+1
doneCallback( thisPlayer )
else
getKloutScores( hashlist, index+1, msg, doneCallback )
getKloutScoreByName = (name, msg, callback)->
- url = "http://api.klout.com/v2/identity.json/twitter?key=#{klout_api_key}&screenName=#{name}"
+ url = "http://api.klout.com/v2/identity.json/twitter?key=#{klout_api_key()}&screenName=#{name}"
msg.http(url)
.get() (error, response, body)->
- player = [{
+ player = {
name: name
id: 0
score: 0
- }]
+ }
if error || response.statusCode != 200 || body == undefined
- callback( player[0] )
+ callback( "Sorry, I cant figure out who @#{name} is" )
return
kloutIdRec = JSON.parse( body )
player.id = kloutIdRec.id
- getKloutScores player, 0, msg, callback
-
+ # remove player if he exists, add player to the end and trim size.
+ nc = competitors[..]
+ competitors = []
+ competitors.push(p) for p in nc when p.name != name
+ competitors.push(player)
+ competitors.shift() if competitors.length > 9
+ checkList name, msg, callback
+

0 comments on commit 755c0c8

Please sign in to comment.