예제 #1
0
        private void CreateDownloadedPost()
        {
            var item = ListViewPosts.SelectedItem as Post;

            if (item == null)
            {
                return;
            }

            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,
                                                    weblogInfo.DecryptPassword(weblogInfo.Password),
                                                    +weblogInfo.BlogId);

                try
                {
                    post = wrapper.GetPost(postId);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to download post.\r\n\r\n" + ex.Message);
                    return;
                }
            }
            else
            {
                var wrapper = new WordPressWrapper(weblogInfo.ApiUrl,
                                                   weblogInfo.Username,
                                                   weblogInfo.DecryptPassword(weblogInfo.Password));

                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();
        }
예제 #2
0
        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;
            });

                       
        }
예제 #3
0
        /// <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);
        }