private void LoadFromXmlDocument() { var doc = new XmlDocument(); doc.Load(XmlReader); var posts = doc.GetElementsByTagName("post"); foreach (XmlNode post in posts) { var blogX = new BlogMlExtendedPost(); if (post.Attributes != null) { blogX.PostUrl = GetAttributeValue <string>(post.Attributes["post-url"]); } if (post.ChildNodes.Count <= 0) { blogsExtended.Add(blogX); continue; } foreach (XmlNode child in post.ChildNodes) { if (child.Name == "tags") { foreach (XmlNode tag in child.ChildNodes) { if (tag.Attributes != null) { if (blogX.Tags == null) { blogX.Tags = new List <string>(); } blogX.Tags.Add(GetAttributeValue <string>(tag.Attributes["ref"])); } } } if (child.Name == "comments") { LoadBlogComments(blogX, child); } if (child.Name == "trackbacks") { LoadBlogTrackbacks(blogX, child); } } blogsExtended.Add(blogX); } }
private void LoadBlogComments(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { // Id = GetGuid("comment", GetAttributeValue<string>(com.Attributes["id"])).ToString(), Author = GetAttributeValue <string>(com.Attributes["user-name"]), Email = GetAttributeValue <string>(com.Attributes["user-email"]), IP = GetAttributeValue <string>(com.Attributes["user-ip"]), CreationTime = GetDate(com.Attributes["date-created"]), }; var parentid = GetAttributeValue <string>(com.Attributes["parentid"]); if (!string.IsNullOrEmpty(parentid)) { c.ParentId = GetGuid("comment", parentid).ToString(); } if (!string.IsNullOrEmpty(GetAttributeValue <string>(com.Attributes["user-url"]))) { c.Website = (GetAttributeValue <string>(com.Attributes["user-url"])); } c.IsApproved = GetAttributeValue <bool>(com.Attributes["approved"]); foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "content") { c.Content = comNode.InnerText; } } if (blogX.Comments == null) { blogX.Comments = new List <Comment>(); } blogX.Comments.Add(c); } } }
private async Task <bool> AddPage(BlogMlExtendedPost extPost) { var p = new Page(); p.Title = extPost.BlogPost.Title; p.CreationTime = extPost.BlogPost.DateCreated; p.LastModificationTime = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.Published = extPost.BlogPost.Approved; if (!string.IsNullOrEmpty(extPost.PostUrl)) { // looking for a Slug with patterns such as: // /some-slug.aspx // /some-slug.html // /some-slug // Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase); if (slugMatch.Success) { p.Slug = slugMatch.Groups[1].Value.Trim(); } } if (string.IsNullOrEmpty(p.Slug)) { p.Slug = p.GetSeName(); } // skip if exists if (await _pageManager.FindBySlugAsync(p.Slug) != null) { return(false); } await _pageManager.CreateAsync(p); return(true); }
private void LoadBlogTrackbacks(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { Id = GetGuid("comment", GetAttributeValue <string>(com.Attributes["id"])).ToString(), IP = "127.0.0.1", IsApproved = GetAttributeValue <bool>(com.Attributes["approved"]), CreationTime = GetDate(com.Attributes["date-created"]) }; if (!string.IsNullOrEmpty(GetAttributeValue <string>(com.Attributes["url"]))) { c.Website = (GetAttributeValue <string>(com.Attributes["url"])); } foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "title") { c.Content = comNode.InnerText; } } c.Email = c.Content.ToLowerInvariant().Contains("pingback") ? "pingback" : "trackback"; c.Author = c.Email; if (blogX.Comments == null) { blogX.Comments = new List <Comment>(); } blogX.Comments.Add(c); } } }
private async Task <bool> AddPost(BlogMlExtendedPost extPost) { var p = new Post(); p.Title = extPost.BlogPost.Title; p.CreationTime = extPost.BlogPost.DateCreated; p.PublishedTime = extPost.BlogPost.DateCreated; p.LastModificationTime = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.IsDraft = !extPost.BlogPost.Approved; p.ViewsCount = GetValue(extPost.BlogPost.Views); if (extPost.BlogPost.HasExcerpt) { p.Description = extPost.BlogPost.Excerpt.UncodedText; } if (!string.IsNullOrEmpty(extPost.PostUrl)) { // looking for a Slug with patterns such as: // /some-slug.aspx // /some-slug.html // /some-slug // Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase); if (slugMatch.Success) { p.Slug = slugMatch.Groups[1].Value.Trim(); } } if (string.IsNullOrEmpty(p.Slug)) { p.Slug = p.GetSeName(); } // skip if exists if (await _postManager.FindBySlugAsync(p.Slug) != null) { return(false); } if (extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0) { // p.UserId = extPost.BlogPost.Authors[0].Ref; p.UserId = _author.Id; } if (extPost.Categories != null && extPost.Categories.Count > 0) { foreach (var item in extPost.Categories) { p.Categories.Add(new PostCategory() { CategoryId = item.Id, PostId = item.Id }); } } if (extPost.Tags != null && extPost.Tags.Count > 0) { foreach (var item in extPost.Tags) { var tag = await _tagsManager.CreateOrUpdateAsync(item); p.Tags.Add(new PostTags() { PostId = p.Id, TagsId = tag.Id }); } } await _postManager.CreateAsync(p); if (extPost.Comments != null && extPost.Comments.Count > 0) { foreach (var comment in extPost.Comments) { comment.PostId = p.Id; try { await _commentManager.CreateAsync(comment); } catch (Exception) { } } p.CommentsCount = extPost.Comments.Count; await _postManager.UpdateAsync(p); } return(true); }
private async Task LoadBlogPosts() { _logger.LogInformation("BlogReader.LoadBlogPosts: Start importing posts"); var allPost = blogsExtended.OrderByDescending(t => t.BlogPost.DateCreated).ToList(); foreach (BlogMlExtendedPost extPost in allPost) { if (extPost.BlogPost.PostType == BlogPostTypes.Normal) { try { BlogMlExtendedPost post = extPost; if (extPost.BlogPost.Categories.Count > 0) { for (var i = 0; i < extPost.BlogPost.Categories.Count; i++) { int i2 = i; var cId = GetGuid("category", post.BlogPost.Categories[i2].Ref).ToString(); foreach (var category in categoryLookup) { if (category.Id == cId) { if (extPost.Categories == null) { extPost.Categories = new List <Category>(); } extPost.Categories.Add(category); } } } } if (await AddPost(extPost)) { PostCount++; } else { _logger.LogInformation("Post '{0}' has been skipped" + extPost.BlogPost.Title); } } catch (Exception ex) { _logger.LogInformation("BlogReader.LoadBlogPosts: " + ex.Message); } } else if (extPost.BlogPost.PostType == BlogPostTypes.Article) { try { BlogMlExtendedPost post = extPost; if (await AddPage(extPost)) { PostCount++; } else { _logger.LogInformation("Post '{0}' has been skipped" + extPost.BlogPost.Title); } } catch (Exception ex) { _logger.LogInformation("BlogReader.LoadBlogPosts: " + ex.Message); } } } _logger.LogInformation(string.Format("BlogReader.LoadBlogPosts: Completed importing {0} posts", PostCount)); }