using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.Text.RegularExpressions;
using System.Xml.Linq;
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();
}
}
}
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),
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)
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);
+ }
}
}