示例#1
0
        private static async Task <List <SyndicationItem> > GetFeedItemsForBlogger(Blogger blogger, HttpClient client)
        {
            var feedItems = new List <SyndicationItem>();
            var timer     = Stopwatch.StartNew();

            String.Format("Parsing / adding Items from \"{0}\" ", blogger.Name).ConsoleWriteLine(ConsoleColor.White);

            try
            {
                // check feed type
                switch (blogger.FeedType.ToLower())
                {
                case "rss":


                    var rssItems = await GetRssItemsForBlogger(blogger, client);

                    rssItems.ForEach(feedItems.Add);
                    break;


                case "atom":
                    var atomItems = await GetAtomItemsForBlogger(blogger, client);

                    atomItems.ForEach(feedItems.Add);
                    break;

                default:
                    break;
                }
                String.Format("Successfully parsed Items from \"{0}\" in {1}s. ", blogger.Name, timer.Elapsed.TotalSeconds)
                .ConsoleWriteLine(ConsoleColor.Green);
                return(feedItems);
            }
            catch (Exception ex)
            {
                timer.Stop();
                String.Format("Failed Parsing / adding Items from \"{0}\"", blogger.Name).ConsoleWriteLine(ConsoleColor.White);
                string.Format("With exception {0}", ex.Message).ConsoleWriteLine(ConsoleColor.Red);
                return(feedItems);
                // throw;
            }
        }
示例#2
0
        private static async Task <List <SyndicationItem> > GetAtomItemsForBlogger(Blogger blogger, HttpClient client)
        {
            var atomItems = new List <SyndicationItem>();
            // parse Atom feed
            var atomResponse = await client.GetAsync(blogger.BlogfeedUrl);

            var atomStream = await atomResponse.Content.ReadAsStreamAsync();

            var atomStreamReader = new StreamReader(atomStream, Encoding.UTF8);

            var atomReader     = XmlReader.Create(atomStreamReader);
            var atomSerializer = new Atom10FeedFormatter();

            atomSerializer.ReadFrom(atomReader);
            var atomFeed = atomSerializer.Feed;

            foreach (var item in atomFeed.Items)
            {
                var newItem = new SyndicationItem
                {
                    BaseUri         = item.BaseUri,
                    Content         = item.Content,
                    Id              = item.Id,
                    LastUpdatedTime = item.LastUpdatedTime
                };

                foreach (var link in item.Links)
                {
                    newItem.Links.Add(link);
                }
                newItem.PublishDate = item.PublishDate;
                newItem.Summary     = item.Summary;
                newItem.Title       = item.Title;

                var copyright =
                    new TextSyndicationContent(blogger.Name);

                newItem.Copyright = copyright;


                if (item.ElementExtensions.Count > 0)
                {
                    var reader = item.ElementExtensions.GetReaderAtElementExtensions();
                    while (reader.Read())
                    {
                        if ("content:encoded" == reader.Name)
                        {
                            SyndicationContent content =
                                SyndicationContent.CreatePlaintextContent(reader.ReadString());
                            newItem.Content = content;
                        }
                    }
                }


                // assign author name explicitly because email is
                // used by default
                var author = new SyndicationPerson {
                    Name = blogger.Name
                };
                newItem.Authors.Add(author);

                newItem.Contributors.Add(author);

                var doc     = new XmlDocument();
                var creator = String.Format(
                    "<dc:creator xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{0}</dc:creator>",
                    blogger.Name);
                doc.LoadXml(creator);
                var insertext = new SyndicationElementExtension(new XmlNodeReader(doc.DocumentElement));

                newItem.ElementExtensions.Add(insertext);

                atomItems.Add(newItem);
            }
            return(atomItems);
        }