Merge pull request #234 from ScottIsAFool/Blogger-Categories

This commit is contained in:
Will Duff 2016-01-22 08:31:36 -08:00
commit abbccecbc6
2 changed files with 54 additions and 15 deletions

View File

@ -23,9 +23,9 @@ using Google.Apis.Util;
using System.Globalization; using System.Globalization;
using System.Diagnostics; using System.Diagnostics;
using Google.Apis.Blogger.v3.Data; using Google.Apis.Blogger.v3.Data;
using System.Net.Http.Headers;
using OpenLiveWriter.Controls; using OpenLiveWriter.Controls;
using System.Windows.Forms; using System.Windows.Forms;
using Newtonsoft.Json;
namespace OpenLiveWriter.BlogClient.Clients namespace OpenLiveWriter.BlogClient.Clients
{ {
@ -88,7 +88,7 @@ namespace OpenLiveWriter.BlogClient.Clients
Permalink = post.Url, Permalink = post.Url,
Contents = post.Content, Contents = post.Content,
DatePublished = post.Published.Value, DatePublished = post.Published.Value,
Keywords = string.Join(new string(LabelDelimiter,1), post.Labels ?? new List<string>()) Categories = post.Labels?.Select(x => new BlogPostCategory(x)).ToArray() ?? new BlogPostCategory[0]
}; };
} }
@ -106,10 +106,12 @@ namespace OpenLiveWriter.BlogClient.Clients
private static Post ConvertToGoogleBloggerPost(BlogPost post) 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<string>());
return new Post() return new Post()
{ {
Content = post.Contents, Content = post.Contents,
Labels = post.Keywords?.Split(new char[] { LabelDelimiter }, StringSplitOptions.RemoveEmptyEntries).Select(k => k.Trim()).ToList(), Labels = labels ?? new List<string>(),
// TODO:OLW - DatePublishedOverride didn't work quite right. Either the date published override was off by several hours, // 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. // needs to be normalized to UTC or the Blogger website thinks I'm in the wrong time zone.
Published = post.HasDatePublishedOverride ? post?.DatePublishedOverride : null, Published = post.HasDatePublishedOverride ? post?.DatePublishedOverride : null,
@ -142,15 +144,15 @@ namespace OpenLiveWriter.BlogClient.Clients
{ {
// configure client options // configure client options
BlogClientOptions clientOptions = new BlogClientOptions(); BlogClientOptions clientOptions = new BlogClientOptions();
clientOptions.SupportsCategories = false; clientOptions.SupportsCategories = true;
clientOptions.SupportsMultipleCategories = false; clientOptions.SupportsMultipleCategories = true;
clientOptions.SupportsNewCategories = false; clientOptions.SupportsNewCategories = true;
clientOptions.SupportsCustomDate = true; clientOptions.SupportsCustomDate = true;
clientOptions.SupportsExcerpt = false; clientOptions.SupportsExcerpt = false;
clientOptions.SupportsSlug = false; clientOptions.SupportsSlug = false;
clientOptions.SupportsFileUpload = true; clientOptions.SupportsFileUpload = true;
clientOptions.SupportsKeywords = true; clientOptions.SupportsKeywords = false;
clientOptions.SupportsGetKeywords = true; clientOptions.SupportsGetKeywords = false;
clientOptions.SupportsPages = true; clientOptions.SupportsPages = true;
clientOptions.SupportsExtendedEntries = true; clientOptions.SupportsExtendedEntries = true;
_clientOptions = clientOptions; _clientOptions = clientOptions;
@ -300,13 +302,33 @@ namespace OpenLiveWriter.BlogClient.Clients
public BlogInfo[] GetUsersBlogs() public BlogInfo[] GetUsersBlogs()
{ {
var blogList = GetService().Blogs.ListByUser("self").Execute(); 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";
public BlogPostCategory[] GetCategories(string blogId) public BlogPostCategory[] GetCategories(string blogId)
{ {
// Google Blogger does not support categories var categories = new BlogPostCategory[0];
return new BlogPostCategory[] { }; 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<CategoryResponse>(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) public BlogPostKeyword[] GetKeywords(string blogId)
@ -328,7 +350,7 @@ namespace OpenLiveWriter.BlogClient.Clients
recentPostsRequest.Status = PostsResource.ListRequest.StatusEnum.Live; recentPostsRequest.Status = PostsResource.ListRequest.StatusEnum.Live;
var recentPosts = recentPostsRequest.Execute(); 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) public string NewPost(string blogId, BlogPost post, INewCategoryContext newCategoryContext, bool publish, out string etag, out XmlDocument remotePost)
@ -394,7 +416,7 @@ namespace OpenLiveWriter.BlogClient.Clients
var getPagesRequest = GetService().Pages.List(blogId); var getPagesRequest = GetService().Pages.List(blogId);
var pageList = getPagesRequest.Execute(); 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) public BlogPost[] GetPages(string blogId, int maxPages)
@ -403,7 +425,7 @@ namespace OpenLiveWriter.BlogClient.Clients
getPagesRequest.MaxResults = maxPages; getPagesRequest.MaxResults = maxPages;
var pageList = getPagesRequest.Execute(); var pageList = getPagesRequest.Execute();
return pageList.Items?.Select(p => ConvertToBlogPost(p)).ToArray() ?? new BlogPost[0]; return pageList.Items?.Select(ConvertToBlogPost).ToArray() ?? new BlogPost[0];
} }
public string NewPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost) public string NewPage(string blogId, BlogPost page, bool publish, out string etag, out XmlDocument remotePost)
@ -834,5 +856,22 @@ namespace OpenLiveWriter.BlogClient.Clients
#endregion #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; }
}
} }
} }

View File

@ -130,7 +130,7 @@
<futurePublishDateWarning>No</futurePublishDateWarning> <futurePublishDateWarning>No</futurePublishDateWarning>
<defaultView>WebLayout</defaultView> <defaultView>WebLayout</defaultView>
<invalidPostIdFaultCodePattern>^404$</invalidPostIdFaultCodePattern> <invalidPostIdFaultCodePattern>^404$</invalidPostIdFaultCodePattern>
<supportsMultipleCategories>No</supportsMultipleCategories> <supportsMultipleCategories>Yes</supportsMultipleCategories>
<supportsEmptyTitles>Yes</supportsEmptyTitles> <supportsEmptyTitles>Yes</supportsEmptyTitles>
<requiresHtmlTitles>Yes</requiresHtmlTitles> <requiresHtmlTitles>Yes</requiresHtmlTitles>
<usePicasaImgMaxAlways>Yes</usePicasaImgMaxAlways> <usePicasaImgMaxAlways>Yes</usePicasaImgMaxAlways>