1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
28 using System.Threading.Tasks;
29 using System.Windows.Forms;
30 using System.Xml.Linq;
31 using System.Xml.XPath;
32 using OpenTween.Api.DataModel;
34 namespace OpenTween.Connection
36 public class Mobypicture : IMediaUploadService
38 private static readonly long MaxFileSize = 5L * 1024 * 1024; // 上限不明
40 // 参照: http://developers.mobypicture.com/documentation/1-0/postmedia/
41 private static readonly IEnumerable<string> AllowedExtensions = new[]
73 private readonly Twitter twitter;
74 private readonly MobypictureApi mobypictureApi;
76 private TwitterConfiguration twitterConfig;
78 public Mobypicture(Twitter twitter, TwitterConfiguration twitterConfig)
81 throw new ArgumentNullException(nameof(twitter));
82 if (twitterConfig == null)
83 throw new ArgumentNullException(nameof(twitterConfig));
85 this.twitter = twitter;
86 this.twitterConfig = twitterConfig;
88 this.mobypictureApi = new MobypictureApi(twitter.AccessToken, twitter.AccessTokenSecret);
91 public int MaxMediaCount
96 public string SupportedFormatsStrForDialog
100 var filterFormatExtensions = "";
101 foreach (var pictureExtension in AllowedExtensions)
103 filterFormatExtensions += '*' + pictureExtension + ';';
105 return "Media Files(" + filterFormatExtensions + ")|" + filterFormatExtensions;
109 public bool CheckFileExtension(string fileExtension)
111 return AllowedExtensions.Contains(fileExtension, StringComparer.OrdinalIgnoreCase);
114 public bool CheckFileSize(string fileExtension, long fileSize)
116 var maxFileSize = this.GetMaxFileSize(fileExtension);
117 return maxFileSize == null || fileSize <= maxFileSize.Value;
120 public long? GetMaxFileSize(string fileExtension)
125 public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
127 if (mediaItems == null)
128 throw new ArgumentNullException(nameof(mediaItems));
130 if (mediaItems.Length != 1)
131 throw new ArgumentOutOfRangeException(nameof(mediaItems));
133 var item = mediaItems[0];
136 throw new ArgumentException("Err:Media not specified.");
139 throw new ArgumentException("Err:Media not found.");
141 var xml = await this.mobypictureApi.UploadFileAsync(item, text)
142 .ConfigureAwait(false);
144 var imageUrlElm = xml.XPathSelectElement("/rsp/media/mediaurl");
145 if (imageUrlElm == null)
146 throw new WebApiException("Invalid API response", xml.ToString());
148 var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
150 await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
151 .ConfigureAwait(false);
154 public int GetReservedTextLength(int mediaCount)
156 return this.twitterConfig.ShortUrlLength;
159 public void UpdateTwitterConfiguration(TwitterConfiguration config)
161 this.twitterConfig = config;
164 public class MobypictureApi : HttpConnectionOAuthEcho
166 private static readonly Uri UploadEndpoint = new Uri("https://api.mobypicture.com/2.0/upload.xml");
168 public MobypictureApi(string twitterAccessToken, string twitterAccessTokenSecret)
169 : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json"))
171 this.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
172 twitterAccessToken, twitterAccessTokenSecret, "", "");
174 this.InstanceTimeout = 60000;
180 /// <exception cref="WebApiException"/>
181 /// <exception cref="XmlException"/>
182 public async Task<XDocument> UploadFileAsync(IMediaItem item, string message)
184 // 参照: http://developers.mobypicture.com/documentation/2-0/upload/
186 var param = new Dictionary<string, string>
188 ["key"] = ApplicationSettings.MobypictureKey,
189 ["message"] = message,
191 var paramFiles = new List<KeyValuePair<string, IMediaItem>>
193 new KeyValuePair<string, IMediaItem>("media", item),
197 var uploadTask = Task.Run(() => this.GetContent(HttpConnection.PostMethod,
198 UploadEndpoint, param, paramFiles, ref response, null, null));
200 var ret = await uploadTask.ConfigureAwait(false);
202 if (ret != HttpStatusCode.OK)
203 throw new WebApiException("Err:" + ret, response);
205 return XDocument.Parse(response);