Esempio n. 1
0
 public void AddArticle(Article article)
 {
     if (Start > article.PublishDate)
     {
         return;
     }
     foreach (var existing in Articles.Where(x => x.UniqueId == article.UniqueId).ToList())
     {
         Articles.Remove(existing);
         article.Id = existing.Id;
         article.PublishDate = existing.PublishDate;
     }
     Articles.Add(article);
     article.ChunkId = Id;
     if (Articles.Any(x => x.PublishDate > article.PublishDate))
     {
         Articles = Articles.OrderBy(x => x.PublishDate).ToList();
     }
 }
Esempio n. 2
0
        private Article ReadArticle(XElement xentry, DateTime now)
        {
            var article = new Article();

            // RSS stuffs
            Elem(xentry, "title", v => article.Title = v);
            Elem(xentry, "author", v => article.Authors.Add(new Author { Name = v }));
            Elem(xentry, elements + "creator", v => article.Authors.Add(new Author { Name = v }));
            Elem(xentry, "description", v => article.Description = v);
            Elem(xentry, "guid", v => article.UniqueId = v);
            Elem(xentry, "pubDate", v => article.PublishDate = DateTime.Parse(v), v => TryParseRfc1123(v, ref article.PublishDate));
            ElemLink(xentry, "link", v => article.Link = v);
            ElemLink(xentry, "comments", v => article.CommentLink = v);

            // Atom stuffs
            Elem(xentry, atom + "id", v => article.UniqueId = v);
            Elem(xentry, atom + "title", v => article.Title = v);
            ReadAuthors(xentry, "author", article.Authors);
            ReadAuthors(xentry, "contributor", article.Authors);
            ElemAttrLink(xentry, v => article.Link = v);

            if (string.IsNullOrEmpty(article.UniqueId))
            {
                // Hash the XML element itself to get a unique ID.
                // This will be hell on porting if that ever comes up...
                article.UniqueId = xentry.ToString().GetHashCode().ToString();
            }

            if (article.PublishDate == DateTime.MinValue)
            {
                // Atom uses ISO8601 rather than RFC1123. Yay!
                Elem(xentry, atom + "published", v => article.PublishDate = DateTime.Parse(v).ToUniversalTime());
                if (article.PublishDate == DateTime.MinValue)
                {
                    Elem(xentry, atom + "updated", v => article.PublishDate = DateTime.Parse(v).ToUniversalTime());
                    if (article.PublishDate == DateTime.MinValue)
                    {
                        article.PublishDate = now;
                    }
                }
            }
            Elem(xentry, atom + "summary", v => article.Summary = v);

            // Should pay attention to the content type.
            var content = xentry.Elements(atom + "content").FirstOrDefault();
            if (content == null)
            {
                content = xentry.Elements(FeedParser.content + "encoded").FirstOrDefault();
            }
            if (content != null)
            {
                if (!string.IsNullOrWhiteSpace(content.Value))
                {
                    article.Description = content.Value;
                    var type = content.Attribute("type");
                    if (type != null &&
                        type.Value.Contains("html") &&
                        !article.Description.Contains("<") &&
                        article.Description.Contains("&"))
                    {
                        // A lot of people seem to double-encode this -- atomenabled.org says you should.
                        // It's pretty f*****g stupid, but there you go.
                        article.Description = WebUtility.HtmlDecode(article.Description);
                    }
                }
                var attr = content.Attribute("src");
                if (attr != null)
                {
                    if (article.Link == null)
                    {
                        // Maybe just have a series of links, with optional tag?
                        Uri.TryCreate(attr.Value, UriKind.Absolute, out article.Link);
                    }
                }
            }
            return article;
        }