public void GetRssFeed(string rssData, int?categoryId) { var date = new DateTime(); var xml = XDocument.Parse(rssData); try { var rssFeedDataList = xml.Descendants("channel"); var rssFeedData = xml.Descendants("item") .Select(x => new NewsArticle { Title = (string)x.Element("title"), SEOURL = StaticData.CleanTitleForSEO((string)x.Element("title")), Url = (string)x.Element("link"), Summary = StaticData.CleanDescription((string)x.Element("description")), //Picture = (string)x.Element("image")?.Element("url") == null? // "https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=2ahUKEwiLuZTK_8_iAhUHQhoKHSBKCSUQjRx6BAgBEAQ&url=https%3A%2F%2Fwww.iconfinder.com%2Ficons%2F341106%2Frss_icon&psig=AOvVaw12ydppBMhEOH011OSC8R-1&ust=1559743515365402" // : (string)x.Element("image")?.Element("url"), Picture = (string)x.Element("enclosure")?.Attribute("url") == null ? (string)x.Element("image")?.Element("url") : (string)x.Element("enclosure")?.Attribute("url"), //CreatedDate = (string)x.Element("pubDate") CreatedDate = !DateTime.TryParse((string)x.Element("pubDate"), CultureInfo.InvariantCulture, DateTimeStyles.None, out date) ? DateTime.Now : date }); SaveToDb(rssFeedData, rssData, categoryId); Console.WriteLine("End!"); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void GetRssFeed(string rssData, int?categoryId) { try { var r = new Regex(@"\s=\s"); var y = r.Replace(rssData, "="); var doc = XDocument.Parse(y); var entries = from item in doc.Root.Elements().Where(i => i.Name.LocalName == "entry") select new NewsArticle { SEOURL = StaticData.CleanTitleForSEO(item.Elements().First(i => i.Name.LocalName == "title").Value), Summary = StaticData.CleanDescription(item.Elements().First(i => i.Name.LocalName == "content") .Value), Url = item.Elements().First(i => i.Name.LocalName == "link").Attribute("href")?.Value, Picture = StaticData.GetImageSource(item.Elements().First(i => i.Name.LocalName == "content").Value) // ? item.Elements().First(i => i.Name.LocalName == "content").Value) , Title = item.Elements().First(i => i.Name.LocalName == "title").Value, CreatedDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "updated").Value) }; SaveToDbAtom(entries, rssData, categoryId); Console.WriteLine("End!"); } catch (XmlException ex) { Console.WriteLine("An error has occured" + ex.Message); } }
public string GetPictures(string link, string parent, string child, NewsArticle newsArticle) { var test = StaticData.GetImageSource(StaticData.CleanDescription(link)); var xml = XDocument.Parse(link); IEnumerable <NewsArticle> rssFeedData; if (link.Contains("rss")) { if (!_isValid) { rssFeedData = xml.Descendants(parent) .Select(x => new NewsArticle { Picture = (string)x.Element(child)?.Attribute("url") }); if (string.IsNullOrEmpty(rssFeedData.FirstOrDefault()?.Picture)) { _isValid = true; return(GetPictures(link, "channel", "image", newsArticle)); } } else { rssFeedData = xml.Descendants(parent) .Select(x => new NewsArticle { Picture = (string)x.Element(child)?.Element("url") }); _isValid = false; } return(rssFeedData.FirstOrDefault()?.Picture); } if (link.Contains("atom")) { var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(newsArticle.Summary); if (htmlDoc.DocumentNode.SelectNodes("//img[@src]") != null) { newsArticle.Picture = htmlDoc.DocumentNode.SelectNodes("//img[@src]").FirstOrDefault() ?.Attributes[0].Value; } newsArticle.Summary = Regex.Replace(htmlDoc.DocumentNode.SelectNodes("//p")[0].InnerHtml, @"\t|\n|\r", "").Trim(); return(newsArticle.Picture); } return(null); }