+++ /dev/null
-' Tween - Client of Twitter
-' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
-' (c) 2008-2011 Moz (@syo68k)
-' (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
-' (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
-' (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
-' All rights reserved.
-'
-' This file is part of Tween.
-'
-' This program is free software; you can redistribute it and/or modify it
-' under the terms of the GNU General Public License as published by the Free
-' Software Foundation; either version 3 of the License, or (at your option)
-' any later version.
-'
-' This program is distributed in the hope that it will be useful, but
-' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-' for more details.
-'
-' You should have received a copy of the GNU General Public License along
-' with this program. If not, see <http://www.gnu.org/licenses/>, or write to
-' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
-' Boston, MA 02110-1301, USA.
-
-Imports System.IO
-Imports System.Net
-Imports System.Xml
-
-Public Class Plixi
- Inherits HttpConnectionOAuthEcho
- Implements IMultimediaShareService
-
- 'OAuth関連
- '''<summary>
- '''OAuthのコンシューマー鍵
- '''</summary>
- Private Const ConsumerKey As String = "ST6eAABKDRKTqbN7pPo2A"
-
- '''<summary>
- '''OAuthの署名作成用秘密コンシューマーデータ
- '''</summary>
- Private Const ConsumerSecretKey As String = "BJMEiivrXlqGESzdb8D0bvLfNYf3fifXRDMFjMogXg"
-
- Private Const ApiKey As String = "91083b55-f8f9-4b91-a0b3-f999e2e45af2"
- Private pictureExt() As String = {".jpg", _
- ".jpeg", _
- ".gif", _
- ".png"}
-
- Private Const MaxFileSize As Long = 5 * 1024 * 1024
-
- Private tw As Twitter
-
- Public Function Upload(ByRef filePath As String,
- ByRef message As String,
- ByVal reply_to As Long) As String Implements IMultimediaShareService.Upload
- If String.IsNullOrEmpty(filePath) Then Return "Err:File isn't specified."
- If String.IsNullOrEmpty(message) Then message = ""
- Dim mediaFile As FileInfo
- Try
- mediaFile = New FileInfo(filePath)
- Catch ex As NotSupportedException
- Return "Err:" + ex.Message
- End Try
- If mediaFile Is Nothing OrElse Not mediaFile.Exists Then Return "Err:File isn't exists."
-
- Dim content As String = ""
- Dim ret As HttpStatusCode
- 'Plixiへの投稿
- Try
- ret = UploadFile(mediaFile, message, content)
- Catch ex As Exception
- Return "Err:" + ex.Message
- End Try
- Dim url As String = ""
- If ret = HttpStatusCode.Created Then
- Dim xd As XmlDocument = New XmlDocument()
- Try
- xd.LoadXml(content)
- 'MediaUrlの取得
- url = xd.ChildNodes().Item(0).ChildNodes(2).InnerText
- Catch ex As XmlException
- Return "Err:" + ex.Message
- Catch Ex As Exception
- Return "Err:" + Ex.Message
- End Try
- Else
- Return "Err:" + ret.ToString
- End If
- 'アップロードまでは成功
- filePath = ""
- If String.IsNullOrEmpty(url) Then url = ""
- If String.IsNullOrEmpty(message) Then message = ""
- 'Twitterへの投稿
- '投稿メッセージの再構成
- If message.Length + AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia + 1 > 140 Then
- message = message.Substring(0, 140 - AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia - 1) + " " + url
- Else
- message += " " + url
- End If
- Return tw.PostStatus(message, reply_to)
- End Function
-
- Private Function UploadFile(ByVal mediaFile As FileInfo, _
- ByVal message As String, _
- ByRef content As String) As HttpStatusCode
- 'Message必須
- If String.IsNullOrEmpty(message) Then message = ""
- 'Check filetype and size(Max 5MB)
- If Not Me.CheckValidExtension(mediaFile.Extension) Then Throw New ArgumentException("Service don't support this filetype.")
- If Not Me.CheckValidFilesize(mediaFile.Extension, mediaFile.Length) Then Throw New ArgumentException("File is too large.")
-
- Dim param As New Dictionary(Of String, String)
- param.Add("api_key", ApiKey)
- param.Add("message", message)
- param.Add("isoauth", "true")
- Dim binary As New List(Of KeyValuePair(Of String, FileInfo))
- binary.Add(New KeyValuePair(Of String, FileInfo)("media", mediaFile))
- Me.InstanceTimeout = 60000 'タイムアウト60秒
-
- Return GetContent(PostMethod, _
- New Uri("http://api.plixi.com/api/upload.aspx"), _
- param, _
- binary, _
- content, _
- Nothing, _
- Nothing)
- End Function
-
- Public Function CheckValidExtension(ByVal ext As String) As Boolean Implements IMultimediaShareService.CheckValidExtension
- If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
- Return True
- End If
- Return False
- End Function
-
- Public Function GetFileOpenDialogFilter() As String Implements IMultimediaShareService.GetFileOpenDialogFilter
- Return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png"
- End Function
-
- Public Function GetFileType(ByVal ext As String) As UploadFileType Implements IMultimediaShareService.GetFileType
- If Me.CheckValidExtension(ext) Then
- Return UploadFileType.Picture
- End If
- Return UploadFileType.Invalid
- End Function
-
- Public Function IsSupportedFileType(ByVal type As UploadFileType) As Boolean Implements IMultimediaShareService.IsSupportedFileType
- Return type.Equals(UploadFileType.Picture)
- End Function
-
- Public Function CheckValidFilesize(ByVal ext As String, ByVal fileSize As Long) As Boolean Implements IMultimediaShareService.CheckValidFilesize
- If Me.CheckValidExtension(ext) Then
- Return fileSize <= MaxFileSize
- End If
- Return False
- End Function
-
- Public Function Configuration(ByVal key As String, ByVal value As Object) As Boolean Implements IMultimediaShareService.Configuration
- Return True
- End Function
-
- Public Sub New(ByVal twitter As Twitter)
- MyBase.New(New Uri("http://api.twitter.com/"), _
- New Uri("https://api.twitter.com/1/account/verify_credentials.json"))
- tw = twitter
- Initialize(ConsumerKey, ConsumerSecretKey, tw.AccessToken, tw.AccessTokenSecret, "", "")
- End Sub
-End Class
-
--- /dev/null
+// OpenTween - Client of Twitter
+// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
+// (c) 2008-2011 Moz (@syo68k)
+// (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
+// (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
+// (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
+// (c) 2011 spinor (@tplantd) <http://d.hatena.ne.jp/spinor/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using HttpConnectionOAuthEcho = Tween.HttpConnectionOAuthEcho;
+using IMultimediaShareService = Tween.IMultimediaShareService;
+using Twitter = Tween.Twitter;
+using FileInfo = System.IO.FileInfo;
+using NotSupportedException = System.NotSupportedException;
+using HttpStatusCode = System.Net.HttpStatusCode;
+using Exception = System.Exception;
+using XmlDocument = System.Xml.XmlDocument;
+using XmlException = System.Xml.XmlException;
+using ArgumentException = System.ArgumentException;
+using System.Collections.Generic; // for Dictionary<TKey, TValue>, List<T>, KeyValuePair<TKey, TValue>
+using Uri = System.Uri;
+using Array = System.Array;
+using UploadFileType = Tween.MyCommon.UploadFileType;
+
+namespace Tween
+{
+ public class Plixi : HttpConnectionOAuthEcho, IMultimediaShareService
+ {
+ // OAuth関連
+
+ /// <summary>
+ /// OAuthのコンシューマー鍵
+ /// </summary>
+ private const string ConsumerKey = "ST6eAABKDRKTqbN7pPo2A";
+
+ /// <summary>
+ /// OAuthの署名作成用秘密コンシューマーデータ
+ /// </summary>
+ private const string ConsumerSecretKey = "BJMEiivrXlqGESzdb8D0bvLfNYf3fifXRDMFjMogXg";
+
+ private const string ApiKey = "91083b55-f8f9-4b91-a0b3-f999e2e45af2";
+
+ private string[] pictureExt = new string[] { ".jpg", ".jpeg", ".gif", ".png" };
+
+ private const long MaxFileSize = 5 * 1024 * 1024;
+
+ private Twitter tw;
+
+ public string Upload( ref string filePath, ref string message, long reply_to )
+ {
+ if ( string.IsNullOrEmpty( filePath ) )
+ return "Err:File isn't specified.";
+ if ( string.IsNullOrEmpty( message ) )
+ message = "";
+ FileInfo mediaFile;
+ try
+ {
+ mediaFile = new FileInfo( filePath );
+ }
+ catch ( NotSupportedException ex )
+ {
+ return "Err:" + ex.Message;
+ }
+ if ( mediaFile == null || !mediaFile.Exists )
+ return "Err:File isn't exists.";
+
+ string content = "";
+ HttpStatusCode ret;
+ // Plixiへの投稿
+ try
+ {
+ ret = this.UploadFile( mediaFile, message, ref content );
+ }
+ catch ( Exception ex )
+ {
+ return "Err:" + ex.Message;
+ }
+ string url = "";
+ if ( ret == HttpStatusCode.Created )
+ {
+ XmlDocument xd = new XmlDocument();
+ try
+ {
+ xd.LoadXml( content );
+ // MediaUrlの取得
+ url = xd.ChildNodes.Item( 0 ).ChildNodes[ 2 ].InnerText;
+ }
+ catch ( XmlException ex )
+ {
+ return "Err:" + ex.Message;
+ }
+ catch ( Exception ex )
+ {
+ return "Err:" + ex.Message;
+ }
+ }
+ else
+ {
+ return "Err:" + ret.ToString();
+ }
+ // アップロードまでは成功
+ filePath = "";
+ if ( string.IsNullOrEmpty( url ) )
+ url = "";
+ if ( string.IsNullOrEmpty( message ) )
+ message = "";
+ // Twitterへの投稿
+ // 投稿メッセージの再構成
+ if ( message.Length + AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia + 1 > 140 )
+ message = message.Substring( 0, 140 - AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia - 1 ) + " " + url;
+ else
+ message += " " + url;
+ return tw.PostStatus( message, reply_to );
+ }
+
+ private HttpStatusCode UploadFile( FileInfo mediaFile, string message, ref string content )
+ {
+ // Message必須
+ if ( string.IsNullOrEmpty( message ) )
+ message = "";
+ // Check filetype and size(Max 5MB)
+ if ( !this.CheckValidExtension( mediaFile.Extension ) )
+ throw new ArgumentException( "Service don't support this filetype." );
+ if ( !this.CheckValidFilesize( mediaFile.Extension, mediaFile.Length ) )
+ throw new ArgumentException( "File is too large." );
+
+ Dictionary< string, string > param = new Dictionary< string, string >();
+ param.Add( "api_key", Plixi.ApiKey );
+ param.Add( "message", message );
+ param.Add( "isoauth", "true" );
+ List< KeyValuePair< string, FileInfo > > binary = new List< KeyValuePair< string, FileInfo > >();
+ binary.Add( new KeyValuePair< string, FileInfo >( "media", mediaFile ) );
+ this.InstanceTimeout = 60000; // タイムアウト60秒
+
+ return base.GetContent( HttpConnection.PostMethod, new Uri( "http://api.plixi.com/api/upload.aspx" ), param, binary, ref content, null, null );
+ }
+
+ public bool CheckValidExtension( string ext )
+ {
+ if ( Array.IndexOf( pictureExt, ext.ToLower() ) > -1 )
+ return true;
+
+ return false;
+ }
+
+ public string GetFileOpenDialogFilter()
+ {
+ return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
+ }
+
+ public UploadFileType GetFileType( string ext )
+ {
+ if ( this.CheckValidExtension( ext ) )
+ return UploadFileType.Picture;
+
+ return UploadFileType.Invalid;
+ }
+
+ public bool IsSupportedFileType( UploadFileType type )
+ {
+ return type.Equals( UploadFileType.Picture );
+ }
+
+ public bool CheckValidFilesize( string ext, long fileSize )
+ {
+ if ( this.CheckValidExtension( ext ) )
+ return fileSize <= Plixi.MaxFileSize;
+
+ return false;
+ }
+
+ public bool Configuration( string key, object value )
+ {
+ return true;
+ }
+
+ public Plixi( Twitter twitter )
+ : base( new Uri( "http://api.twitter.com/" ), new Uri( "https://api.twitter.com/1/account/verify_credentials.json" ) )
+ {
+ this.tw = twitter;
+ base.Initialize( ConsumerKey, ConsumerSecretKey, tw.AccessToken, tw.AccessTokenSecret, "", "" );
+ }
+ }
+}