OSDN Git Service

280738ed600654b30c4a2a2af6c108f4a8c0dfd7
[futonwriter/old_trunk.git] / HatenaDiaryClient / Models / Hatena / HatenaDiary.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml.Linq;
6
7 namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
8 {
9     public class HatenaDiary
10     {
11         private string userName;
12         private string password;
13
14         public HatenaDiary(string userName, string password)
15         {
16             this.userName = userName;
17             this.password = password;
18         }
19
20         public Tuple<string, IEnumerable<BlogEntry>> GetEntrys(int page)
21         {
22             var xdoc = WsseAtomConnection.Get(
23                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog?page={1}", this.userName, page),
24                 this.userName,
25                 this.password);
26             var title = xdoc.Root.Element(XmlNamespaces.Atom + "title").Value;
27             var entrys = xdoc.Root.Elements(XmlNamespaces.Atom + "entry").Select(_ => new BlogEntry(_));
28             return Tuple.Create(title, entrys);
29         }
30
31         public BlogEntry PostEntry(string title, string content, DateTime? updated = null)
32         {
33             var elm = new XElement(XmlNamespaces.Atom02Spec + "entry",
34                 new XElement(XmlNamespaces.Atom02Spec + "title", title),
35                 new XElement(XmlNamespaces.Atom02Spec + "content", new XAttribute("type", "text/plain"), content));
36             if (updated.HasValue)
37                 elm.Add(new XElement(XmlNamespaces.Atom02Spec + "updated", updated.Value.ToString("o")));
38             var reXml = WsseAtomConnection.Post(
39                 string.Format("http://d.hatena.ne.jp/{0}/atom/blog", this.userName),
40                 new XDocument(elm),
41                 this.userName,
42                 this.password);
43             return new BlogEntry(reXml.Root)
44             {
45                 HatenaSyntax = content
46             };
47         }
48     }
49 }