diff options
author | Aryadev Chavali <aryadevchavali1@gmail.com> | 2019-02-04 00:24:32 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadevchavali1@gmail.com> | 2019-02-04 00:24:32 +0000 |
commit | 216629800e8af7fe2582ea53cf59de82026184bb (patch) | |
tree | d081e0f3c4d787c5b1e4a9e4fa3cfde5a25ffec9 /Aggregator/Parser/ParserService.cs | |
parent | 5f1278fb32f12cdfa0634dc9d8ed6f8b551f4e49 (diff) | |
download | newsaggregator-216629800e8af7fe2582ea53cf59de82026184bb.tar.gz newsaggregator-216629800e8af7fe2582ea53cf59de82026184bb.tar.bz2 newsaggregator-216629800e8af7fe2582ea53cf59de82026184bb.zip |
Changed class name
Diffstat (limited to 'Aggregator/Parser/ParserService.cs')
-rw-r--r-- | Aggregator/Parser/ParserService.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Aggregator/Parser/ParserService.cs b/Aggregator/Parser/ParserService.cs new file mode 100644 index 0000000..7d490cb --- /dev/null +++ b/Aggregator/Parser/ParserService.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Xml.Linq; +using System.Collections.Generic; +using Aggregator.Classes; + +namespace Aggregator.Parser { + /// <summary> + /// Class to provide Parsing services + /// </summary> + public static class Parser { + + /// <summary> + /// Parse a single xml code, given a config, to Articles + /// </summary> + /// <param name="xml">XML code from an RSS feed</param> + /// <param name="config">Config that tells parser where to get relevant information</param> + /// <returns>Parsed articles from XML code</returns> + public static IEnumerable<Article> Parse(string xml, Config config) { + + return XDocument.Parse(xml).Descendants("item") //make an XDocument out of the xml + .Select(articles => articles.Descendants().ToArray()) //turn each 'item' into it's respective descendants + .Select(article => new Article() //make a new article from each 'item' article, using config for pointers + { + Source = config.Source, + Title = article[config.Title].Value, + Description = article[config.Description].Value, + ArticleLink = article[config.ArticleLink].Value, + ImageLink = article[config.ImageLink].Attribute("url").Value, + PublishDate = DateTime.Parse(article[config.PublishDate].Value) + }).Distinct(); //remove any duplicates + } + + /// <summary> + /// Parse multiple xml sets, given a similar set of configs, to Articles + /// </summary> + /// <param name="xmls">Array of XML sets to parse for Articles</param> + /// <param name="configs">List of configs to parse for corresponding xml sets</param> + /// <returns>Parsed articles from every XML set</returns> + public static IEnumerable<Article> ParseMultiple(string[] xmls, List<Config> configs) { + if (configs.Count == 0 || xmls.Length == 0 || (xmls.Length != configs.Count)) { + return null; //for bad cases + } + else { + return xmls.Select((xml, index) => Parse(xml, configs[index])) //Parse each item + .SelectMany(i => i); //remove distinctions between each set of articles + } + } + } +}
\ No newline at end of file |