• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

http://sourceforge.jp/projects/futonwriter/の旧リポジトリ


Commit MetaInfo

Revisionf5bf5f659df1a3d169c28cb5f9aeaae696a5fcc9 (tree)
Zeit2011-05-10 19:03:33
Autorazyobuzin <azyobuzin@user...>
Commiterazyobuzin

Log Message

フォトライフへの投稿に成功。疲れた。WsseAtomConnectionの試行錯誤の結果。
TODO:いろいろ

Ändern Zusammenfassung

Diff

--- a/HatenaDiaryClient/HatenaDiaryClient.csproj
+++ b/HatenaDiaryClient/HatenaDiaryClient.csproj
@@ -75,7 +75,9 @@
7575 <SubType>Code</SubType>
7676 </Compile>
7777 <Compile Include="Models\Hatena\BlogEntry.cs" />
78+ <Compile Include="Models\Hatena\FotolifeEntry.cs" />
7879 <Compile Include="Models\Hatena\HatenaDiary.cs" />
80+ <Compile Include="Models\Hatena\HatenaFotolife.cs" />
7981 <Compile Include="Models\Hatena\WsseAtomConnection.cs" />
8082 <Compile Include="Models\Hatena\XmlNamespaces.cs" />
8183 <Compile Include="Models\Model.cs" />
--- a/HatenaDiaryClient/Models/Hatena/BlogEntry.cs
+++ b/HatenaDiaryClient/Models/Hatena/BlogEntry.cs
@@ -8,7 +8,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
88 public class BlogEntry
99 {
1010 public DateTime Edited { set; get; }
11- public string BlogPage { set; get; }
11+ public string BlogPageUri { set; get; }
1212 public string Title { set; get; }
1313 public string Html { set; get; }
1414 public string HatenaSyntax { set; get; }
@@ -18,7 +18,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
1818 public BlogEntry(XElement xml)
1919 {
2020 this.Edited = DateTime.Parse(xml.Element(XmlNamespaces.AtomPub + "edited").Value);
21- this.BlogPage = xml.Elements(XmlNamespaces.Atom + "link")
21+ this.BlogPageUri = xml.Elements(XmlNamespaces.Atom + "link")
2222 .Where(_ => _.Attribute("rel").Value == "alternate")
2323 .Select(_ => _.Attribute("href").Value)
2424 .FirstOrDefault();
--- /dev/null
+++ b/HatenaDiaryClient/Models/Hatena/FotolifeEntry.cs
@@ -0,0 +1,44 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Linq;
4+using System.Text;
5+using System.Xml.Linq;
6+using System.Text.RegularExpressions;
7+
8+namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
9+{
10+ public class FotolifeEntry
11+ {
12+ public string Title { set; get; }
13+ public string PageUri { set; get; }
14+ public string Id { set; get; }
15+ public DateTime Issued { set; get; }
16+ public string Folder { set; get; }
17+ public string ImageUri { set; get; }
18+ public string ThumbnailUri { set; get; }
19+ public string HatenaSyntax { set; get; }
20+
21+ public FotolifeEntry() { }
22+ public FotolifeEntry(XElement xml, bool isRdf)
23+ {
24+ if (isRdf)
25+ {
26+ //TODO
27+ }
28+ else
29+ {
30+ this.Title = xml.Element(XmlNamespaces.Atom02Spec + "title").Value;
31+ this.PageUri = xml.Elements(XmlNamespaces.Atom02Spec + "link")
32+ .Where(_ => _.Attribute("rel").Value == "alternate")
33+ .Select(_ => _.Attribute("href").Value)
34+ .FirstOrDefault();
35+ this.Id = Regex.Match(this.PageUri, @"\d+$").ToString();
36+ this.Issued = DateTime.Parse(xml.Element(XmlNamespaces.Atom02Spec + "issued").Value);
37+ this.Folder = xml.Element(XmlNamespaces.DublinCore + "subject").Value;
38+ this.ImageUri = xml.Element(XmlNamespaces.HatenaNs + "imageurl").Value;
39+ this.ThumbnailUri = xml.Element(XmlNamespaces.HatenaNs + "imageurlsmall").Value;
40+ this.HatenaSyntax = xml.Element(XmlNamespaces.HatenaNs + "syntax").Value;
41+ }
42+ }
43+ }
44+}
--- a/HatenaDiaryClient/Models/Hatena/HatenaDiary.cs
+++ b/HatenaDiaryClient/Models/Hatena/HatenaDiary.cs
@@ -2,6 +2,7 @@
22 using System.Collections.Generic;
33 using System.Linq;
44 using System.Net;
5+using System.Text;
56 using System.Text.RegularExpressions;
67 using System.Xml.Linq;
78
@@ -44,6 +45,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
4445 var reXml = WsseAtomConnection.Post(
4546 string.Format("http://d.hatena.ne.jp/{0}/atom/blog", this.userName),
4647 CreatePostEntryXml(title, content, updated),
48+ Encoding.UTF8,
4749 this.userName,
4850 this.password);
4951 return new BlogEntry(reXml.Root)
@@ -66,6 +68,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
6668 var reXml = WsseAtomConnection.Put(
6769 string.Format("http://d.hatena.ne.jp/{0}/atom/blog/{1}", this.userName, dateId),
6870 CreatePostEntryXml(title, content, updated),
71+ Encoding.UTF8,
6972 this.userName,
7073 this.password);
7174 return new BlogEntry(reXml.Root)
@@ -98,6 +101,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
98101 var reXml = WsseAtomConnection.Post(
99102 string.Format("http://d.hatena.ne.jp/{0}/atom/draft", this.userName),
100103 CreatePostEntryXml(title, content, updated),
104+ Encoding.UTF8,
101105 this.userName,
102106 this.password);
103107 return new BlogEntry(reXml.Root)
@@ -120,6 +124,7 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
120124 var reXml = WsseAtomConnection.Put(
121125 string.Format("http://d.hatena.ne.jp/{0}/atom/draft/{1}", this.userName, id),
122126 CreatePostEntryXml(title, content, updated),
127+ Encoding.UTF8,
123128 this.userName,
124129 this.password);
125130 return new BlogEntry(reXml.Root)
--- /dev/null
+++ b/HatenaDiaryClient/Models/Hatena/HatenaFotolife.cs
@@ -0,0 +1,90 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Linq;
4+using System.Text;
5+using System.IO;
6+using System.Xml.Linq;
7+using System.Net;
8+
9+namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
10+{
11+ public class HatenaFotolife
12+ {
13+ private string userName;
14+ private string password;
15+
16+ public HatenaFotolife(string userName, string password)
17+ {
18+ this.userName = userName;
19+ this.password = password;
20+ }
21+
22+ public FotolifeEntry Upload(string title, string fileName, string folder = null)
23+ {
24+ string mimeType;
25+ #region MIME Type判断
26+ switch (Path.GetExtension(fileName).TrimStart('.').ToLower())
27+ {
28+ case "jpg":
29+ case "jpeg":
30+ mimeType = "image/jpeg";
31+ break;
32+ case "gif":
33+ mimeType = "image/gif";
34+ break;
35+ case "png":
36+ mimeType = "image/png";
37+ break;
38+ case "bmp":
39+ mimeType = "image/x-bmp";
40+ break;
41+ case "mov":
42+ mimeType = "video/quicktime";
43+ break;
44+ case "mpg":
45+ case "mpeg":
46+ case "m1v":
47+ mimeType = "video/mpeg";
48+ break;
49+ case "wmv":
50+ mimeType = "video/x-ms-wmv";
51+ break;
52+ case "avi":
53+ mimeType = "video/avi";
54+ break;
55+ case "flv":
56+ mimeType = "video/x-flv";
57+ break;
58+ case "3gp":
59+ case "3gpp":
60+ mimeType = "video/3gpp";
61+ break;
62+ case "3g2":
63+ mimeType = "video/3gpp2";
64+ break;
65+ default:
66+ throw new ArgumentException("対応してないファイルです。");
67+ }
68+ #endregion
69+
70+ var entry = new XElement(XmlNamespaces.Atom02Spec + "entry",
71+ new XElement(XmlNamespaces.Atom02Spec + "title", title),
72+ new XElement(XmlNamespaces.Atom02Spec + "content",
73+ new XAttribute("mode", "base64"),
74+ new XAttribute("type", mimeType),
75+ Convert.ToBase64String(File.ReadAllBytes(fileName)))
76+ );
77+
78+ if (!string.IsNullOrEmpty(folder))
79+ entry.Add(new XElement(XmlNamespaces.DublinCore + "subject", folder));
80+
81+ var reXml = WsseAtomConnection.Post(
82+ "http://f.hatena.ne.jp/atom/post",
83+ new XDocument(entry),
84+ Encoding.GetEncoding("shift_jis"),
85+ this.userName,
86+ this.password);
87+ return new FotolifeEntry(reXml.Root, false);
88+ }
89+ }
90+}
--- a/HatenaDiaryClient/Models/Hatena/WsseAtomConnection.cs
+++ b/HatenaDiaryClient/Models/Hatena/WsseAtomConnection.cs
@@ -1,4 +1,5 @@
11 using System;
2+using System.Linq;
23 using System.Net;
34 using System.Security.Cryptography;
45 using System.Text;
@@ -12,18 +13,16 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
1213
1314 public static string CreateHeader(string userName, string password)
1415 {
15- var tmp = new byte[25];
16- random.NextBytes(tmp);
17- var nonce = Convert.ToBase64String(tmp);
18- tmp = null;
16+ var nonce = new byte[8];
17+ random.NextBytes(nonce);
1918 var created = DateTime.Now.ToUniversalTime().ToString("o");
2019 var digest = Convert.ToBase64String(
2120 new SHA1Managed().ComputeHash(
22- Encoding.ASCII.GetBytes(nonce + created + password)
21+ nonce.Concat(Encoding.ASCII.GetBytes(created)).Concat(Encoding.ASCII.GetBytes(password)).ToArray()
2322 ));
2423 return string.Format(
2524 @"X-WSSE:UsernameToken Username=""{0}"", PasswordDigest=""{1}"", Nonce=""{2}"", Created=""{3}""",
26- userName, digest, nonce, created);
25+ userName, digest, Convert.ToBase64String(nonce), created);
2726 }
2827
2928 public static XDocument Get(string reqUri, string userName, string password)
@@ -44,13 +43,13 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
4443 req.GetResponse().Close();
4544 }
4645
47- private static XDocument PostOrPut(string reqUri, string method, XDocument reqData, string userName, string password)
46+ private static XDocument PostOrPut(string reqUri, string method, XDocument reqData, Encoding enc, string userName, string password)
4847 {
4948 var req = WebRequest.Create(reqUri);
5049 req.Method = method;
5150 req.Headers.Add(CreateHeader(userName, password));
52- req.ContentType = "application/x.atom+xml; charset=utf-8";
53- var bs = Encoding.UTF8.GetBytes(reqData.ToString());
51+ req.ContentType = "application/x.atom+xml";
52+ var bs = Encoding.GetEncoding("shift_jis").GetBytes(reqData.ToString());
5453 req.ContentLength = bs.Length;
5554 using (var stream = req.GetRequestStream())
5655 stream.Write(bs, 0, bs.Length);
@@ -58,14 +57,14 @@ namespace Azyobuzi.HatenaDiaryClient.Models.Hatena
5857 return XDocument.Load(res.GetResponseStream());
5958 }
6059
61- public static XDocument Post(string reqUri, XDocument reqData, string userName, string password)
60+ public static XDocument Post(string reqUri, XDocument reqData, Encoding enc, string userName, string password)
6261 {
63- return PostOrPut(reqUri, "POST", reqData, userName, password);
62+ return PostOrPut(reqUri, "POST", reqData, enc, userName, password);
6463 }
6564
66- public static XDocument Put(string reqUri, XDocument reqData, string userName, string password)
65+ public static XDocument Put(string reqUri, XDocument reqData, Encoding enc, string userName, string password)
6766 {
68- return PostOrPut(reqUri, "PUT", reqData, userName, password);
67+ return PostOrPut(reqUri, "PUT", reqData, enc, userName, password);
6968 }
7069 }
7170 }
\ No newline at end of file