Created
May 17, 2014 01:28
-
-
Save AlexArchive/a7f404fae133bd6fb17e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class ProfileService | |
{ | |
private static readonly Dictionary<string, int> _starValues | |
= new Dictionary<string, int> { | |
{"Star Empty", 0}, | |
{"Star Quarter", 25}, | |
{"Star Half", 50}, | |
{"Star ThreeQuarter", 75}, | |
{"Star Full", 100} | |
}; | |
public async Task<Profile> ResolveProfileAsync(string region, string gamertag) | |
{ | |
var document = await DownloadDocumentAsync("http://live.xbox.com/" + region + "/Profile?gamertag=" + gamertag); | |
var documentNode = document.DocumentNode; | |
var profile = new Profile { Gamertag = gamertag }; | |
profile.Gamerscore = documentNode.SelectSingleNode("//div[@class='gamerscore']").InnerText; | |
var badgeNode = documentNode.SelectSingleNode("//div[@class='goldBadge']"); | |
profile.Tier = badgeNode == null ? "Silver" : "Gold"; | |
var starNodes = documentNode.SelectNodes("//div[starts-with(@class, 'Star')]"); | |
foreach (var star in starNodes) | |
{ | |
var kind = star.GetAttributeValue("class", null); | |
profile.Reputation += _starValues[kind]; | |
} | |
profile.Presence = documentNode.SelectSingleNode("//div[@class='presence']").InnerText; | |
profile.Online = profile.Presence.StartsWith("Online") && profile.Presence != "Online Status Unavailable"; | |
var mottoNode = documentNode.SelectSingleNode("//div[@class='motto']"); | |
profile.Motto = mottoNode != null ? mottoNode.InnerText.Trim() : "None"; | |
profile.Name = documentNode.SelectSingleNode("//div[@class='name']/div").InnerText; | |
profile.Location = documentNode.SelectSingleNode("//div[@class='location']/div").InnerText; | |
profile.Biography = documentNode.SelectSingleNode("//div[@class='bio']/div").InnerText.Trim(); | |
profile.Avatar = new Avatar(); | |
profile.Avatar.Full = string.Format("http://avatar.xboxlive.com/avatar/{0}/avatar-body.png", gamertag); | |
profile.Avatar.Small = string.Format("http://avatar.xboxlive.com/avatar/{0}/avatarpic-s.png", gamertag); | |
profile.Avatar.Large = string.Format("http://avatar.xboxlive.com/avatar/{0}/avatarpic-l.png", gamertag); | |
profile.Avatar.Tile = documentNode.SelectSingleNode("//img[@class='gamerpic']").GetAttributeValue("src", null); | |
profile.Avatar.Tile = profile.Avatar.Tile.Replace("https://avatar-ssl", "http://avatar"); | |
return profile; | |
} | |
private static async Task<HtmlDocument> DownloadDocumentAsync(string address) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
var response = await client.GetAsync(address, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); | |
response.EnsureSuccessStatusCode(); | |
var pageData = await response.Content.ReadAsStringAsync(); | |
var document = new HtmlDocument(); | |
document.LoadHtml(pageData); | |
return document; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment