/// <summary> /// Loads news from single RSS source. Asynchronous call /// </summary> /// <param name="source">Rss source</param> /// <param name="refresh">Flag indicating that news should be refreshed</param> /// <returns>News headers</returns> public Task<NewsHeaders> GetNewsFromSingleSource(RssSource source, bool refresh) { var task = new Task<NewsHeaders>(() => { var headers = _rssProvider.GetArticlesHeaders(source, refresh); return new NewsHeaders { Headers = headers, TotalArticlesCount = headers.Count }; }); task.Start(); return task; }
/// <summary> /// Loads news from single RSS source. Asynchronous call /// </summary> /// <param name="source">Rss source</param> /// <param name="refresh">Flag indicating that news should be refreshed</param> /// <returns>News headers</returns> public Task<NewsHeaders> GetNewsFromSingleSource(RssSource source, bool refresh) { throw new NotImplementedException(); }
/// <summary> /// Obtains articles by their RSS source /// </summary> /// <param name="source">RSS source</param> /// <returns>List of articles</returns> public List<ArticleContainer> GetArticlesBySource(RssSource source) { return _articleStorage.Values.Where(art => art.Header.Source == source.SiteName).ToList(); }
/// <summary> /// Obtains article headers from RSS source /// </summary> /// <param name="source">Rss source</param> /// <param name="refresh">Flad indicating news should be refreshed</param> /// <returns>News headers</returns> public List<NewsHeader> GetArticlesHeaders(RssSource source, bool refresh) { var articleHeaders = new List<NewsHeader>(); var rssFeed = new XmlDocument(); #region new data loading if (refresh) { try { rssFeed.Load(source.Url); // todo refactor this with HttpClient // Load logo if present if (source.Logo == null) { var logoNodes = rssFeed.SelectNodes("rss/channel/image"); if (logoNodes != null) { foreach (var logoUrl in from XmlNode inner in logoNodes select inner.SelectSingleNode("url") into urlNode where urlNode != null select urlNode.InnerText) { var url = logoUrl; if (!logoUrl.Contains("http")) url = "http:" + url; GetContent(url).ContinueWith(response => { if (response.Result != null) { var content = response.Result.Content as StreamContent; if (content != null) content.ReadAsByteArrayAsync() .ContinueWith(bytes => source.Logo = bytes.Result); } }).Wait(); break; } } } var rssNodes = rssFeed.SelectNodes("rss/channel/item"); if (rssNodes != null) { foreach (XmlNode rssNode in rssNodes) { var rssSubNode = rssNode.SelectSingleNode("link"); var link = rssSubNode != null ? rssSubNode.InnerText : ""; var task = _contentStorage.GetArticleByUrl(link); task.Wait(); if (task.Result != null) { articleHeaders.Add(task.Result.Header); continue; } rssSubNode = rssNode.SelectSingleNode("title"); var title = rssSubNode != null ? rssSubNode.InnerText.Replace("—", "-") : ""; rssSubNode = rssNode.SelectSingleNode("description"); var description = rssSubNode != null ? rssSubNode.InnerText.StripTagsRegex() : ""; if (description.Length > 150) description = description.Substring(0, 147) + "..."; // trim too long description for pop-over rssSubNode = rssNode.SelectSingleNode("pubDate"); var date = rssSubNode != null ? rssSubNode.InnerText : ""; rssSubNode = rssNode.SelectSingleNode("enclosure"); var enclosure = rssSubNode != null && rssSubNode.Attributes != null ? rssSubNode.Attributes["url"].Value : ""; byte[] enclosured = null; if (enclosure != "") { GetContent(enclosure).ContinueWith(response => { var content = response.Result.Content as StreamContent; if (content != null) content.ReadAsByteArrayAsync() .ContinueWith(bytes => enclosured = bytes.Result); }).Wait(); } var newsHeader = new NewsHeader { Description = description, Link = link, Title = title, Guid = Guid.NewGuid(), PublishDate = DateTime.Parse(date), Enclosure = enclosured, Source = source.SiteName, HasLogo = source.Logo != null, HasEnclosure = enclosured != null }; articleHeaders.Add(newsHeader); _contentStorage.SaveArticle(new ArticleContainer { Guid = newsHeader.Guid, RssSource = source, Header = newsHeader }); } } } catch (Exception ex) { _errorDescription = new ErrorDescription { Level = ApplicationLevel.RssParser, Description = ex.Message }; } } #endregion #region obtaining loaded data else articleHeaders = _contentStorage.GetArticlesBySource(source).Select(art => art.Header).ToList(); #endregion return articleHeaders; }