OSDN Git Service

下書きの実装完了
authorazyobuzin <azyobuzin@users.sourceforge.jp>
Mon, 9 May 2011 09:15:23 +0000 (18:15 +0900)
committerazyobuzin <azyobuzin@users.sourceforge.jp>
Mon, 9 May 2011 09:15:23 +0000 (18:15 +0900)
HatenaDiaryClient/Models/Hatena/BlogEntry.cs
HatenaDiaryClient/Models/Hatena/HatenaDiary.cs
HatenaDiaryClient/Models/Hatena/WsseAtomConnection.cs

index abfaf7f..19ce6a4 100644 (file)
@@ -1,7 +1,5 @@
 using System;
-using System.Collections.Generic;
 using System.Linq;
-using System.Text;
 using System.Text.RegularExpressions;
 using System.Xml.Linq;
 
@@ -29,7 +27,13 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
             this.HatenaSyntax = xml.Elements(XmlNamespaces.HatenaNs + "syntax")
                 .Select(_ => _.Value)
                 .SingleOrDefault();
-            this.DateId = Regex.Match(this.BlogPage, @"\d+/\d+$").ToString();
+            this.DateId = Regex.Match(
+                xml.Elements(XmlNamespaces.Atom + "link")
+                    .Where(_ => _.Attribute("rel").Value == "edit")
+                    .Select(_ => _.Attribute("href").Value)
+                    .First(),
+                @"(\d+/\d+|\d+)$"
+            ).ToString();
         }
     }
 }
index 2f759f3..00539ef 100644 (file)
@@ -1,7 +1,8 @@
 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
@@ -31,8 +32,8 @@ 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);
@@ -63,10 +64,10 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
         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
@@ -80,5 +81,77 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
                 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();
+        }
     }
 }
index 7abe19e..93990f4 100644 (file)
@@ -10,7 +10,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
     {
         private static Random random = new Random();
 
-        private static string CreateHeader(string userName, string password)
+        public static string CreateHeader(string userName, string password)
         {
             var tmp = new byte[25];
             random.NextBytes(tmp);