Created
June 13, 2014 04:46
-
-
Save PeteGoo/d88833b43cbe9e16eb3f to your computer and use it in GitHub Desktop.
mmbot world cup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var robot = Require<Robot>(); | |
robot.Respond(@"show all the scores", msg => { | |
msg.Http("http://live.mobileapp.fifa.com/api/wc/matches").GetJson((err, res, body) => { | |
var groups = GetMatches(msg).Where(m => m["b_Started"].Value<bool>()).GroupBy(m => m["c_Phase_en"].ToString()); | |
if(!groups.Any()){ | |
return; | |
} | |
var sb = new StringBuilder(); | |
foreach(var group in groups){ | |
sb.AppendLine(group.Key); | |
foreach(var match in group){ | |
sb.AppendLine(" " + GetScore(msg, match)); | |
} | |
sb.AppendLine(); | |
} | |
msg.Send(sb.ToString().Trim()); | |
}); | |
}); | |
public IEnumerable<JToken> GetMatches(MMBot.IResponse<TextMessage> msg) { | |
var result = msg.Http("http://live.mobileapp.fifa.com/api/wc/matches").GetJson().Result; | |
return ((JToken)result)["data"]["group"]; | |
} | |
public string GetScore(MMBot.IResponse<TextMessage> msg, JToken match){ | |
return string.Format("{0} {1}:{2} {3} - {4} vs {5} ({6})", | |
match["c_HomeNatioShort"].ToString(), | |
match["n_HomeGoals"].ToString(), | |
match["n_AwayGoals"].ToString(), | |
match["c_AwayNatioShort"].ToString(), | |
match["c_HomeTeam_en"].ToString(), | |
match["c_AwayTeam_en"].ToString(), | |
match["b_Finished"].Value<bool>() ? "full time" : match["c_Minute"].ToString() + " minutes" | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment