OSDN Git Service

Port Connection/TwitPic.vb to C#
authorspinor <spinor2109@gmail.com>
Sun, 18 Dec 2011 12:14:28 +0000 (21:14 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 22 Feb 2012 10:50:52 +0000 (19:50 +0900)
Tween/Connection/TwitPic.vb [deleted file]
TweenCS/Connection/TwitPic.cs [new file with mode: 0644]
TweenCS/Connection/yfrog.cs
TweenCS/TweenCS.csproj

diff --git a/Tween/Connection/TwitPic.vb b/Tween/Connection/TwitPic.vb
deleted file mode 100644 (file)
index 0aa55ca..0000000
+++ /dev/null
@@ -1,185 +0,0 @@
-' 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 TwitPic
-    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 = "bbc6449ceac87ef10c546e4a0ca06ef4"
-    Private pictureExt() As String = {".jpg", _
-                                    ".jpeg", _
-                                    ".gif", _
-                                    ".png"}
-    Private multimediaExt() As String = {".avi", _
-                                         ".wmv", _
-                                         ".flv", _
-                                         ".m4v", _
-                                         ".mov", _
-                                         ".mp4", _
-                                         ".rm", _
-                                         ".mpeg", _
-                                         ".mpg", _
-                                         ".3gp", _
-                                         ".3g2"}
-
-    Private Const MaxFileSize As Long = 10 * 1024 * 1024    'Image only
-    'Multimedia filesize limit unknown. But length limit is 1:30.
-
-    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
-        'TwitPicへの投稿
-        Try
-            ret = UploadFile(mediaFile, message, content)
-        Catch ex As Exception
-            Return "Err:" + ex.Message
-        End Try
-        Dim url As String = ""
-        If ret = HttpStatusCode.OK Then
-            Dim xd As XmlDocument = New XmlDocument()
-            Try
-                xd.LoadXml(content)
-                'URLの取得
-                url = xd.SelectSingleNode("/image/url").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(message) Then message = ""
-        If String.IsNullOrEmpty(url) Then url = ""
-        '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, 0)
-    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("key", ApiKey)
-        param.Add("message", message)
-        Dim binary As New List(Of KeyValuePair(Of String, FileInfo))
-        binary.Add(New KeyValuePair(Of String, FileInfo)("media", mediaFile))
-        If Me.GetFileType(mediaFile.Extension) = UploadFileType.Picture Then
-            Me.InstanceTimeout = 60000 'タイムアウト60秒
-        Else
-            Me.InstanceTimeout = 120000
-        End If
-
-        Return GetContent(PostMethod, _
-                          New Uri("http://api.twitpic.com/2/upload.xml"), _
-                          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
-        If Array.IndexOf(multimediaExt, ext.ToLower) > -1 Then Return True
-        Return False
-    End Function
-
-    Public Function GetFileOpenDialogFilter() As String Implements IMultimediaShareService.GetFileOpenDialogFilter
-        Return "Image Files(*" + String.Join(";*", pictureExt) + ")|*" + String.Join(";*", pictureExt) +
-            "|Videos(*" + String.Join(";*", multimediaExt) + ")|*" + String.Join(";*", multimediaExt)
-    End Function
-
-    Public Function GetFileType(ByVal ext As String) As UploadFileType Implements IMultimediaShareService.GetFileType
-        If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then Return UploadFileType.Picture
-        If Array.IndexOf(multimediaExt, ext.ToLower) > -1 Then Return UploadFileType.MultiMedia
-        Return UploadFileType.Invalid
-    End Function
-
-    Public Function IsSupportedFileType(ByVal type As UploadFileType) As Boolean Implements IMultimediaShareService.IsSupportedFileType
-        Return Not type.Equals(UploadFileType.Invalid)
-    End Function
-
-    Public Function CheckValidFilesize(ByVal ext As String, ByVal fileSize As Long) As Boolean Implements IMultimediaShareService.CheckValidFilesize
-        If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then Return fileSize <= MaxFileSize
-        If Array.IndexOf(multimediaExt, ext.ToLower) > -1 Then Return True 'Multimedia : no check
-        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
diff --git a/TweenCS/Connection/TwitPic.cs b/TweenCS/Connection/TwitPic.cs
new file mode 100644 (file)
index 0000000..e049900
--- /dev/null
@@ -0,0 +1,213 @@
+// 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 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 UploadFileType = Tween.MyCommon.UploadFileType;
+using Uri = System.Uri;
+using Array = System.Array;
+
+namespace Tween
+{
+       public class TwitPic : HttpConnectionOAuthEcho, IMultimediaShareService
+       {
+               // OAuth関連
+
+               /// <summary>
+               /// OAuthのコンシューマー鍵
+               /// </summary>
+               private const string ConsumerKey = "ST6eAABKDRKTqbN7pPo2A";
+
+               /// <summary>
+               /// OAuthの署名作成用秘密コンシューマーデータ
+               /// </summary>
+               private const string ConsumerSecretKey = "BJMEiivrXlqGESzdb8D0bvLfNYf3fifXRDMFjMogXg";
+
+               private const string ApiKey = "bbc6449ceac87ef10c546e4a0ca06ef4";
+
+               private string[] pictureExt = new string[] { ".jpg", ".jpeg", ".gif", ".png" };
+
+               private string[] multimediaExt = new string[] { ".avi", ".wmv", ".flv", ".m4v", ".mov", ".mp4", ".rm", ".mpeg", ".mpg", ".3gp", ".3g2" };
+
+               private const long MaxFileSize = 10 * 1024 * 1024; // Image only
+               // Multimedia filesize limit unknown. But length limit is 1:30.
+
+               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;
+                       // TwitPicへの投稿
+                       try
+                       {
+                               ret = this.UploadFile( mediaFile, message, ref content );
+                       }
+                       catch ( Exception ex )
+                       {
+                               return "Err:" + ex.Message;
+                       }
+                       string url = "";
+                       if ( ret == HttpStatusCode.OK )
+                       {
+                               XmlDocument xd = new XmlDocument();
+                               try
+                               {
+                                       xd.LoadXml( content );
+                                       // URLの取得
+                                       url = xd.SelectSingleNode( "/image/url" ).InnerText;
+                               }
+                               catch ( XmlException ex )
+                               {
+                                       return "Err:" + ex.Message;
+                               }
+                               catch ( Exception ex )
+                               {
+                                       return "Err:" + ex.Message;
+                               }
+                       }
+                       else
+                               return "Err:" + ret.ToString();
+
+                       // アップロードまでは成功
+                       filePath = "";
+                       if ( string.IsNullOrEmpty( message ) )
+                               message = "";
+                       if ( string.IsNullOrEmpty( url ) )
+                               url = "";
+                       // 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, 0 );
+               }
+
+               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( "key", TwitPic.ApiKey );
+                       param.Add( "message", message );
+                       List< KeyValuePair< string, FileInfo > > binary = new List< KeyValuePair< string, FileInfo > >();
+                       binary.Add( new KeyValuePair< string, FileInfo >( "media", mediaFile ) );
+                       if ( this.GetFileType( mediaFile.Extension ) == UploadFileType.Picture )
+                               this.InstanceTimeout = 60000; // タイムアウト60秒
+                       else
+                               this.InstanceTimeout = 120000;
+
+                       return this.GetContent( HttpConnection.PostMethod, new Uri( "http://api.twitpic.com/2/upload.xml" ), param, binary, ref content, null, null );
+               }
+
+               public bool CheckValidExtension( string ext )
+               {
+                       if ( Array.IndexOf( this.pictureExt, ext.ToLower() ) > -1 )
+                               return true;
+                       if ( Array.IndexOf( this.multimediaExt, ext.ToLower() ) > -1 )
+                               return true;
+
+                       return false;
+               }
+
+               public string GetFileOpenDialogFilter()
+               {
+                       return "Image Files(*" + string.Join( ";*", this.pictureExt ) + ")|*" + string.Join( ";*", this.pictureExt )
+                              + "|Videos(*" + string.Join( ";*", this.multimediaExt ) + ")|*" + string.Join( ";*", this.multimediaExt );
+               }
+
+               public UploadFileType GetFileType( string ext )
+               {
+                       if ( Array.IndexOf( this.pictureExt, ext.ToLower() ) > -1 )
+                               return UploadFileType.Picture;
+                       if ( Array.IndexOf( this.multimediaExt, ext.ToLower() ) > -1 )
+                               return UploadFileType.MultiMedia;
+
+                       return UploadFileType.Invalid;
+               }
+
+               public bool IsSupportedFileType( UploadFileType type )
+               {
+                       return !type.Equals( UploadFileType.Invalid );
+               }
+
+               public bool CheckValidFilesize( string ext, long fileSize )
+               {
+                       if ( Array.IndexOf( this.pictureExt, ext.ToLower() ) > -1 )
+                               return fileSize <= TwitPic.MaxFileSize;
+                       if ( Array.IndexOf( this.multimediaExt, ext.ToLower() ) > -1 )
+                               return true; // Multimedia : no check
+
+                       return false;
+               }
+
+               public bool Configuration( string key, object value )
+               {
+                       return true;
+               }
+
+               public TwitPic( Twitter twitter )
+                       : base( new Uri( "http://api.twitter.com/" ), new Uri( "https://api.twitter.com/1/account/verify_credentials.json" ) )
+               {
+                       this.tw = twitter;
+                       this.Initialize( TwitPic.ConsumerKey, TwitPic.ConsumerSecretKey, tw.AccessToken, tw.AccessTokenSecret, "", "" );
+               }
+       }
+}
index 0105756..89e74d3 100644 (file)
@@ -51,7 +51,7 @@ namespace Tween
 
                /// <summary>
                /// OAuthの署名作成用秘密コンシューマーデータ
-               /// </summary><summary>
+               /// </summary>
                private const string ConsumerSecretKey = "BJMEiivrXlqGESzdb8D0bvLfNYf3fifXRDMFjMogXg";
 
                private const string ApiKey = "HIDP42ZO6314ee2218e2995662bad5ae320c32f1";
@@ -113,7 +113,7 @@ namespace Tween
                                }
                        }
                        else
-                       return "Err:" + ret.ToString();
+                               return "Err:" + ret.ToString();
 
                        if ( string.IsNullOrEmpty( url ) )
                                url = "";
@@ -126,7 +126,8 @@ namespace Tween
                        if ( message.Length + AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia + 1 > 140 )
                                message = message.Substring(0, 140 - AppendSettingDialog.Instance.TwitterConfiguration.CharactersReservedPerMedia - 1) + " " + url;
                        else
-                       message += " " + url;
+                               message += " " + url;
+
                        return this.tw.PostStatus( message, 0 );
                }
 
index 47b0910..ea63641 100644 (file)
@@ -52,6 +52,7 @@
     <Compile Include="Connection\imgly.cs" />\r
     <Compile Include="Connection\IMultimediaShareService.cs" />\r
     <Compile Include="Connection\Plixi.cs" />\r
+    <Compile Include="Connection\TwitPic.cs" />\r
     <Compile Include="Connection\TwitterPhoto.cs" />\r
     <Compile Include="Connection\TwitVideo.cs" />\r
     <Compile Include="Connection\yfrog.cs" />\r