public bool CreatePost(PostInfo postInfo) { try { var newPost = new AtomEntry(); newPost.Title.Text = postInfo.Title; newPost.Content = new AtomContent(); newPost.Content.Content = postInfo.Content; newPost.Content.Type = "xhtml"; newPost.IsDraft = postInfo.IsDraft; if(newPost.Content.Content.StartsWith("<div xmlns='http://www.w3.org/1999/xhtml'>") == false) { newPost.Content.Content = newPost.Content.Content.Insert(0, "<div xmlns='http://www.w3.org/1999/xhtml'>"); newPost.Content.Content = newPost.Content.Content += "</div>"; } newPost.Content.Content = newPost.Content.Content.Replace("’", "'"); File.WriteAllText("c:\\tmp.html", newPost.Content.Content); var blogFeedUri = new Uri("http://www.blogger.com/feeds/" + postInfo.BlogId + "/posts/default"); service.Insert(blogFeedUri, newPost); return true; } catch (Exception exception) { if(exception is GDataRequestException) { Console.WriteLine("The html is not valid: " + (exception as GDataRequestException).ResponseString); } return false; } }
private static bool PostBlogger(string[] parameters) { var options = new Dictionary<string, string>(); //-p , [-t], -id, -h or -hp for (int i = 2; i < parameters.Length; i++) { if (parameters[i].Contains("-p*") == false) { string[] keyValue = parameters[i].Split(':'); if (keyValue.Length > 2) { keyValue[1] = keyValue[1] + ":" + keyValue[2]; } if (keyValue.Length < 2) return false; keyValue[0] = keyValue[0].Trim('\"'); keyValue[1] = keyValue[1].Trim('\"'); options[keyValue[0]] = keyValue[1]; } else { options["-p"] = parameters[i].Split('*')[1]; } } if (options == null) throw new InvalidCommand("You must specify parameters like html content/path, blog id and post title"); var postInfo = new PostInfo(); string rssUrl = null; int count = 1; foreach (var keyValuePair in options) { switch (keyValuePair.Key) { case "-hp": postInfo.Content = File.ReadAllText(keyValuePair.Value); break; case "-h": postInfo.Content = keyValuePair.Value; break; case "-id": postInfo.BlogId = keyValuePair.Value; break; case "-rc": Int32.TryParse(keyValuePair.Value, out count); break; case "-t": postInfo.Title = keyValuePair.Value; break; case "-rss": rssUrl = keyValuePair.Value; if (rssUrl == "") break; break; case "-d": postInfo.IsDraft = true; break; case "-p": postInfo.Parse = true; postInfo.Args = keyValuePair.Value.Trim('\"').Split('+'); break; default: throw new InvalidCommand("Unknown parameter: " + keyValuePair.Key); } } if (postInfo.BlogId == null) { Console.WriteLine("Choose a blog:"); postInfo.BlogId = GetBlogs(true); } if (postInfo.Content == null || postInfo.BlogId == null) throw new InvalidCommand("You must specify all the parameters: content, blog id and post title"); string htmlContent = string.Empty; if (string.IsNullOrEmpty(rssUrl) == false) { string template = postInfo.Content; foreach (Item item in ParseRssFeed(rssUrl, count)) { htmlContent += ParseHtml(template, new[] {item.Title, item.Description, item.Link}); } postInfo.Content = htmlContent; return _apiManager.BloggerClient.CreatePost(postInfo); } if (postInfo.Parse) { postInfo.Content = ParseHtml(postInfo.Content, postInfo.Args); } return _apiManager.BloggerClient.CreatePost(postInfo); }