aboutsummaryrefslogtreecommitdiff
path: root/Aggregator/Parser/Parser.cs
blob: 5a6e7e686ccf968cff43d3edf6b990eb93b7c53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using Aggregator.Classes;

namespace Aggregator.Parser {
    public static class Parser {
        public static IEnumerable<Article> Parse(string xml, Config config) {
            return XDocument.Parse(xml).Descendants("item")
                .Select(articles => articles.Descendants().ToArray())
                .Select(article => new Article()
                {
                    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();
        }
        public static IEnumerable<Article> ParseMultiple(string[] xmls, List<Config> configs) {
            if (configs.Count == 0 || xmls.Length == 0 || (xmls.Length != configs.Count)) {
                return null;
            }
            else {
                return xmls.Select((xml, index) => Parse(xml, configs[index])).SelectMany(i => i);
            }
        }
    }
}