【发布时间】:2015-12-12 10:19:14
【问题描述】:
我从来没有写过咖啡脚本,我一直试图用蛮力解决这个问题好几个小时。
它应该为 myfantasyleague.com 获取实时评分的 JSON 结果,但我似乎无法正确循环。
第一个循环获取实时比分,但返回的数据只有一个团队 ID 号,所以我必须将该 ID 与另一个 JSON 查询匹配。我已经尝试了大约 100 种排列 i++、中断、继续、返回等的排列,但我就是不明白。
这是我所拥有的:
# 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 <cdffl@remailed.ws>
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++
现在,这会一遍又一遍地返回相同的东西,直到它退出:
[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
以下是返回的两个 JSON 数据查询:
{
"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"
}
下一个是在第一个循环中从给定 id 获取团队名称的查询 - 我更改了下面的团队名称等以保护有罪:)
{
"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"
}
【问题讨论】:
标签: javascript json coffeescript