async void FetchClick(object Sender, RoutedEventArgs e) { if (_mwvm.SelectedTab.Module.Provider == null) { return; } List <Post> posts = await MetaWeblogWrapper.GetRecentPosts(blogger, _mwvm.SelectedTab.Module.Cmdlets, providerInfo.FetchPostCount); foreach (CmdletObject cmdlet in _mwvm.SelectedTab.Module.Cmdlets) { Int32 index = posts.IndexOf(new Post { Title = cmdlet.Name }); if (index >= 0) { cmdlet.ArticleIDString = posts[index].PostId; cmdlet.URL = _mwvm.SelectedTab.Module.Provider.ProviderName.ToLower() == "codeplex" ? _mwvm.SelectedTab.Module.Provider.Blog.url + "wikipage?title=" + cmdlet.Name : posts[index].Permalink; if (!Uri.IsWellFormedUriString(cmdlet.URL, UriKind.Absolute)) { var baseUrl = new Uri(_mwvm.SelectedTab.Module.Provider.ProviderURL); cmdlet.URL = String.Format("{0}://{1}{2}", baseUrl.Scheme, baseUrl.DnsSafeHost, cmdlet.URL); } } } }
/// <summary> /// Parses each of the images in the document and posts them to the server. /// Updates the HTML with the returned Image Urls /// </summary> /// <param name="html">HTML that contains images</param> /// <param name="filename">image file name</param> /// <param name="wrapper">blog wrapper instance that sends</param> /// <param name="metaData">metadata containing post info</param> /// <returns>update HTML string for the document with updated images</returns> private string SendImages(string html, string filename, MetaWeblogWrapper wrapper, WeblogPostMetadata metaData) { var basePath = Path.GetDirectoryName(filename); var baseName = Path.GetFileName(basePath); var doc = new HtmlDocument(); doc.LoadHtml(html); try { // send up normalized path images as separate media items var images = doc.DocumentNode.SelectNodes("//img"); if (images != null) { foreach (HtmlNode img in images) { string imgFile = img.Attributes["src"]?.Value as string; if (imgFile == null) { continue; } if (!imgFile.StartsWith("http://") && !imgFile.StartsWith("https://")) { imgFile = Path.Combine(basePath, imgFile.Replace("/", "\\")); if (File.Exists(imgFile)) { var media = new MediaObject() { Type = "application/image", Bits = File.ReadAllBytes(imgFile), Name = baseName + "/" + Path.GetFileName(imgFile) }; var mediaResult = wrapper.NewMediaObject(media); img.Attributes["src"].Value = mediaResult.URL; // use first image as featured image if (string.IsNullOrEmpty(metaData.FeaturedImageUrl)) { metaData.FeaturedImageUrl = mediaResult.URL; } } } } html = doc.DocumentNode.OuterHtml; } } catch (Exception ex) { MessageBox.Show("Error posting images to Weblog: " + ex.Message, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); mmApp.Log(ex); return(null); } return(html); }
public void RawPostMetaWeblogTest() { var rawPost = new Post() { Body = "<b>Markdown Text</b>", DateCreated = DateTime.UtcNow, mt_keywords = "Test,NewPost", CustomFields = new CustomField[] { new CustomField() { ID = Guid.NewGuid().ToString(), Key = "mt_Markdown", Value = "**Markdown Text**" } }, PostID = 0, Title = "Testing a post" }; WeblogInfo weblogInfo = WeblogAddinConfiguration.Current.Weblogs[ConstWeblogName]; var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password); rawPost.PostID = wrapper.NewPost(rawPost, true); }
private void CreateDownloadedPost() { var item = ListViewPosts.SelectedItem as Post; if (item == null) { return; } string postId = item.PostID.ToString(); WeblogInfo weblogInfo = Model.ActiveWeblogInfo; var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password); Post post = null; try { post = wrapper.GetPost(postId); } catch (Exception ex) { MessageBox.Show("Unable to download post.\r\n\r\n" + ex.Message); return; } Model.Addin.CreateDownloadedPostOnDisk(post, weblogInfo.Name); Close(); }
private async Task CreateDownloadedPost() { var item = ListViewPosts.SelectedItem as Post; if (item == null) { return; } StatusBar.ShowStatusProgress("Downloading Weblog post '" + item.Title + "'"); string postId = item.PostId.ToString(); WeblogInfo weblogInfo = Model.ActiveWeblogInfo; Post post = null; if (weblogInfo.Type == WeblogTypes.MetaWeblogApi) { var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, mmApp.DecryptString(weblogInfo.Password), weblogInfo.BlogId); try { post = await Task.Run(() => wrapper.GetPost(postId)); } catch (Exception ex) { StatusBar.ShowStatus(); MessageBox.Show("Unable to download post.\r\n\r\n" + ex.Message); return; } } else { var wrapper = new WordPressWrapper(weblogInfo.ApiUrl, weblogInfo.Username, mmApp.DecryptString(weblogInfo.Password)); try { post = wrapper.GetPost(postId); } catch (Exception ex) { StatusBar.ShowStatus(); MessageBox.Show("Unable to download post.\r\n\r\n" + ex.Message); return; } } Model.Addin.CreateDownloadedPostOnDisk(post, weblogInfo.Name); Close(); StatusBar.ShowStatus(); }
static void PublishAll(Object obj) { working = true; MainWindowVM mwvm = (MainWindowVM)Application.Current.MainWindow.DataContext; (((MainWindow)obj).sb.pb).Visibility = Visibility.Visible; MetaWeblogWrapper.PublishAll(mwvm.SelectedTab.Module, ((MainWindow)obj).sb.pb); (((MainWindow)obj).sb.pb).Visibility = Visibility.Collapsed; working = false; }
async void publish(Object obj) { ListView lv = (ListView)obj; foreach (OnlinePublishEntry cmdlet in Cmdlets) { cmdlet.Status = OnlinePublishStatusEnum.Pending; cmdlet.StatusText = "Pending for publish"; } PbValue = 0; Blogger blogger = Utils.InitializeBlogger(module.Provider); if (blogger == null) { Utils.MsgBox("Warning", Strings.WarnBloggerNeedsMoreData, MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } Double duration = 100.0 / Cmdlets.Count; PbValue = 0; foreach (OnlinePublishEntry cmdlet in Cmdlets) { lv.ScrollIntoView(cmdlet); if (cmdlet.Cmdlet.Publish) { try { await MetaWeblogWrapper.PublishSingle(cmdlet.Cmdlet, module, blogger, true); cmdlet.Status = OnlinePublishStatusEnum.Succeed; cmdlet.StatusText = "The operation completed successfully."; } catch (Exception e) { cmdlet.Status = OnlinePublishStatusEnum.Failed; cmdlet.StatusText = e.Message; } } else { cmdlet.Status = OnlinePublishStatusEnum.Skipped; cmdlet.StatusText = "The item is not configured for publishing"; } PbValue += duration; } PbValue = 100; }
public void GetRecentPosts() { WeblogInfo weblogInfo = WeblogAddinConfiguration.Current.Weblogs[ConstWeblogName]; var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password); var posts = wrapper.GetRecentPosts(2).ToList(); Assert.IsTrue(posts.Count == 2); foreach (var post in posts) { Console.WriteLine(post.Title + " " + post.DateCreated); } }
static async void PublishSingle(Object obj) { if (obj == null) { return; } working = true; var mwvm = (MainWindowVM)Application.Current.MainWindow.DataContext; try { await MetaWeblogWrapper.PublishSingle((CmdletObject)obj, mwvm.SelectedTab.Module, null, false); Utils.MsgBox("Success", "The operation completed successfully.", MessageBoxImage.Information); } catch (Exception e) { Utils.MsgBox("Error", e.Message); } working = false; }
public void GetRecentPost() { WeblogInfo weblogInfo = WeblogAddinConfiguration.Current.Weblogs[ConstWeblogName]; var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password); var posts = wrapper.GetRecentPosts(2).ToList(); Assert.IsTrue(posts.Count == 2); var postId = posts[0].PostID; var post = wrapper.GetPost(postId.ToString()); Assert.IsNotNull(post); Console.WriteLine(post.Title); // markdown Console.WriteLine(post.CustomFields?[0].Value); }
async void FetchClick(object Sender, RoutedEventArgs e) { if (_mwvm.SelectedTab.Module.Provider == null) { return; } List <Post> posts = await MetaWeblogWrapper.GetRecentPosts(blogger, _mwvm.SelectedTab.Module.Cmdlets, providerInfo.FetchPostCount); foreach (CmdletObject cmdlet in _mwvm.SelectedTab.Module.Cmdlets) { Int32 index = posts.IndexOf(new Post { Title = cmdlet.Name }); if (index >= 0) { cmdlet.ArticleIDString = posts[index].PostId; cmdlet.URL = _mwvm.SelectedTab.Module.Provider.ProviderName.ToLower() == "codeplex" ? _mwvm.SelectedTab.Module.Provider.Blog.url + "wikipage?title=" + cmdlet.Name : posts[index].Permalink; } } }
public void GetCategories() { WeblogInfo weblogInfo = WeblogAddinConfiguration.Current.Weblogs[ConstWeblogName]; var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password); var categoryStrings = new List <string>(); var categories = wrapper.GetCategories(); foreach (var cat in categories) { categoryStrings.Add(cat.Description); } Assert.IsTrue(categoryStrings.Count > 0); foreach (string cat in categoryStrings) { Console.WriteLine(cat); } }
private async void Button_DownloadPosts_Click(object sender, RoutedEventArgs e) { WeblogInfo weblogInfo = Model.ActiveWeblogInfo; var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.DecryptPassword(weblogInfo.Password), weblogInfo.BlogId); Model.Configuration.LastWeblogAccessed = weblogInfo.Name; Dispatcher.Invoke(() => { Model.PostList = new List<Post>(); SetStatusIcon(FontAwesomeIcon.Download, Colors.Orange,true); ShowStatus("Downloading last " + Model.NumberOfPostsToRetrieve + " posts..."); }); WindowUtilities.DoEvents(); List<Post> posts = null; try { bool result = await Task.Run(() => { posts = wrapper.GetRecentPosts(Model.NumberOfPostsToRetrieve).ToList(); return false; }); } catch (XmlRpcException ex) { string message = ex.Message; if (message == "Not Found") message = "Invalid Blog API Url:\r\n" + weblogInfo.ApiUrl; MessageBox.Show("Unable to download posts:\r\n" + message,mmApp.ApplicationName, MessageBoxButton.OK,MessageBoxImage.Warning); return; } catch(Exception ex) { MessageBox.Show("Unable to download posts:\r\n" + ex.Message,mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Warning); return; } for (int i = 0; i < posts.Count; i++) { var post = posts[i]; post.mt_excerpt = StringUtils.TextAbstract(post.mt_excerpt, 220); } WindowUtilities.DoEvents(); Dispatcher.Invoke(() => { ShowStatus(posts.Count + " posts downloaded.",5000); SetStatusIcon(); Model.PostList = posts; }); }
private async Task CreateDownloadedPost() { var item = ListViewPosts.SelectedItem as Post; if (item == null) { return; } WeblogInfo weblogInfo = Model.ActiveWeblogInfo; StatusBar.ShowStatusProgress("Downloading Weblog post '" + item.Title + "'"); string postId = item.PostId?.ToString(); Post post = null; if (weblogInfo.Type == WeblogTypes.MetaWeblogApi) { var wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, mmApp.DecryptString(weblogInfo.Password), weblogInfo.BlogId); try { post = await Task.Run(() => wrapper.GetPost(postId)); } catch (Exception ex) { StatusBar.ShowStatusError("Unable to download post.\r\n\r\n" + ex.Message); return; } Model.Addin.CreateDownloadedPostOnDisk(post, weblogInfo.Name); } else if (weblogInfo.Type == WeblogTypes.Wordpress) { var wrapper = new WordPressWrapper(weblogInfo.ApiUrl, weblogInfo.Username, mmApp.DecryptString(weblogInfo.Password)); try { post = wrapper.GetPost(postId); } catch (Exception ex) { StatusBar.ShowStatus(); StatusBar.ShowStatusError("Unable to download post.\r\n\r\n" + ex.Message); return; } Model.Addin.CreateDownloadedPostOnDisk(post, weblogInfo.Name); } else if (weblogInfo.Type == WeblogTypes.LocalJekyll) { var pub = new LocalJekyllPublisher(null, weblogInfo, null); post = pub.GetPost(postId); if (post == null) { StatusBar.ShowStatusError("Unable to import post from Jekyll."); return; } string outputFile = pub.CreateDownloadedPostOnDisk(post, weblogInfo.Name); mmApp.Model.Window.OpenTab(outputFile); mmApp.Model.Window.ShowFolderBrowser(folder: Path.GetDirectoryName(outputFile)); } Close(); StatusBar.ShowStatusSuccess("Post has been imported into Markdown Monster Web log Posts."); }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogTypes type = WeblogTypes.Unknown, 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); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); 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 weblogInfo = kv.Value; if (type == WeblogTypes.Unknown) { type = weblogInfo.Type; } MetaWeblogWrapper wrapper; if (type == WeblogTypes.MetaWeblogApi) { wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.DecryptPassword(weblogInfo.Password), weblogInfo.BlogId); } else { wrapper = new WordPressWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.DecryptPassword(weblogInfo.Password)); } string body; try { body = SendImages(html, doc.Filename, wrapper, meta); } catch (Exception ex) { mmApp.Log($"Error sending images to Weblog at {weblogInfo.ApiUrl}: ", ex); return(false); } if (body == null) { return(false); } ActivePost.Body = body; ActivePost.PostID = meta.PostId; var customFields = new List <CustomField>(); customFields.Add( new CustomField() { ID = "mt_markdown", Key = "mt_markdown", Value = meta.MarkdownBody }); if (!string.IsNullOrEmpty(meta.FeaturedImageUrl)) { customFields.Add( new CustomField() { ID = "wp_post_thumbnail", Key = "wp_post_thumbnail", Value = meta.FeaturedImageUrl }); } ActivePost.CustomFields = customFields.ToArray(); bool isNewPost = IsNewPost(ActivePost.PostID); try { if (!isNewPost) { wrapper.EditPost(ActivePost, !sendAsDraft); } else { ActivePost.PostID = wrapper.NewPost(ActivePost, !sendAsDraft); } } catch (Exception ex) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: ", ex); MessageBox.Show($"Error sending post to Weblog: " + ex.Message, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); mmApp.Log(ex); return(false); } 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 { try { var postRaw = wrapper.GetPostRaw(ActivePost.PostID); var link = postRaw.link; if (!string.IsNullOrEmpty(link) && (link.StartsWith("http://") || link.StartsWith("https://"))) { ShellUtils.GoUrl(link); } else { // just go to the base domain - assume posts are listed there ShellUtils.GoUrl(new Uri(weblogInfo.ApiUrl).GetLeftPart(UriPartial.Authority)); } } catch { } } return(true); }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogTypes type = WeblogTypes.MetaWeblogApi, 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); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); 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 weblogInfo = kv.Value; MetaWeblogWrapper wrapper; if (type == WeblogTypes.MetaWeblogApi) { wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password, weblogInfo.BlogId) as MetaWeblogWrapper; } else { wrapper = new WordPressWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password) as MetaWeblogWrapper; } string body = SendImages(html, doc.Filename, wrapper); if (body == null) { return(false); } ActivePost.Body = body; ActivePost.PostID = meta.PostId; ActivePost.CustomFields = new CustomField[1] { new CustomField() { ID = "mt_markdown", Key = "mt_markdown", Value = meta.MarkdownBody } }; bool isNewPost = IsNewPost(ActivePost.PostID); try { if (!isNewPost) { wrapper.EditPost(ActivePost, !sendAsDraft); } else { ActivePost.PostID = wrapper.NewPost(ActivePost, !sendAsDraft); } } catch (Exception ex) { MessageBox.Show("Error sending post to Weblog: " + ex.Message, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); mmApp.Log(ex); return(false); } 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); } return(true); }