private void NextBarIconButton_Click(object sender, EventArgs e) { NewsPage currentContext = (NewsPage)((PivotItem)MainPivot.SelectedItem).DataContext; nextPrevUrlStacks[MainPivot.SelectedIndex].Push(currentContext.NextURLPostfix); FetchNews(MainPivot.SelectedIndex); UpdateAppBarLabels(); }
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { LoadingProgressBar.IsVisible = false; if (e.Error != null && !e.Cancelled) { FlurryWP7SDK.Api.LogError("Error in client_DownloadStringCompleted when fetching news on Main Page", e.Error); // Display the connection error text in the SystemTray for 2s ShowConnectionError(true); PerformActionWithDelay(() => ShowConnectionError(false), 2000); } else if (!e.Cancelled) { // Parse the news results on a background thread, and then update the UI BackgroundWorker updateNewsWorker = new BackgroundWorker(); updateNewsWorker.DoWork += delegate(object s, DoWorkEventArgs args) { args.Result = HackerNewsParser.ParseNewsPage(e.Result); }; updateNewsWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) { NewsPage newsPage = (NewsPage)args.Result; int pivotIndx = (int)e.UserState; PivotItem currentPivotItem = (PivotItem)MainPivot.Items[pivotIndx]; ListBox currentListBox = (ListBox)currentPivotItem.Content; lastRefreshed[pivotIndx] = DateTime.Now; currentPivotItem.DataContext = newsPage; currentListBox.ItemsSource = null; currentListBox.ItemsSource = newsPage.Items; currentListBox.UpdateLayout(); currentListBox.ScrollIntoView(currentListBox.Items[0]); UpdateAppBarLabels(); }; updateNewsWorker.RunWorkerAsync(); } }
public static NewsPage ParseNewsPage(string htmlString) { HtmlDocument document = new HtmlDocument(); document.LoadHtml(htmlString); // LINQ Query to obtain the news story title and URL on the front page var titlesOnPage = from node in document.DocumentNode.Descendants() where node.Name == "td" && node.Attributes["class"] != null && node.Attributes["class"].Value == "title" && node.FirstChild.Name == "a" select node.FirstChild; NewsPage newsPage = new NewsPage(); List <NewsItem> newsItemList = new List <NewsItem>(); foreach (HtmlNode n in titlesOnPage) { NewsItem i = new NewsItem(); try { i.Title = HttpUtility.HtmlDecode(n.InnerText.Trim()); i.Url = n.Attributes["href"].Value; i.IsLocal = i.Url.StartsWith("item?"); if (i.Title == "More") { newsPage.NextURLPostfix = i.Url; if (!newsPage.NextURLPostfix.StartsWith("/")) { newsPage.NextURLPostfix = "/" + newsPage.NextURLPostfix; } break; } } catch (Exception exception) { FlurryWP7SDK.Api.LogError("Exception parsing news story title/url. Title = " + i.Title + "; URL = " + i.Url, exception); } newsItemList.Add(i); } // LINQ Query to obtain the Id, CommentCount, Points, PostedAgo, and PostedBy for each // story on the front page var storySubtextsOnPage = from node in document.DocumentNode.Descendants() where node.Name == "td" && node.Attributes["class"] != null && node.Attributes["class"].Value == "subtext" select node; int cntr = 0; foreach (HtmlNode n in storySubtextsOnPage) { NewsItem i = newsItemList[cntr]; try { if (n.ChildNodes.Count == 1) { // This is a job ad - all that is displayed is how long ago posted i.Points = 0; i.PostedAgo = n.ChildNodes[0].InnerText; i.PostedBy = string.Empty; i.CommentCount = 0; i.Id = -1; i.IsJobAd = true; } else { string pointsStr = n.ChildNodes[0].InnerText.Trim(); i.Points = int.Parse(pointsStr.Split()[0]); i.PostedBy = n.ChildNodes[2].InnerText; i.PostedAgo = n.ChildNodes[3].InnerText.Trim(new char[] { ' ', '|' }); string commentsStr = n.ChildNodes[4].InnerText.Trim(); if (commentsStr == "discuss") { i.CommentCount = 0; } else { i.CommentCount = int.Parse(commentsStr.Split()[0]); } string urlStr = n.ChildNodes[4].Attributes["href"].Value; i.Id = int.Parse(urlStr.Substring(urlStr.IndexOf("=") + 1)); } } catch (Exception exception) { FlurryWP7SDK.Api.LogError("Exception parsing news story subtext. Title = " + i.Title + "; URL = " + i.Url, exception); } cntr++; } newsPage.Items = newsItemList; return(newsPage); }