Reputation: 1317
I've never written coffeescript and have been trying to brute force my way through this for hours.
It's supposed to go grab JSON results for live scoring for myfantasyleague.com, but I can't seem to get the loops right.
The first loop gets the live scores, but the data returned only has a team id number, so I have to go match that id to another JSON query. I've tried about 100 permutations of where to put the i++, a break, a continue, a return, etc. but I'm just not getting it.
Here's what I have:
# Description:
# Get MyFantasyLeague data
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_MFL_LEAGUE_ID - set the league ID
# HUBOT_MFL_LEAGUE_URL - set the league URL
# HUBOT_MFL_LEAGUE_DATA_URL - Used to get the name to team id mapping
#
# Commands:
# hubot: mfl scores
#
#
# Author:
# Clayton Dukes <[email protected]>
leagueId = process.env.HUBOT_MFL_LEAGUE_ID
unless leagueId
exit "You must enter your HUBOT_MFL_LEAGUE_ID in your environment variables"
leagueURL = process.env.HUBOT_MFL_LEAGUE_URL
unless leagueURL
exit "You must enter your HUBOT_MFL_LEAGUE_URL in your environment variables"
teamData = process.env.HUBOT_MFL_LEAGUE_DATA_URL
unless teamData
exit "You must enter your HUBOT_MFL_LEAGUE_DATA_URL in your environment variables"
module.exports = (robot) ->
robot.logger.debug "Using League URL: " + leagueURL
robot.logger.debug "Using Team Data from " + leagueURL
robot.respond /sc/i, (msg) ->
response = "Live Scoring Results\n"
robot.http(leagueURL)
.header('Accept', 'application/json')
.get() (err, res, body) ->
if err
return msg.send "Encountered an error :( #{err}"
content = JSON.parse(body)
matchups = content.liveScoring.matchup
for game in matchups
i=0
#robot.logger.debug game['franchise']
for team in game['franchise']
id = team['id']
playersCurrentlyPlaying = team['playersCurrentlyPlaying']
gameSecondsRemaining = team['gameSecondsRemaining']
score = team['score']
robot.http(teamData)
.header('Accept', 'application/json')
.get() (err, res, body) ->
if err
return msg.send "Encountered an error :( #{err}"
data = JSON.parse(body)
teams = data.league.franchises
count = teams['franchise'].length
#robot.logger.debug "Found #{count} records"
while i<=count
name = teams['franchise'][i]['name']
teamId = teams['franchise'][i]['id']
if id is teamId
msg.send "ID = #{id}\n"
msg.send "TID = #{teamId}\n"
response += "Name: #{name}\n"
response += "Currently Playing: #{playersCurrentlyPlaying}\n"
response += "Game Seconds Remaining: #{gameSecondsRemaining}\n"
response += "Score: #{score}\n"
msg.send response
break
i++
Right now, this returns the same thing over and over until it bails:
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: ID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: TID = 0005
[Tue Sep 15 2015 21:09:08 GMT-0400 (EDT)] DEBUG Sending to cdukes: Live Scoring Results
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
Name: Blah
Currently Playing: 0
Game Seconds Remaining: 0
Score: 78
[Tue Sep 15 2015 21:09:09 GMT-0400 (EDT)] ERROR Received error {"code":-1,"msg":"slow down, too many messages..."}
[Tue Sep 15 2015 21:09:09 GMT-0400 (EDT)] ERROR undefined
[Tue Sep 15 2015 21:09:09 GMT-0400 (EDT)] ERROR Exiting in 1 second
Here are the two JSON data queries that get returned:
{
"liveScoring":{
"matchup":[
{
"franchise":[
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"0",
"players":{
},
"playersYetToPlay":"0",
"score":"129",
"id":"0001"
},
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"1",
"players":{
},
"playersYetToPlay":"0",
"score":"99",
"id":"0008"
}
]
},
{
"franchise":[
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"0",
"players":{
},
"playersYetToPlay":"0",
"score":"105",
"id":"0003"
},
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"1",
"players":{
},
"playersYetToPlay":"0",
"score":"98",
"id":"0007"
}
]
},
{
"franchise":[
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"0",
"players":{
},
"playersYetToPlay":"0",
"score":"125",
"id":"0002"
},
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"1",
"players":{
},
"playersYetToPlay":"0",
"score":"101",
"id":"0006"
}
]
},
{
"franchise":[
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"0",
"players":{
},
"playersYetToPlay":"0",
"score":"96",
"id":"0004"
},
{
"playersCurrentlyPlaying":"0",
"gameSecondsRemaining":"0",
"isHome":"1",
"players":{
},
"playersYetToPlay":"0",
"score":"78",
"id":"0005"
}
]
}
],
"week":"1"
},
"version":"1.0",
"encoding":"ISO-8859-1"
}
Next one is the query to get the team name from the given id in the first loop - I changed the team names, etc. below to protect the guilty :)
{
"version":"1.0",
"league":{
"currentWaiverType":"REVERSE",
"playerLimitUnit":"LEAGUE",
"taxiSquad":"0",
"survivorPool":"Yes",
"lastRegularSeasonWeek":"14",
"endWeek":"17",
"lockout":"No",
"minKeepers":"2",
"tiebreakerPosition":"*",
"injuredReserve":"0",
"franchises":{
"count":"8",
"franchise":[
{
"icon":"",
"abbrev":"CLAY",
"division":"00",
"name":"Team 1",
"waiverSortOrder":"8",
"iscommish":"1",
"logo":"",
"id":"0001"
},
{
"division":"01",
"name":"Team 2",
"id":"0002",
"waiverSortOrder":"7"
},
{
"logo":"",
"division":"00",
"name":"Team 3",
"id":"0003",
"waiverSortOrder":"6"
},
{
"icon":"",
"division":"01",
"name":"Team 4",
"id":"0004",
"waiverSortOrder":"5"
},
{
"division":"01",
"name":"Team 5",
"id":"0005",
"waiverSortOrder":"1"
},
{
"division":"01",
"name":"Team 6",
"id":"0006",
"waiverSortOrder":"4"
},
{
"logo":"",
"icon":"",
"abbrev":"BCB",
"division":"00",
"name":"Team 7",
"id":"0007",
"waiverSortOrder":"2"
},
{
"icon":"",
"division":"00",
"name":"Team 8",
"id":"0008",
"waiverSortOrder":"3"
}
]
},
"standingsSort":"PCT,PTS",
"draftPlayerPool":"Both",
"id":"36554",
"startWeek":"1",
"survivorPoolStartWeek":"1",
"survivorPoolEndWeek":"17",
"history":{
"league":[
{
"url":"http://football33.myfantasyleague.com/2015/home/36554",
"year":"2015"
},
{
"url":"http://football.myfantasyleague.com/2013/home/54662",
"year":"2013"
},
{
"url":"http://www.myfantasyleague.com/2014/home/12321",
"year":"2014"
}
]
},
"rosterSize":"16",
"name":"Some Name",
"rostersPerPlayer":"1",
"tiebreakerCount":"1",
"h2h":"YES",
"tiebreaker":"nonstarter",
"draftLimitHours":"1:00",
"maxKeepers":"2",
"divisions":{
"count":"2",
"division":[
{
"name":"East",
"id":"00"
},
{
"name":"West",
"id":"01"
}
]
},
"starters":{
"count":"9",
"position":[
{
"name":"QB",
"limit":"1"
},
{
"name":"RB",
"limit":"2-3"
},
{
"name":"WR",
"limit":"2-3"
},
{
"name":"TE",
"limit":"1"
},
{
"name":"PK",
"limit":"1"
},
{
"name":"Def",
"limit":"1"
}
],
"idp_starters":""
},
"baseURL":"http://football33.myfantasyleague.com",
"precision":"0",
"loadRosters":"email_draft"
},
"encoding":"ISO-8859-1"
}
Upvotes: 2
Views: 683
Reputation: 1784
I think you'll be better off by decomposing a little bit and then using list comprehensions, etc... You can also use promises to cleanup the code and only load the data once instead of in a loop.
Something along these lines...
Promise = require('promise')
# configuration block (left out here)
jsonGet = (robot, url) ->
new Promise (resolve, reject) ->
robot.http(url)
.header('Accept', 'application/json')
.get() (err, res, body) ->
if err
robot.logger.error e
reject err
try
resolve(JSON.parse(body));
catch e
robot.logger.error e
reject e
teamName = (leagueData, team) ->
return x.name for x in leagueData.league.franchises.franchise when x.id is team.id
sendLiveScoringResults = (msg, leagueData, liveScoringData) ->
response = 'Live Scoring Results'
for game in liveScoringData.liveScoring.matchup
for team in game.franchise
theTeamName = teamName(leagueData, team)
response += "\nName: #{theTeamName} Score: #{team.score}"
... (do whatever here)
msg.send response
onFantasyScores = (robot, msg) ->
jsonGet(robot, OWNER_INFORMATION)
.then (leagueData) ->
return { leagueData: leagueData }
.then (data) ->
jsonGet(robot, LIVE_SCORING_URL).then (liveScoringData) ->
data.liveScoringData = liveScoringData
data
.then (data) ->
sendLiveScoringResults msg, data.leagueData, data.liveScoringData
.then null, (err) -> msg.send "An error occurred : #{err}"
module.exports = (robot) ->
robot.respond /fantasyscores/i, (msg) -> onFantasyScores(robot, msg)
Upvotes: 1
Reputation: 345
you should be able to do something like this
instead of looping through the JSON
teams = data.league.franchises
theTeam = teams.franchises.filter(teamId)
that should give you an object with only the team data you want.
Upvotes: 0