public NewsArticlePage()
 {
     this.InitializeComponent();
     Settings = IsolatedStorageSettings.ApplicationSettings;
     this.article = new Article()
                        {
                            Author = "",
                            Title = "",
                            Date = DateTime.Now,
                            Pictures = new List<string>(),
                            HtmlContent = "",
                            Url = ""
                        };
 }
        private void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Result == null)
            {
                if (Settings.Contains("article" + this.article.Title))
                {
                    // Get the article to display from local
                    this.article = (Article)Settings["article" + this.article.Title];
                    this.DisplayArticleData();
                }
                else
                {
                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                }
            }
            else
            {
                var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
                HtmlDocument doc = new HtmlDocument();
                doc.Load(stream);

                try
                {
                    if (doc.DocumentNode != null)
                    {
                        foreach (var htmlNode in doc.DocumentNode.Descendants().ToList())
                        {
                            // Find the title of the news
                            if (htmlNode.GetAttributeValue("class", "") == "entry-title")
                            {
                                this.article.Title = HttpUtility.HtmlDecode(htmlNode.InnerText);
                            }

                            // Find the published date of the news
                            if (htmlNode.GetAttributeValue("class", "") == "entry-time")
                            {
                                this.article.Date = DateTime.Parse(htmlNode.InnerText, new CultureInfo("fr-FR"));
                            }

                            // Find the author of the news
                            if (htmlNode.GetAttributeValue("class", "") == "entry-author")
                            {
                                this.article.Author = htmlNode.InnerText;
                            }

                            // Find the content of the news
                            if (htmlNode.GetAttributeValue("class", "") == "entry-content")
                            {
                                var divList = htmlNode.Descendants().Where(item => item.Name == "div").ToList();

                                foreach (
                                    var node in
                                        divList.Where(
                                            node =>
                                            node.Attributes["class"] != null
                                            && node.Attributes["class"].Value == "sharedaddy sd-sharing-enabled"))
                                {
                                    node.Remove();
                                }

                                this.article.HtmlContent = htmlNode.InnerHtml;

                                // Find the pictures
                                var y = htmlNode.Descendants().ToList();
                                this.article.Pictures = new List<string>();
                                foreach (var node in y)
                                {
                                    if (node.Attributes.Contains("src"))
                                    {
                                        bool pictureFind = false;
                                        if (node.Attributes.Contains("data-large-file"))
                                        {
                                            if (node.Attributes["data-large-file"].Value.Contains(".jpg")
                                                && node.Attributes["data-large-file"].Value.StartsWith(
                                                    "http://www.therondels.fr/"))
                                            {
                                                this.article.Pictures.Add(node.Attributes["data-large-file"].Value);
                                                pictureFind = true;
                                            }
                                        }
                                        else if (node.ParentNode.Attributes.Contains("href"))
                                        {
                                            if (node.ParentNode.Attributes["href"].Value.Contains(".jpg")
                                                && node.ParentNode.Attributes["href"].Value.StartsWith(
                                                    "http://www.therondels.fr/"))
                                            {
                                                this.article.Pictures.Add(node.ParentNode.Attributes["href"].Value);
                                                pictureFind = true;
                                            }
                                        }

                                        if (node.GetAttributeValue("src", "").StartsWith("http://www.therondels.fr/")
                                            && pictureFind == false)
                                        {
                                            this.article.Pictures.Add(node.GetAttributeValue("src", ""));
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (Settings.Contains("article" + this.article.Title))
                    {
                        Settings.Remove("article" + this.article.Title);
                    }

                    Settings.Add("article" + this.article.Title, this.article);
                    this.DisplayArticleData();
                }
                catch (Exception)
                {
                    if (this.exception == false)
                    {
                        this.exception = true;
                        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                    }
                }

            }
        }
Ejemplo n.º 3
0
 private static void AddSettings(Article article)
 {
     var settings = IsolatedStorageSettings.ApplicationSettings;
     if (!settings.Contains("article" + article.Title))
     {
         settings.Add("article" + article.Title, article);
     }
 }