Permalink
Browse files

now search MLB team news

  • Loading branch information...
1 parent eb0289f commit a2541abb72d69d0a0ff35ecc3cc14e89b5f5f61e @mjw56 committed Aug 28, 2012
Showing with 19 additions and 5 deletions.
  1. +19 −5 src/scripts/espn.coffee
@@ -1,5 +1,5 @@
# Description:
-# Grab a headline from ESPN
+# Grab a headline from ESPN through querying hubot
#
# Dependencies:
# None
@@ -8,7 +8,8 @@
# HUBOT_ESPN_ACCOUNT_KEY
#
# Commands:
-# hubot espn - Displays a random headline from ESPN.com
+# hubot espn headline - Displays a random headline from ESPN.com
+# hubot espn mlb <name of team> - Displays ESPN.com team homepage
#
# Author:
# mjw56
@@ -18,7 +19,7 @@ unless espnApiKey
throw "You must enter your HUBOT_ESPN_ACCOUNT_KEY in your environment variables"
module.exports = (robot) ->
- robot.respond /espn/i, (msg) ->
+ robot.respond /espn headline/i, (msg) ->
search = escape(msg.match[2])
msg.http('http://api.espn.com/v1/sports/news/headlines?apikey=' + espnApiKey)
.get() (err, res, body) ->
@@ -27,10 +28,23 @@ module.exports = (robot) ->
if result.headlines.count <= 0
msg.send "Couldn't find any headlines"
return
-
+
urls = [ ]
for child in result.headlines
urls.push(child.headline + "- " + child.links.web.href)
-
+
rnd = Math.floor(Math.random()*urls.length)
msg.send urls[rnd]
+
+ robot.respond /(espn)( mlb)? (.*)/i, (msg) ->
+ msg.http('http://api.espn.com/v1/sports/baseball/mlb/teams?apikey=' + espnApiKey)
+ .get() (err, res, body) ->
+ result = JSON.parse(body)
+
+ for child in result.sports[0].leagues[0].teams
+ team = child.name.toLowerCase()
+ city = child.location.toLowerCase()
+ input = msg.match[3].toLowerCase()
+
+ if team is input || city is input
+ msg.send 'Team news for the '+ child.location + ' ' + child.name + '- ' + child.links.web.teams.href

0 comments on commit a2541ab

Please sign in to comment.