public void UploadCompletePost() { var medium = new MediumApiClient(WeblogInfo); bool result = medium.GetUser(); Assert.IsTrue(result, medium.ErrorMessage); // make sure there is at least one publication available var pubs = medium.GetBlogs(); Assert.IsNotNull(pubs, medium.ErrorMessage); Assert.IsTrue(pubs.Count > 0); string pubId = pubs.FirstOrDefault().BlogId as string; WeblogInfo.BlogId = pubId; var post = new Post { Tags = new string[] { "Markdown", "Test" }, Title = "Test Post #" + DataUtils.GenerateUniqueId(), Body = @"<h1>New Post</h1> <img src=""MarkdownMonster.png"" /> <p>This is a new post and text and image</p> " }; var mediumPost = medium.PublishCompletePost(post, documentBasePath: FileUtils.GetPhysicalPath(".\\")); Assert.IsNotNull(mediumPost.url, medium.ErrorMessage); Console.WriteLine(mediumPost.url); Console.WriteLine(mediumPost.id); ShellUtils.GoUrl(mediumPost.url); }
public void UploadPost() { var medium = new MediumApiClient(WeblogInfo); bool result = medium.GetUser(); Assert.IsTrue(result, medium.ErrorMessage); var post = new MediumPost() { tags = new string[] { "Markdown", "Test" }, title = "Test Post #" + DataUtils.GenerateUniqueId(), content = "This is a **test post**.", contentFormat = "markdown", publishStatus = "draft", notifyFollowers = false, canonicalUrl = "https://weblog.west-wind.com" }; post = medium.PublishPost(post); Assert.IsNotNull(post.url, medium.ErrorMessage); Console.WriteLine(post.url); Console.WriteLine(post.id); ShellUtils.GoUrl(post.url); }
public void UploadPostToPublication() { var medium = new MediumApiClient(WeblogInfo); bool result = medium.GetUser(); Assert.IsTrue(result, medium.ErrorMessage); // make sure there is at least one publication available var pubs = medium.GetBlogs(); Assert.IsNotNull(pubs, medium.ErrorMessage); string pubId = pubs.FirstOrDefault().BlogId as string; var post = new MediumPost() { tags = new string[] { "Markdown", "Test" }, title = "Test Post #" + DataUtils.GenerateUniqueId(), content = "This is a **test post**.", contentFormat = "markdown", publishStatus = "draft", notifyFollowers = false, canonicalUrl = "https://weblog.west-wind.com" }; post = medium.PublishPost(post, pubId); Assert.IsNotNull(post.url, medium.ErrorMessage); Console.WriteLine(post.url); Console.WriteLine(post.id); ShellUtils.GoUrl(post.url); }
public void GetUser() { var medium = new MediumApiClient(WeblogInfo); bool result = medium.GetUser(); Assert.IsTrue(result, medium.ErrorMessage); Assert.IsNotNull(medium.User.id); Console.WriteLine(medium.User.id); }
public void UploadImage() { var medium = new MediumApiClient(WeblogInfo); bool result = medium.GetUser(); Assert.IsTrue(result, medium.ErrorMessage); string url = medium.PublishImage(".\\MarkdownMonster.png"); Assert.IsNotNull(url, medium.ErrorMessage); Console.WriteLine(url); ShellUtils.GoUrl(url); }
public void GetBlogs() { Console.WriteLine("Starting."); var medium = new MediumApiClient(WeblogInfo); bool result = medium.GetUser(); Assert.IsTrue(result, medium.ErrorMessage); var pubs = medium.GetBlogs(); Assert.IsNotNull(pubs, medium.ErrorMessage); foreach (var pub in pubs) { Console.WriteLine(pub.BlogId + " - " + pub.BlogName); } }
private void DropDownButton_Click(object sender, RoutedEventArgs e) { ShowStatus("Getting Blog listing information from service..."); WindowUtilities.DoEvents(); var context = Resources["BlogsContextMenu"] as ContextMenu; context.Items.Clear(); IEnumerable <UserBlog> blogs = null; try { if (Model.ActiveWeblogInfo.Type == WeblogTypes.Medium) { var client = new MediumApiClient(Model.ActiveWeblogInfo); blogs = client.GetBlogs(); if (blogs == null) { ShowStatus("Failed to get blog listing: " + client.ErrorMessage, 6000); } } else if (Model.ActiveWeblogInfo.Type == WeblogTypes.MetaWeblogApi || Model.ActiveWeblogInfo.Type == WeblogTypes.Wordpress) { var client = new MetaWebLogWordpressApiClient(Model.ActiveWeblogInfo); blogs = client.GetBlogs(); if (blogs == null) { ShowStatus("Failed to get blog listing: " + client.ErrorMessage, 6000); } } } catch (Exception ex) { ShowStatus("Failed to get blogs: " + ex.Message, 6000); return; } ShowStatus("Blogs retrieved...", 2000); if (blogs == null) { return; } string blogId = Model.ActiveWeblogInfo.BlogId as string; if (!string.IsNullOrEmpty(blogId) && !blogs.Any(b => blogId == b.BlogId as string)) { context.Items.Add(new MenuItem { Header = blogId, Tag = blogId }); } foreach (var blog in blogs) { var item = new MenuItem() { Header = blog.BlogName, Tag = blog.BlogId, }; item.Click += (s, ea) => { var mitem = s as MenuItem; if (mitem == null) { return; } Model.ActiveWeblogInfo.BlogId = mitem.Tag as string; context.Items.Clear(); }; context.Items.Add(item); } }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogInfo weblogInfo, bool sendAsDraft = false) { var editor = Model.ActiveEditor; if (editor == null) { return(false); } var doc = editor.MarkdownDocument; WeblogModel.ActivePost = new Post() { DateCreated = DateTime.Now }; // start by retrieving the current Markdown from the editor string markdown = editor.GetMarkdown(); // Retrieve Meta data from post and clean up the raw markdown // so we render without the config data var meta = WeblogPostMetadata.GetPostConfigFromMarkdown(markdown, WeblogModel.ActivePost, weblogInfo); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); WeblogModel.ActivePost.Body = html; WeblogModel.ActivePost.PostId = meta.PostId; WeblogModel.ActivePost.PostStatus = meta.PostStatus; // Custom Field Processing: // Add custom fields from existing post // then add or update our custom fields var customFields = new Dictionary <string, CustomField>(); // load existing custom fields from post online if possible if (!string.IsNullOrEmpty(meta.PostId)) { var existingPost = GetPost(meta.PostId, weblogInfo); if (existingPost != null && meta.CustomFields != null && existingPost.CustomFields != null) { customFields = existingPost.CustomFields .ToDictionary(cf => cf.Key, cf => cf); } } // add custom fields from Weblog configuration if (weblogInfo.CustomFields != null) { foreach (var kvp in weblogInfo.CustomFields) { if (!customFields.ContainsKey(kvp.Key)) { AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value); } } } // add custom fields from Meta data if (meta.CustomFields != null) { foreach (var kvp in meta.CustomFields) { AddOrUpdateCustomField(customFields, kvp.Key, kvp.Value.Value); } } if (!string.IsNullOrEmpty(markdown)) { AddOrUpdateCustomField(customFields, "mt_markdown", markdown); } WeblogModel.ActivePost.CustomFields = customFields.Values.ToArray(); var config = WeblogAddinConfiguration.Current; var kv = config.Weblogs.FirstOrDefault(kvl => kvl.Value.Name == meta.WeblogName); if (kv.Equals(default(KeyValuePair <string, WeblogInfo>))) { MessageBox.Show("Invalid Weblog configuration selected.", "Weblog Posting Failed", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } weblogInfo = kv.Value; var type = weblogInfo.Type; if (type == WeblogTypes.Unknown) { type = weblogInfo.Type; } string basePath = Path.GetDirectoryName(doc.Filename); string postUrl = null; if (type == WeblogTypes.MetaWeblogApi || type == WeblogTypes.Wordpress) { MetaWebLogWordpressApiClient client; client = new MetaWebLogWordpressApiClient(weblogInfo); // if values are already configured don't overwrite them again client.DontInferFeaturedImage = meta.DontInferFeaturedImage; client.FeaturedImageUrl = meta.FeaturedImageUrl; client.FeatureImageId = meta.FeaturedImageId; if (!client.PublishCompletePost(WeblogModel.ActivePost, basePath, sendAsDraft, markdown)) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage); MessageBox.Show("Error sending post to Weblog: " + client.ErrorMessage, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } var post = client.GetPost(WeblogModel.ActivePost.PostId); if (post != null) { postUrl = post.Url; } } if (type == WeblogTypes.Medium) { var client = new MediumApiClient(weblogInfo); var result = client.PublishCompletePost(WeblogModel.ActivePost, basePath, sendAsDraft); if (result == null) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage); MessageBox.Show($"Error sending post to Weblog: " + client.ErrorMessage, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } // this is null postUrl = client.PostUrl; } meta.PostId = WeblogModel.ActivePost.PostId.ToString(); // retrieve the raw editor markdown markdown = editor.GetMarkdown(); meta.RawMarkdownBody = markdown; // add the meta configuration to it markdown = meta.SetPostYaml(); // write it back out to editor editor.SetMarkdown(markdown, updateDirtyFlag: true); try { // preview post if (!string.IsNullOrEmpty(weblogInfo.PreviewUrl)) { var url = weblogInfo.PreviewUrl.Replace("{0}", WeblogModel.ActivePost.PostId.ToString()); ShellUtils.GoUrl(url); } else { if (!string.IsNullOrEmpty(postUrl)) { ShellUtils.GoUrl(postUrl); } else { ShellUtils.GoUrl(new Uri(weblogInfo.ApiUrl).GetLeftPart(UriPartial.Authority)); } } } catch { mmApp.Log("Failed to display Weblog Url after posting: " + weblogInfo.PreviewUrl ?? postUrl ?? weblogInfo.ApiUrl); } return(true); }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogInfo weblogInfo, bool sendAsDraft = false) { var editor = Model.ActiveEditor; if (editor == null) { return(false); } var doc = editor.MarkdownDocument; ActivePost = new Post() { DateCreated = DateTime.Now }; // start by retrieving the current Markdown from the editor string markdown = editor.GetMarkdown(); // Retrieve Meta data from post and clean up the raw markdown // so we render without the config data var meta = GetPostConfigFromMarkdown(markdown, weblogInfo); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); ActivePost.Body = html; ActivePost.PostID = meta.PostId; var customFields = new List <CustomField>(); if (!string.IsNullOrEmpty(markdown)) { customFields.Add(new CustomField() { ID = "mt_markdown", Key = "mt_markdown", Value = markdown }); } if (weblogInfo.CustomFields != null) { foreach (var kvp in weblogInfo.CustomFields) { customFields.Add(new CustomField { ID = kvp.Key, Key = kvp.Key, Value = kvp.Value }); } } if (meta.CustomFields != null) { foreach (var kvp in meta.CustomFields) { customFields.Add(new CustomField { Key = kvp.Key, ID = kvp.Key, Value = kvp.Value }); } } ActivePost.CustomFields = customFields.ToArray(); var config = WeblogAddinConfiguration.Current; var kv = config.Weblogs.FirstOrDefault(kvl => kvl.Value.Name == meta.WeblogName); if (kv.Equals(default(KeyValuePair <string, WeblogInfo>))) { MessageBox.Show("Invalid Weblog configuration selected.", "Weblog Posting Failed", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } weblogInfo = kv.Value; var type = weblogInfo.Type; if (type == WeblogTypes.Unknown) { type = weblogInfo.Type; } string basePath = Path.GetDirectoryName(doc.Filename); string postUrl = null; if (type == WeblogTypes.MetaWeblogApi || type == WeblogTypes.Wordpress) { MetaWebLogWordpressApiClient client; client = new MetaWebLogWordpressApiClient(weblogInfo); // if values are already configured don't overwrite them again client.FeaturedImageUrl = meta.FeaturedImageUrl; client.FeatureImageId = meta.FeatureImageId; if (!client.PublishCompletePost(ActivePost, basePath, sendAsDraft, markdown)) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage); MessageBox.Show($"Error sending post to Weblog: " + client.ErrorMessage, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } postUrl = client.GetPostUrl(ActivePost.PostID); } if (type == WeblogTypes.Medium) { var client = new MediumApiClient(weblogInfo); var result = client.PublishCompletePost(ActivePost, basePath, sendAsDraft); if (result == null) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: " + client.ErrorMessage); MessageBox.Show($"Error sending post to Weblog: " + client.ErrorMessage, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } // this is null postUrl = client.PostUrl; } meta.PostId = ActivePost.PostID.ToString(); // retrieve the raw editor markdown markdown = editor.GetMarkdown(); meta.RawMarkdownBody = markdown; // add the meta configuration to it markdown = SetConfigInMarkdown(meta); // write it back out to editor editor.SetMarkdown(markdown); // preview post if (!string.IsNullOrEmpty(weblogInfo.PreviewUrl)) { var url = weblogInfo.PreviewUrl.Replace("{0}", ActivePost.PostID.ToString()); ShellUtils.GoUrl(url); } else { if (postUrl != null) { ShellUtils.GoUrl(postUrl); } else { ShellUtils.GoUrl(new Uri(weblogInfo.ApiUrl).GetLeftPart(UriPartial.Authority)); } } return(true); }