/// <summary> /// Loads news from multiple RSS sources /// </summary> /// <param name="sources">RSS Sources list</param> /// <param name="page">Current page</param> /// <param name="refresh">Flag indicating that news should be refreshed</param> /// <returns>News headers</returns> public NewsHeaders GetNews(RssSources sources, int page, bool refresh) { var articleHeaders = new List<NewsHeader>(); foreach (var headers in sources.Sources.Select(src => _rssProvider.GetArticlesHeaders(src, refresh))) articleHeaders.AddRange(headers); var totalCount = articleHeaders.Count; articleHeaders = articleHeaders.OrderByDescending(article => article.PublishDate) .Where((article, i) => (i >= NewsPerPage*(page - 1)) && (i < NewsPerPage*page)) .ToList(); return new NewsHeaders { TotalArticlesCount = totalCount, Headers = articleHeaders }; }
/// <summary> /// Obtains list of RSS sources from Web.config /// </summary> /// <returns>List of Rss source URLs</returns> public RssSources GetRssSources(RssSources sources) { var appsettings = WebConfigurationManager.AppSettings; var srcs = new RssSources { Sources = (from object key in appsettings.Keys select key.ToString() into strKey let splitted = strKey.Split(':') where splitted[0] == "rss" let url = WebConfigurationManager.AppSettings[strKey] select new RssSource { SiteName = splitted[1], Url = url }).ToList() }; if (sources != null) srcs.Sources = srcs.Sources.Where(src => sources.Sources.Any(site => site.SiteName == src.SiteName)).ToList(); return srcs; }
/// <summary> /// Loads news from multiple RSS sources /// </summary> /// <param name="sources">RSS Sources list</param> /// <param name="page">Current page</param> /// <param name="refresh">Flag indicating that news should be refreshed</param> /// <returns>News headers</returns> public NewsHeaders GetNews(RssSources sources, int page, bool refresh) { var headers = new List<NewsHeader>(); var randomizer = new Random(); var amount = (int) Math.Ceiling(randomizer.NextDouble()*10); for (var i = 0; i < amount; i++) { headers.Add(new NewsHeader { Guid = Guid.NewGuid(), PublishDate = DateTime.Now.AddMinutes(-i), Title = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." }); } var timeToSleep = (int) (randomizer.NextDouble()*10000); Thread.Sleep(timeToSleep); return new NewsHeaders { Headers = headers }; }
private Task<NewsHeaders> GetNews(RssSources sources, int page, bool refresh) { var task = new Task<NewsHeaders>( () => _newsProvider.GetNews(_rssSourcesProvider.GetRssSources(sources), page, refresh)); task.Start(); return task; }
public async Task<IHttpActionResult> Post(JObject jsonData) { int page; RssSources sources; bool refresh; try { dynamic jSon = jsonData; var jPage = jSon["page"]; var jSources = jSon["sources"]; var jRefresh = jSon["refresh"]; page = jPage.ToObject<int>(); refresh = jRefresh.ToObject<bool>(); var jarray = jSources.ToObject<JArray>(); var list = new List<RssSource>(); foreach (var src in jarray) { list.Add(new RssSource { SiteName = (string) src["sitename"] }); } sources = new RssSources { Sources = list }; } catch { return BadRequest(); } var headers = await GetNews(sources, page, refresh); return Ok(headers); }
/// <summary> /// Asynchronous SignalR RPC call. Loads list of news headers and reports loading progress. Called from client /// </summary> /// <param name="jsonData">Request data in JSON format. Parsed inside</param> /// <param name="progress">Progress reporting callback</param> /// <returns>Container with list of news headers</returns> public async Task<NewsHeaders> LoadNews(JObject jsonData, IProgress<int> progress) { int page; bool refresh; RssSources rssSources; try { dynamic jSon = jsonData; var jPage = jSon["page"]; var jSources = jSon["sources"]; var jRefresh = jSon["refresh"]; page = jPage.ToObject<int>(); refresh = jRefresh.ToObject<bool>(); var jarray = jSources.ToObject<JArray>(); var list = new List<RssSource>(); foreach (var src in jarray) { list.Add(new RssSource { SiteName = (string) src["sitename"] }); } rssSources = new RssSources { Sources = list }; } catch { if(progress != null) progress.Report(100); return new NewsHeaders(); } var sources = _rssSourcesProvider.GetRssSources(rssSources).Sources; var totalSourcesCount = sources.Count; var progressIncrement = 100/totalSourcesCount; var totalProgress = 0; var totalHeaders = new NewsHeaders { TotalArticlesCount = 0, Headers = new List<NewsHeader>() }; foreach (var src in sources) { var headers = await _newsProvider.GetNewsFromSingleSource(src, refresh); totalHeaders.Headers.AddRange(headers.Headers); totalHeaders.TotalArticlesCount += headers.TotalArticlesCount; totalProgress += progressIncrement; if(progress != null) progress.Report(totalProgress); } totalHeaders.Headers = totalHeaders.Headers.OrderByDescending(article => article.PublishDate) .Where((article, i) => (i >= NewsPerPage*(page - 1)) && (i < NewsPerPage*page)) .ToList(); if(progress!= null) progress.Report(100); return totalHeaders; }