Ejemplo n.º 1
0
        private static void readXML(string feed)
        {
            Console.WriteLine("Reading " + feed);
            Uri feedUri;
            Uri.TryCreate(feed, UriKind.Absolute, out feedUri);
            feedUri.ToString();
            SyndicationFeed syndicationFeed;
            try {
                string Url = feed;
                //HtmlWeb web = new HtmlWeb();
                //HtmlDocument doc = web.Load(Url);
                //Console.WriteLine(doc.ToString());
                using (XmlReader reader = XmlReader.Create(feed))
                {
                    syndicationFeed = SyndicationFeed.Load(reader);
                }
            }catch(Exception ex)
            {
                Console.WriteLine("Exception on feedreader");
                Console.WriteLine(ex.Message);
                return;
            }
            syndicationFeed.Id = feed;
            Feed = feed;
            foreach (SyndicationItem si in syndicationFeed.Items)
            {
                FeedItem feedItem = new FeedItem();
                try
                {
                    feedItem = new FeedItem();
                    feedItem.setValues();

                    #region Things that should not go wrong
                    feedItem.FeedID = feed;
                    
                    feedItem.Title = si.Title.Text;


                    //Console.WriteLine("FeedItem: " + feedItem.Title);
                    if (si.Summary.Text.Length > 5)
                    {
                        feedItem.Summary = si.Summary.Text;
                    }

                    if (si.BaseUri != null)
                    feedItem.BaseUri = si.BaseUri.ToString();

                    if (si.Id != null)
                        feedItem.ID = si.Id;

                    if (si.PublishDate != null)
                        feedItem.PublishDate = si.PublishDate;

                    if (si.LastUpdatedTime != null)
                        feedItem.LastUpdated = si.LastUpdatedTime;

                    #endregion

                    #region Things that can go wrong

                    HtmlDocument doc = new HtmlDocument();
                    doc.Load(new StringReader(si.Summary.Text));
                    HtmlNode node = doc.DocumentNode.SelectNodes("//img").FirstOrDefault();
                    if(node != null)
                    {
                        Console.WriteLine(node.OuterHtml);
                        feedItem.ImageLink = node.OuterHtml;
                    }


                    int count = si.Authors != null ? si.Authors.Count : 0;
                    foreach (SyndicationPerson sp in si.Authors.Where(x => x != null))
                        feedItem.Authors += sp.Name + (count-- > 1 ? ", " : "");


                    if (si.Links != null) {
                        count = si.Links.Where(x => x != null).Count();
                        foreach (SyndicationLink sl in si.Links.Where(x => x != null))
                            feedItem.Links += sl.BaseUri + (count-- > 1 ? ", " : "");
                    }

                    

                    if(si.SourceFeed != null && si.SourceFeed.BaseUri != null && !string.IsNullOrEmpty(si.SourceFeed.BaseUri.ToString()))
                    feedItem.SourceFeed = si.SourceFeed.BaseUri.ToString();

                    if (si.Categories != null)
                    {
                        count = si.Categories.Count;
                        foreach (SyndicationCategory sc in si.Categories.Where(x => x != null))
                            feedItem.Categories += sc.Name + (count-- > 1 ? ", " : "");
                        if (si.Content != null)
                        {
                            TextSyndicationContent content = (TextSyndicationContent)si.Content;
                            feedItem.Content = content.Text;
                        }
                    }

                    if (si.Contributors != null)
                    {
                        count = si.Contributors.Where(x => x != null).Count();
                        foreach (SyndicationPerson spc in si.Contributors.Where(x => x != null))
                            feedItem.Contributors += spc.Name + (count-- > 1 ? ", " : "");
                    }
                    if (si.Copyright != null)
                        feedItem.Copyright = string.IsNullOrEmpty(si.Copyright.Text) ? "" : si.Copyright.Text;

                    #endregion
                }
                catch (Exception ex)
                {
                    errorCount++;
                    ErrorMessages += ex.Message + Environment.NewLine;
                    Console.WriteLine("Something went wrong: " + ex.Message);
                }
                finally
                {
                    //check if Summary, Title, date are present
                    if (feedItem.Title != null && feedItem.Title.Length > 0 &&
                        feedItem.PublishDate != null)
                    {
                        DatabaseQueue.Enqueue(feedItem);
                        FirstItemDone = true;
                        //Console.WriteLine("FeedItem added:");
                    }
                }
            }
            if (errorCount > 0)
            {
                MailMessage mail = new MailMessage();
                SmtpClient client = new SmtpClient();
                client.Port = 587;
                client.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Azerty123");
                client.EnableSsl = true;
                mail.IsBodyHtml = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Host = "smtp.live.com";
                mail.To.Add(new MailAddress("*****@*****.**"));
                mail.From = new MailAddress("*****@*****.**");

                mail.Subject = "Failure to add items";
                mail.Body = "Errors: " + errorCount + Environment.NewLine + ErrorMessages;
                //client.Send(mail);
                Console.WriteLine("Mail sent to admin");
            }
            errorCount = 0;
            ErrorMessages = "";
        }