From 1ea16ca4b12af510ef88361ec7303174cacc1849 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 23 Dec 2015 16:33:05 +0000 Subject: [PATCH 1/6] Initial pull of blogger categories --- .../Clients/GoogleBloggerv3Client.cs | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs index b35d214f..07dd2c4a 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs @@ -26,6 +26,7 @@ using Google.Apis.Blogger.v3.Data; using System.Net.Http.Headers; using OpenLiveWriter.Controls; using System.Windows.Forms; +using Newtonsoft.Json; namespace OpenLiveWriter.BlogClient.Clients { @@ -142,9 +143,9 @@ namespace OpenLiveWriter.BlogClient.Clients { // configure client options BlogClientOptions clientOptions = new BlogClientOptions(); - clientOptions.SupportsCategories = false; - clientOptions.SupportsMultipleCategories = false; - clientOptions.SupportsNewCategories = false; + clientOptions.SupportsCategories = true; + clientOptions.SupportsMultipleCategories = true; + clientOptions.SupportsNewCategories = true; clientOptions.SupportsCustomDate = true; clientOptions.SupportsExcerpt = false; clientOptions.SupportsSlug = false; @@ -303,10 +304,30 @@ namespace OpenLiveWriter.BlogClient.Clients return blogList.Items.Select(b => new BlogInfo(b.Id, b.Name, b.Url)).ToArray(); } + private const string CategoriesEndPoint = "/feeds/posts/summary?alt=json&max-results=0"; public BlogPostCategory[] GetCategories(string blogId) { - // Google Blogger does not support categories - return new BlogPostCategory[] { }; + var categories = new BlogPostCategory[0]; + var blog = GetService().Blogs.Get(blogId).Execute(); + + if (blog != null) + { + var categoriesUrl = string.Concat(blog.Url, CategoriesEndPoint); + + var response = SendAuthenticatedHttpRequest(categoriesUrl, 30, CreateAuthorizationFilter()); + if (response != null) + { + using (var reader = new StreamReader(response.GetResponseStream())) + { + var json = reader.ReadToEnd(); + var item = JsonConvert.DeserializeObject(json); + var cats = item?.Feed?.CategoryArray.Select(x => new BlogPostCategory(x.Term)); + categories = cats?.ToArray() ?? new BlogPostCategory[0]; + } + } + } + + return categories; } public BlogPostKeyword[] GetKeywords(string blogId) @@ -834,5 +855,22 @@ namespace OpenLiveWriter.BlogClient.Clients #endregion + public class Category + { + [JsonProperty("term")] + public string Term { get; set; } + } + + public class Feed + { + [JsonProperty("category")] + public Category[] CategoryArray { get; set; } + } + + public class CategoryResponse + { + [JsonProperty("feed")] + public Feed Feed { get; set; } + } } } From 2435a48763f69727aa5fdea1c6d2898f44aeb803 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 23 Dec 2015 16:48:21 +0000 Subject: [PATCH 2/6] Update provider xml for multicategory support --- .../OpenLiveWriter.BlogClient/Providers/BlogProvidersB5.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Providers/BlogProvidersB5.xml b/src/managed/OpenLiveWriter.BlogClient/Providers/BlogProvidersB5.xml index 9f879d16..82143bbe 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Providers/BlogProvidersB5.xml +++ b/src/managed/OpenLiveWriter.BlogClient/Providers/BlogProvidersB5.xml @@ -130,7 +130,7 @@ No WebLayout ^404$ - No + Yes Yes Yes Yes From 76d5d785eaac5d20d2be5fd92c7ac0f2094ed727 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 23 Dec 2015 16:58:31 +0000 Subject: [PATCH 3/6] Actually set the labels from categories --- .../Clients/GoogleBloggerv3Client.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs index 07dd2c4a..fafe7cef 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs @@ -107,10 +107,12 @@ namespace OpenLiveWriter.BlogClient.Clients private static Post ConvertToGoogleBloggerPost(BlogPost post) { + var labels = post.Categories?.Select(x => x.Name).ToList(); + labels?.AddRange(post.NewCategories?.Select(x => x.Name) ?? new List()); return new Post() { Content = post.Contents, - Labels = post.Keywords?.Split(new char[] { LabelDelimiter }, StringSplitOptions.RemoveEmptyEntries).Select(k => k.Trim()).ToList(), + Labels = labels ?? new List(), // TODO:OLW - DatePublishedOverride didn't work quite right. Either the date published override was off by several hours, // needs to be normalized to UTC or the Blogger website thinks I'm in the wrong time zone. Published = post.HasDatePublishedOverride ? post?.DatePublishedOverride : null, From cd938e62d5aebd776864aa62e1985a02d730cc2f Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 23 Dec 2015 17:00:23 +0000 Subject: [PATCH 4/6] Remove keywords, as not needed anymore --- .../Clients/GoogleBloggerv3Client.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs index fafe7cef..31e37770 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs @@ -23,7 +23,6 @@ using Google.Apis.Util; using System.Globalization; using System.Diagnostics; using Google.Apis.Blogger.v3.Data; -using System.Net.Http.Headers; using OpenLiveWriter.Controls; using System.Windows.Forms; using Newtonsoft.Json; @@ -152,8 +151,8 @@ namespace OpenLiveWriter.BlogClient.Clients clientOptions.SupportsExcerpt = false; clientOptions.SupportsSlug = false; clientOptions.SupportsFileUpload = true; - clientOptions.SupportsKeywords = true; - clientOptions.SupportsGetKeywords = true; + clientOptions.SupportsKeywords = false; + clientOptions.SupportsGetKeywords = false; clientOptions.SupportsPages = true; clientOptions.SupportsExtendedEntries = true; _clientOptions = clientOptions; From 1eb48152f8105b3bfd0ba7b85cd90c1b5da6fb0c Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 23 Dec 2015 19:13:14 +0000 Subject: [PATCH 5/6] Load categories when editing a post --- .../OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs index 31e37770..1cbaffca 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs @@ -88,7 +88,7 @@ namespace OpenLiveWriter.BlogClient.Clients Permalink = post.Url, Contents = post.Content, DatePublished = post.Published.Value, - Keywords = string.Join(new string(LabelDelimiter,1), post.Labels ?? new List()) + Categories = post.Labels.Select(x => new BlogPostCategory(x)).ToArray() }; } From 5c83552e9128f98c048160604951a7d7b1fe7d06 Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 20 Jan 2016 22:27:27 +0000 Subject: [PATCH 6/6] Fixed up some potential ArgumentNullExceptions --- .../Clients/GoogleBloggerv3Client.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs index 1cbaffca..0a84f3fb 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/GoogleBloggerv3Client.cs @@ -88,7 +88,7 @@ namespace OpenLiveWriter.BlogClient.Clients Permalink = post.Url, Contents = post.Content, DatePublished = post.Published.Value, - Categories = post.Labels.Select(x => new BlogPostCategory(x)).ToArray() + Categories = post.Labels?.Select(x => new BlogPostCategory(x)).ToArray() ?? new BlogPostCategory[0] }; } @@ -302,7 +302,7 @@ namespace OpenLiveWriter.BlogClient.Clients public BlogInfo[] GetUsersBlogs() { var blogList = GetService().Blogs.ListByUser("self").Execute(); - return blogList.Items.Select(b => new BlogInfo(b.Id, b.Name, b.Url)).ToArray(); + return blogList.Items?.Select(b => new BlogInfo(b.Id, b.Name, b.Url)).ToArray() ?? new BlogInfo[0]; } private const string CategoriesEndPoint = "/feeds/posts/summary?alt=json&max-results=0"; @@ -350,7 +350,7 @@ namespace OpenLiveWriter.BlogClient.Clients recentPostsRequest.Status = PostsResource.ListRequest.StatusEnum.Live; var recentPosts = recentPostsRequest.Execute(); - return recentPosts.Items.Select(p => ConvertToBlogPost(p)).ToArray(); + return recentPosts.Items?.Select(ConvertToBlogPost).ToArray() ?? new BlogPost[0]; } public string NewPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, out string etag, out XmlDocument remotePost) @@ -416,7 +416,7 @@ namespace OpenLiveWriter.BlogClient.Clients var getPagesRequest = GetService().Pages.List(blogId); var pageList = getPagesRequest.Execute(); - return pageList.Items.Select(p => ConvertToPageInfo(p)).ToArray(); + return pageList.Items?.Select(ConvertToPageInfo).ToArray() ?? new PageInfo[0]; } public BlogPost[] GetPages(string blogId, int maxPages) @@ -425,7 +425,7 @@ namespace OpenLiveWriter.BlogClient.Clients getPagesRequest.MaxResults = maxPages; var pageList = getPagesRequest.Execute(); - return pageList.Items.Select(p => ConvertToBlogPost(p)).ToArray(); + return pageList.Items?.Select(ConvertToBlogPost).ToArray() ?? new BlogPost[0]; } public string NewPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost)