OSDN Git Service

00539ef529f49b734118d571602740c426b63fd1
[futonwriter/old_trunk.git] / HatenaDiaryClient / Models / Hatena / HatenaDiary.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Text.RegularExpressions;
6 using System.Xml.Linq;
7
8 namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
9 {
10     public class HatenaDiary
11     {
12         private string userName;
13         private string password;
14
15         public HatenaDiary(string userName, string password)
16         {
17             this.userName = userName;
18             this.password = password;
19         }
20
21         public Tuple<string, IEnumerable<BlogEntry>> GetEntrys(int page = 1)
22         {
23             var xdoc = WsseAtomConnection.Get(
24                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog?page={1}", this.userName, page),
25                 this.userName,
26                 this.password);
27             var title = xdoc.Root.Element(XmlNamespaces.Atom + "title").Value;
28             var entrys = xdoc.Root.Elements(XmlNamespaces.Atom + "entry").Select(_ => new BlogEntry(_));
29             return Tuple.Create(title, entrys);
30         }
31
32         private static XDocument CreatePostEntryXml(string title, string content, DateTime? updated)
33         {
34             var elm = new XElement(XmlNamespaces.Atom02Spec + "entry",
35                 new XElement(XmlNamespaces.Atom02Spec + "title", title),
36                 new XElement(XmlNamespaces.Atom02Spec + "content", new XAttribute("type", "text/plain"), content));
37             if (updated.HasValue)
38                 elm.Add(new XElement(XmlNamespaces.Atom02Spec + "updated", updated.Value.ToString("o")));
39             return new XDocument(elm);
40         }
41
42         public BlogEntry PostEntry(string title, string content, DateTime? updated = null)
43         {
44             var reXml = WsseAtomConnection.Post(
45                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog", this.userName),
46                 CreatePostEntryXml(title, content, updated),
47                 this.userName,
48                 this.password);
49             return new BlogEntry(reXml.Root)
50             {
51                 HatenaSyntax = content
52             };
53         }
54
55         public BlogEntry GetEntry(string dateId)
56         {
57             var xdoc = WsseAtomConnection.Get(
58                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
59                 this.userName,
60                 this.password);
61             return new BlogEntry(xdoc.Root);
62         }
63
64         public BlogEntry EditEntry(string dateId, string title, string content, DateTime? updated = null)
65         {
66             var reXml = WsseAtomConnection.Put(
67                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
68                 CreatePostEntryXml(title, content, updated),
69                 this.userName,
70                 this.password);
71             return new BlogEntry(reXml.Root)
72             {
73                 HatenaSyntax = content
74             };
75         }
76
77         public void DeleteEntry(string dateId)
78         {
79             WsseAtomConnection.Delete(
80                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
81                 this.userName,
82                 this.password);
83         }
84
85         public Tuple<string, IEnumerable<BlogEntry>> GetDrafts(int page = 1)
86         {
87             var xdoc = WsseAtomConnection.Get(
88                 string.Format("http://d.hatena.ne.jp/{0}/atom/draft?page={1}", this.userName, page),
89                 this.userName,
90                 this.password);
91             var title = xdoc.Root.Element(XmlNamespaces.Atom + "title").Value;
92             var entrys = xdoc.Root.Elements(XmlNamespaces.Atom + "entry").Select(_ => new BlogEntry(_));
93             return Tuple.Create(title, entrys);
94         }
95         
96         public BlogEntry PostDraft(string title, string content, DateTime? updated = null)
97         {
98             var reXml = WsseAtomConnection.Post(
99                 string.Format("http://d.hatena.ne.jp/{0}/atom/draft", this.userName),
100                 CreatePostEntryXml(title, content, updated),
101                 this.userName,
102                 this.password);
103             return new BlogEntry(reXml.Root)
104             {
105                 HatenaSyntax = content
106             };
107         }
108
109         public BlogEntry GetDraft(string id)
110         {
111             var xdoc = WsseAtomConnection.Get(
112                 string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
113                 this.userName,
114                 this.password);
115             return new BlogEntry(xdoc.Root);
116         }
117
118         public BlogEntry EditDraft(string id, string title, string content, DateTime? updated = null)
119         {
120             var reXml = WsseAtomConnection.Put(
121                 string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
122                 CreatePostEntryXml(title, content, updated),
123                 this.userName,
124                 this.password);
125             return new BlogEntry(reXml.Root)
126             {
127                 HatenaSyntax = content
128             };
129         }
130
131         public void DeleteDraft(string id)
132         {
133             WsseAtomConnection.Delete(
134                 string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
135                 this.userName,
136                 this.password);
137         }
138
139         public string PublishDraft(string id)
140         {
141             var req = (HttpWebRequest)WebRequest.Create(string.Format(
142                 "http://d.hatena.ne.jp/{0}/atom/draft/{1}",
143                 this.userName,
144                 id));
145             req.Method = "PUT";
146             req.AllowAutoRedirect = false;
147             req.Headers.Add(WsseAtomConnection.CreateHeader(this.userName, this.password));
148             req.Headers.Add("X-HATENA-PUBLISH: 1");
149             using (var res = req.GetResponse())
150                 return Regex.Match(
151                     res.Headers[HttpResponseHeader.Location],
152                     @"\d+/\d+"
153                 )
154                 .ToString();
155         }
156     }
157 }