private void cmdGetData_Click(object sender, EventArgs e) { ListViewItem lsvItem; pProfile = XboxLeadersAPI.DownloadProfileData(txtGamerTag.Text); gGames = XboxLeadersAPI.DownloadGameData(txtGamerTag.Text); fFriends = XboxLeadersAPI.DownloadFriendList(txtGamerTag.Text); pbAvatar.Image = pProfile.Avatar; lblGamerTag.Text = "GamerTag: " + pProfile.GamerTag; lblScore.Text = "Gamer Score: " + pProfile.GamerScore.ToString(); lblRep.Text = "Reputation: " + pProfile.Reputation.ToString(); lblPresence.Text = "Presence: " + pProfile.Presence; lblOnline.Text = "Online: " + pProfile.Online.ToString(); lblLocation.Text = "Location: " + pProfile.Location; lblBiography.Text = "Biography: " + pProfile.Biography; foreach (Activity aActivity in pProfile.RecentActivity) { lsvItem = new ListViewItem(aActivity.Title); lsvItem.SubItems.Add(aActivity.LastPlayed.ToLocalTime().ToString()); lsvActivity.Items.Add(lsvItem); } lblAchievements.Text = String.Format("Achievements: {0}/{1}", gGames.Achievements.Current, gGames.Achievements.Total); lblTotalGames.Text = "Games Played: " + gGames.TotalGames.ToString(); lblProgress.Text = String.Format("Progress: {0}%", gGames.Progress); foreach (Game gGame in gGames.PlayedGames) { lsvItem = new ListViewItem(gGame.Title); lsvItem.SubItems.Add(String.Format("{0}/{1}", gGame.GamerScore.Current, gGame.GamerScore.Total)); lsvItem.SubItems.Add(String.Format("{0}/{1}", gGame.Achievements.Current, gGame.Achievements.Total)); lsvItem.SubItems.Add(String.Format("{0}%", gGame.Progress)); lsvItem.SubItems.Add(gGame.LastPlayed.ToLocalTime().ToString()); lsvGames.Items.Add(lsvItem); } foreach (Friend fFriend in fFriends.GamerFriends) { lsvItem = new ListViewItem(fFriend.GamerTag); if (fFriend.Online) { lsvItem.Group = lsvFriends.Groups[0]; } else { lsvItem.Group = lsvFriends.Groups[1]; } lsvItem.SubItems.Add(fFriend.Status); lsvItem.SubItems.Add(fFriend.LastSeen.ToLocalTime().ToString()); lsvFriends.Items.Add(lsvItem); } }
/// <summary> /// Downloads the profile data for a the specified gamer tag. /// </summary> /// <param name="sGamerTag">The gamer tag to download profile data for.</param> /// <returns>Returns a <seealso cref="XboxLeadersNETWrapper.Profile">Profile</seealso> object.</returns> /// <example>XboxLeadersNETWrapper.Profile pProfile = XboxLeadersNETWrapper.DownloadProfileData("Major Nelson");</example> public static Profile DownloadProfileData(String sGamerTag) { WebClient wcDownloader = new WebClient(); Profile pDownloadedProfile = new Profile(); XmlDocument xProfile = new XmlDocument(); XmlReader xReader; XElement xJSONRoot; Byte[] bJSONData; bJSONData = System.Text.Encoding.ASCII.GetBytes(wcDownloader.DownloadString("https://www.xboxleaders.com/api/profile.json?gamertag=" + sGamerTag)); xReader = JsonReaderWriterFactory.CreateJsonReader(bJSONData, new XmlDictionaryReaderQuotas()); if (xReader == null) { throw new Exception("Unable to convert data."); } xJSONRoot = XElement.Load(xReader); xProfile.LoadXml(xJSONRoot.ToString()); if (xProfile.SelectSingleNode("root/status").InnerText.ToLower() != "success") { //TODO: Catch errors in JSON feed (Problem: If JSON returns a 501 the WebClient throws an exception.) throw new Exception(""); } pDownloadedProfile.GamerTag = xProfile.SelectSingleNode("root/data/gamertag").InnerText; pDownloadedProfile.Avatar = DownloadImage(xProfile.SelectSingleNode("root/data/avatar/full").InnerText); pDownloadedProfile.GamerScore = Int32.Parse(xProfile.SelectSingleNode("root/data/gamerscore").InnerText); pDownloadedProfile.Reputation = Int32.Parse(xProfile.SelectSingleNode("root/data/reputation").InnerText); pDownloadedProfile.Presence = xProfile.SelectSingleNode("root/data/presence").InnerText; pDownloadedProfile.Online = Boolean.Parse(xProfile.SelectSingleNode("root/data/online").InnerText); pDownloadedProfile.Motto = xProfile.SelectSingleNode("root/data/motto").InnerText; pDownloadedProfile.Name = xProfile.SelectSingleNode("root/data/name").InnerText; pDownloadedProfile.Location = xProfile.SelectSingleNode("root/data/location").InnerText; pDownloadedProfile.Biography = xProfile.SelectSingleNode("root/data/biography").InnerText; pDownloadedProfile.RecentActivity = new List<Activity>(); foreach (XmlNode xNode in xProfile.SelectNodes("root/data/recentactivity/item")) { Activity tmpActivity = new Activity(); tmpActivity.ID = Int32.Parse(xNode.SelectSingleNode("id").InnerText); tmpActivity.HID = xNode.SelectSingleNode("hid").InnerText; tmpActivity.IsApp = Boolean.Parse(xNode.SelectSingleNode("isapp").InnerText); tmpActivity.Title = HttpUtility.HtmlDecode(xNode.SelectSingleNode("title").InnerText); tmpActivity.Artwork = DownloadImage(xNode.SelectSingleNode("artwork").SelectSingleNode("large").InnerText); tmpActivity.GamerScore = new GamerScore(Int32.Parse(xNode.SelectSingleNode("gamerscore/current").InnerText), Int32.Parse(xNode.SelectSingleNode("gamerscore/total").InnerText)); tmpActivity.Achievements = new Achievements(Int32.Parse(xNode.SelectSingleNode("achievements/current").InnerText), Int32.Parse(xNode.SelectSingleNode("achievements/total").InnerText)); tmpActivity.Progress = Double.Parse(xNode.SelectSingleNode("progress").InnerText); tmpActivity.LastPlayed = DateTimeFromEpoc(Int64.Parse(xNode.SelectSingleNode("lastplayed").InnerText)); pDownloadedProfile.RecentActivity.Add(tmpActivity); } return pDownloadedProfile; }