using System;
using System.Collections.Generic;
using System.Linq;
-using System.Text;
+using System.Net;
+using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
private static XDocument CreatePostEntryXml(string title, string content, DateTime? updated)
{
var elm = new XElement(XmlNamespaces.Atom02Spec + "entry",
- new XElement(XmlNamespaces.Atom02Spec + "title", title),
- new XElement(XmlNamespaces.Atom02Spec + "content", new XAttribute("type", "text/plain"), content));
+ new XElement(XmlNamespaces.Atom02Spec + "title", title),
+ new XElement(XmlNamespaces.Atom02Spec + "content", new XAttribute("type", "text/plain"), content));
if (updated.HasValue)
elm.Add(new XElement(XmlNamespaces.Atom02Spec + "updated", updated.Value.ToString("o")));
return new XDocument(elm);
public BlogEntry EditEntry(string dateId, string title, string content, DateTime? updated = null)
{
var reXml = WsseAtomConnection.Put(
- string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
- CreatePostEntryXml(title, content, updated),
- this.userName,
- this.password);
+ string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
+ CreatePostEntryXml(title, content, updated),
+ this.userName,
+ this.password);
return new BlogEntry(reXml.Root)
{
HatenaSyntax = content
this.userName,
this.password);
}
+
+ public Tuple<string, IEnumerable<BlogEntry>> GetDrafts(int page = 1)
+ {
+ var xdoc = WsseAtomConnection.Get(
+ string.Format("http://d.hatena.ne.jp/{0}/atom/draft?page={1}", this.userName, page),
+ this.userName,
+ this.password);
+ var title = xdoc.Root.Element(XmlNamespaces.Atom + "title").Value;
+ var entrys = xdoc.Root.Elements(XmlNamespaces.Atom + "entry").Select(_ => new BlogEntry(_));
+ return Tuple.Create(title, entrys);
+ }
+
+ public BlogEntry PostDraft(string title, string content, DateTime? updated = null)
+ {
+ var reXml = WsseAtomConnection.Post(
+ string.Format("http://d.hatena.ne.jp/{0}/atom/draft", this.userName),
+ CreatePostEntryXml(title, content, updated),
+ this.userName,
+ this.password);
+ return new BlogEntry(reXml.Root)
+ {
+ HatenaSyntax = content
+ };
+ }
+
+ public BlogEntry GetDraft(string id)
+ {
+ var xdoc = WsseAtomConnection.Get(
+ string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
+ this.userName,
+ this.password);
+ return new BlogEntry(xdoc.Root);
+ }
+
+ public BlogEntry EditDraft(string id, string title, string content, DateTime? updated = null)
+ {
+ var reXml = WsseAtomConnection.Put(
+ string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
+ CreatePostEntryXml(title, content, updated),
+ this.userName,
+ this.password);
+ return new BlogEntry(reXml.Root)
+ {
+ HatenaSyntax = content
+ };
+ }
+
+ public void DeleteDraft(string id)
+ {
+ WsseAtomConnection.Delete(
+ string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
+ this.userName,
+ this.password);
+ }
+
+ public string PublishDraft(string id)
+ {
+ var req = (HttpWebRequest)WebRequest.Create(string.Format(
+ "http://d.hatena.ne.jp/{0}/atom/draft/{1}",
+ this.userName,
+ id));
+ req.Method = "PUT";
+ req.AllowAutoRedirect = false;
+ req.Headers.Add(WsseAtomConnection.CreateHeader(this.userName, this.password));
+ req.Headers.Add("X-HATENA-PUBLISH: 1");
+ using (var res = req.GetResponse())
+ return Regex.Match(
+ res.Headers[HttpResponseHeader.Location],
+ @"\d+/\d+"
+ )
+ .ToString();
+ }
}
}