OSDN Git Service

・DateIdとかいうプロパティを追加
authorazyobuzin <azyobuzin@users.sourceforge.jp>
Mon, 9 May 2011 08:31:07 +0000 (17:31 +0900)
committerazyobuzin <azyobuzin@users.sourceforge.jp>
Mon, 9 May 2011 08:31:07 +0000 (17:31 +0900)
・記事をいじるメソッドを揃えた

HatenaDiaryClient/Models/Hatena/BlogEntry.cs
HatenaDiaryClient/Models/Hatena/HatenaDiary.cs
HatenaDiaryClient/Models/Hatena/WsseAtomConnection.cs

index ba699bb..abfaf7f 100644 (file)
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Xml.Linq;
 
 namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
@@ -13,20 +14,22 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
         public string Title { set; get; }
         public string Html { set; get; }
         public string HatenaSyntax { set; get; }
+        public string DateId { set; get; }
 
         public BlogEntry() { }
         public BlogEntry(XElement xml)
         {
-            Edited = DateTime.Parse(xml.Element(XmlNamespaces.AtomPub + "edited").Value);
-            BlogPage = xml.Elements(XmlNamespaces.Atom + "link")
+            this.Edited = DateTime.Parse(xml.Element(XmlNamespaces.AtomPub + "edited").Value);
+            this.BlogPage = xml.Elements(XmlNamespaces.Atom + "link")
                 .Where(_ => _.Attribute("rel").Value == "alternate")
                 .Select(_ => _.Attribute("href").Value)
                 .FirstOrDefault();
-            Title = xml.Element(XmlNamespaces.Atom + "title").Value;
-            Html = xml.Element(XmlNamespaces.Atom + "content").Value;
-            HatenaSyntax = xml.Elements(XmlNamespaces.HatenaNs + "syntax")
+            this.Title = xml.Element(XmlNamespaces.Atom + "title").Value;
+            this.Html = xml.Element(XmlNamespaces.Atom + "content").Value;
+            this.HatenaSyntax = xml.Elements(XmlNamespaces.HatenaNs + "syntax")
                 .Select(_ => _.Value)
                 .SingleOrDefault();
+            this.DateId = Regex.Match(this.BlogPage, @"\d+/\d+$").ToString();
         }
     }
 }
index 280738e..2f759f3 100644 (file)
@@ -17,7 +17,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
             this.password = password;
         }
 
-        public Tuple<string, IEnumerable<BlogEntry>> GetEntrys(int page)
+        public Tuple<string, IEnumerable<BlogEntry>> GetEntrys(int page = 1)
         {
             var xdoc = WsseAtomConnection.Get(
                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog?page={1}", this.userName, page),
@@ -28,16 +28,21 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
             return Tuple.Create(title, entrys);
         }
 
-        public BlogEntry PostEntry(string title, string content, DateTime? updated = null)
+        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 PostEntry(string title, string content, DateTime? updated = null)
+        {
             var reXml = WsseAtomConnection.Post(
                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog", this.userName),
-                new XDocument(elm),
+                CreatePostEntryXml(title, content, updated),
                 this.userName,
                 this.password);
             return new BlogEntry(reXml.Root)
@@ -45,5 +50,35 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
                 HatenaSyntax = content
             };
         }
+
+        public BlogEntry GetEntry(string dateId)
+        {
+            var xdoc = WsseAtomConnection.Get(
+                string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
+                this.userName,
+                this.password);
+            return new BlogEntry(xdoc.Root);
+        }
+
+        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);
+            return new BlogEntry(reXml.Root)
+            {
+                HatenaSyntax = content
+            };
+        }
+
+        public void DeleteEntry(string dateId)
+        {
+            WsseAtomConnection.Delete(
+                string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
+                this.userName,
+                this.password);
+        }
     }
 }
index b9d26dd..7abe19e 100644 (file)
@@ -49,7 +49,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
             var req = WebRequest.Create(reqUri);
             req.Method = method;
             req.Headers.Add(CreateHeader(userName, password));
-            req.ContentType = "application/x.atom+xml";
+            req.ContentType = "application/x.atom+xml; charset=utf-8";
             var bs = Encoding.UTF8.GetBytes(reqData.ToString());
             req.ContentLength = bs.Length;
             using (var stream = req.GetRequestStream())