開発に使用するリポジトリ
Révision | d1ef3ad260e41a13b32cd276d10cf5caadf5cd7a (tree) |
---|---|
l'heure | 2013-06-13 20:07:06 |
Auteur | ANIKITI <anikiti07@gmai...> |
Commiter | ANIKITI |
画像アップロード対応サービスについっぷるフォトを追加
@@ -0,0 +1,185 @@ | ||
1 | +// OpenTween - Client of Twitter | |
2 | +// Copyright (c) 2013 ANIKITI (@anikiti07) <https://twitter.com/anikiti07> | |
3 | +// All rights reserved. | |
4 | +// | |
5 | +// This file is part of OpenTween. | |
6 | +// | |
7 | +// This program is free software; you can redistribute it and/or modify it | |
8 | +// under the terms of the GNU General Public License as published by the Free | |
9 | +// Software Foundation; either version 3 of the License, or (at your option) | |
10 | +// any later version. | |
11 | +// | |
12 | +// This program is distributed in the hope that it will be useful, but | |
13 | +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
14 | +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | +// for more details. | |
16 | +// | |
17 | +// You should have received a copy of the GNU General Public License along | |
18 | +// with this program. If not, see <http://www.gnu.org/licenses/>, or write to | |
19 | +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | |
20 | +// Boston, MA 02110-1301, USA. | |
21 | + | |
22 | +using System; | |
23 | +using System.Collections.Generic; | |
24 | +using System.IO; | |
25 | +using System.Linq; | |
26 | +using System.Net; | |
27 | +using System.Text; | |
28 | +using System.Xml; | |
29 | + | |
30 | +namespace OpenTween.Connection | |
31 | +{ | |
32 | + public sealed class TwipplePhoto : HttpConnectionOAuthEcho, | |
33 | + IMultimediaShareService | |
34 | + { | |
35 | + private const string TwipplePhotoUploadEndpointV2 = "http://p.twipple.jp/api/upload2"; | |
36 | + private const long MaxFileSize = 4 * 1024 * 1024; | |
37 | + | |
38 | + private readonly Twitter _twitter; | |
39 | + private readonly IEnumerable<string> _pictureExtensions = new[] | |
40 | + { | |
41 | + ".gif", | |
42 | + ".jpg", | |
43 | + ".png" | |
44 | + }; | |
45 | + | |
46 | + #region Constructors | |
47 | + | |
48 | + public TwipplePhoto(Twitter twitter) | |
49 | + : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json")) | |
50 | + { | |
51 | + if (twitter == null) | |
52 | + throw new ArgumentNullException("twitter"); | |
53 | + | |
54 | + _twitter = twitter; | |
55 | + Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret, | |
56 | + _twitter.AccessToken, _twitter.AccessTokenSecret, | |
57 | + "", ""); | |
58 | + } | |
59 | + | |
60 | + #endregion | |
61 | + | |
62 | + #region IMultimediaShareService Members | |
63 | + | |
64 | + #region Upload Methods | |
65 | + | |
66 | + public string Upload(ref string filePath, ref string message, long reply_to) | |
67 | + { | |
68 | + if (!File.Exists(filePath)) | |
69 | + return "Err:File isn't exists."; | |
70 | + | |
71 | + var mediaFile = new FileInfo(filePath); | |
72 | + var content = ""; | |
73 | + HttpStatusCode result; | |
74 | + try | |
75 | + { | |
76 | + result = UploadFile(mediaFile, ref content); | |
77 | + } | |
78 | + catch (Exception ex) | |
79 | + { | |
80 | + return "Err:" + ex.Message; | |
81 | + } | |
82 | + | |
83 | + var imageUrl = ""; | |
84 | + if (result == HttpStatusCode.OK) | |
85 | + { | |
86 | + try | |
87 | + { | |
88 | + var xdoc = new XmlDocument(); | |
89 | + xdoc.LoadXml(content); | |
90 | + var urlNode = xdoc.SelectSingleNode("/rsp/mediaurl"); | |
91 | + if (urlNode != null) | |
92 | + { | |
93 | + imageUrl = urlNode.InnerText; | |
94 | + } | |
95 | + } | |
96 | + catch (XmlException ex) | |
97 | + { | |
98 | + return "XmlErr:" + ex.Message; | |
99 | + } | |
100 | + } | |
101 | + else | |
102 | + { | |
103 | + return "Err:" + result; | |
104 | + } | |
105 | + | |
106 | + filePath = ""; | |
107 | + if (message == null) | |
108 | + message = ""; | |
109 | + | |
110 | + // Post to twitter | |
111 | + if (message.Length + AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia + 1 > 140) | |
112 | + { | |
113 | + message = message.Substring(0, 140 - AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia - 1) + " " + imageUrl; | |
114 | + } | |
115 | + else | |
116 | + { | |
117 | + message += " " + imageUrl; | |
118 | + } | |
119 | + return _twitter.PostStatus(message, reply_to); | |
120 | + } | |
121 | + | |
122 | + private HttpStatusCode UploadFile(FileInfo mediaFile, ref string content) | |
123 | + { | |
124 | + if (!CheckValidExtension(mediaFile.Extension)) | |
125 | + throw new ArgumentException("Service don't support this filetype", "mediaFile"); | |
126 | + if (!CheckValidFilesize(mediaFile.Extension, mediaFile.Length)) | |
127 | + throw new ArgumentException("File is too large", "mediaFile"); | |
128 | + | |
129 | + var binaly = new List<KeyValuePair<string, FileInfo>> | |
130 | + { | |
131 | + new KeyValuePair<string, FileInfo>("media", mediaFile) | |
132 | + }; | |
133 | + InstanceTimeout = 60000; | |
134 | + | |
135 | + return GetContent(PostMethod, new Uri(TwipplePhotoUploadEndpointV2), null, binaly, ref content, null, null); | |
136 | + } | |
137 | + | |
138 | + #endregion | |
139 | + | |
140 | + public bool CheckValidExtension(string ext) | |
141 | + { | |
142 | + return _pictureExtensions.Contains(ext, StringComparer.InvariantCultureIgnoreCase); | |
143 | + } | |
144 | + | |
145 | + public string GetFileOpenDialogFilter() | |
146 | + { | |
147 | + var filter = new StringBuilder("Image Files(", 128); | |
148 | + var sb = new StringBuilder(64); | |
149 | + foreach (var photoExtension in _pictureExtensions) | |
150 | + { | |
151 | + sb.Append('*'); | |
152 | + sb.Append(photoExtension); | |
153 | + sb.Append(';'); | |
154 | + } | |
155 | + filter.Append(sb); | |
156 | + filter.Append(")|"); | |
157 | + filter.Append(sb); | |
158 | + return filter.ToString(); | |
159 | + } | |
160 | + | |
161 | + public MyCommon.UploadFileType GetFileType(string ext) | |
162 | + { | |
163 | + return CheckValidExtension(ext) | |
164 | + ? MyCommon.UploadFileType.Picture | |
165 | + : MyCommon.UploadFileType.Invalid; | |
166 | + } | |
167 | + | |
168 | + public bool IsSupportedFileType(MyCommon.UploadFileType type) | |
169 | + { | |
170 | + return type == MyCommon.UploadFileType.Picture; | |
171 | + } | |
172 | + | |
173 | + public bool CheckValidFilesize(string ext, long fileSize) | |
174 | + { | |
175 | + return CheckValidExtension(ext) && fileSize <= MaxFileSize; | |
176 | + } | |
177 | + | |
178 | + public bool Configuration(string key, object value) | |
179 | + { | |
180 | + throw new NotImplementedException(); | |
181 | + } | |
182 | + | |
183 | + #endregion | |
184 | + } | |
185 | +} | |
\ No newline at end of file |
@@ -90,6 +90,7 @@ | ||
90 | 90 | <Compile Include="Connection\IHttpConnection.cs" /> |
91 | 91 | <Compile Include="Connection\imgly.cs" /> |
92 | 92 | <Compile Include="Connection\IMultimediaShareService.cs" /> |
93 | + <Compile Include="Connection\TwipplePhoto.cs" /> | |
93 | 94 | <Compile Include="EventViewerDialog.cs"> |
94 | 95 | <SubType>Form</SubType> |
95 | 96 | </Compile> |
@@ -1,6 +1,7 @@ | ||
1 | 1 | 更新履歴 |
2 | 2 | |
3 | 3 | ==== Ver 1.1.2-beta1(2013/xx/xx) |
4 | + * NEW: 画像アップロード対応サービスについっぷるフォトを追加 | |
4 | 5 | * CHG: 画像アップロード対応サービスから Lockerz を削除 (API廃止のため) |
5 | 6 | * FIX: Twitpic, yfrog, img.ly に画像をアップロードすることができない問題を修正 |
6 | 7 |
@@ -31,23 +31,23 @@ | ||
31 | 31 | using System; |
32 | 32 | using System.Collections.Generic; |
33 | 33 | using System.ComponentModel; |
34 | -using System.Data; | |
34 | +using System.Diagnostics; | |
35 | 35 | using System.Drawing; |
36 | +using System.IO; | |
36 | 37 | using System.Linq; |
38 | +using System.Media; | |
39 | +using System.Net; | |
40 | +using System.Reflection; | |
37 | 41 | using System.Text; |
38 | -using System.Windows.Forms; | |
39 | -using OpenTween.OpenTweenCustomControl; | |
40 | -using System.IO; | |
41 | 42 | using System.Text.RegularExpressions; |
42 | -using System.Reflection; | |
43 | 43 | using System.Threading; |
44 | -using System.Media; | |
45 | -using System.Web; | |
46 | -using System.Diagnostics; | |
47 | -using OpenTween.Thumbnail; | |
48 | 44 | using System.Threading.Tasks; |
49 | -using System.Net; | |
45 | +using System.Windows.Forms; | |
46 | + | |
50 | 47 | using OpenTween.Api; |
48 | +using OpenTween.Connection; | |
49 | +using OpenTween.OpenTweenCustomControl; | |
50 | +using OpenTween.Thumbnail; | |
51 | 51 | |
52 | 52 | namespace OpenTween |
53 | 53 | { |
@@ -1294,7 +1294,8 @@ namespace OpenTween | ||
1294 | 1294 | {"TwitPic", new TwitPic(tw)}, |
1295 | 1295 | {"img.ly", new imgly(tw)}, |
1296 | 1296 | {"yfrog", new yfrog(tw)}, |
1297 | - {"Twitter", new TwitterPhoto(tw)} | |
1297 | + {"Twitter", new TwitterPhoto(tw)}, | |
1298 | + {"ついっぷるフォト", new TwipplePhoto(tw)} | |
1298 | 1299 | }; |
1299 | 1300 | } |
1300 | 1301 |
@@ -12466,10 +12467,12 @@ namespace OpenTween | ||
12466 | 12467 | string svc = ""; |
12467 | 12468 | if (ImageServiceCombo.SelectedIndex > -1) svc = ImageServiceCombo.SelectedItem.ToString(); |
12468 | 12469 | ImageServiceCombo.Items.Clear(); |
12469 | - ImageServiceCombo.Items.Add("TwitPic"); | |
12470 | - ImageServiceCombo.Items.Add("img.ly"); | |
12471 | - ImageServiceCombo.Items.Add("yfrog"); | |
12472 | - ImageServiceCombo.Items.Add("Twitter"); | |
12470 | + | |
12471 | + // Add service names to combobox | |
12472 | + foreach (var key in pictureService.Keys) | |
12473 | + { | |
12474 | + ImageServiceCombo.Items.Add(key); | |
12475 | + } | |
12473 | 12476 | |
12474 | 12477 | if (string.IsNullOrEmpty(svc)) |
12475 | 12478 | { |