OSDN Git Service

aae555f45769c3038249fb14e0ef4e674dfb5a10
[opentween/open-tween.git] / Tween / Connection / TwitPic.vb
1 ' Tween - Client of Twitter
2 ' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 '           (c) 2008-2011 Moz (@syo68k)
4 '           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 '           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 '           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 ' All rights reserved.
8
9 ' This file is part of Tween.
10
11 ' This program is free software; you can redistribute it and/or modify it
12 ' under the terms of the GNU General Public License as published by the Free
13 ' Software Foundation; either version 3 of the License, or (at your option)
14 ' any later version.
15
16 ' This program is distributed in the hope that it will be useful, but
17 ' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 ' for more details. 
20
21 ' You should have received a copy of the GNU General Public License along
22 ' with this program. If not, see <http://www.gnu.org/licenses/>, or write to
23 ' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 ' Boston, MA 02110-1301, USA.
25
26 Imports System.IO
27 Imports System.Text
28 Imports System.Net
29
30 Public Class TwitPic
31     Inherits HttpConnectionOAuthEcho
32
33     'OAuth関連
34     '''<summary>
35     '''OAuthのコンシューマー鍵
36     '''</summary>
37     Private Const ConsumerKey As String = "tLbG3uS0BIIE8jm1mKzKOfZ6EgUOmWVM"
38
39     '''<summary>
40     '''OAuthの署名作成用秘密コンシューマーデータ
41     '''</summary>
42     Private Const ConsumerSecretKey As String = "M0IMsbl2722iWa+CGPVcNeQmE+TFpJk8B/KW9UUTk3eLOl9Ij005r52JNxVukTzM"
43
44     Private Const PostMethod As String = "POST"
45     Private Const GetMethod As String = "GET"
46     Private Const ApiKey As String = "287b60562aea3cab9f58fa54015848e8"
47     Private pictureExt() As String = {".jpg", _
48                                     ".jpeg", _
49                                     ".gif", _
50                                     ".png"}
51
52     Private Const MaxFileSize As Long = 5 * 1024 * 1024
53
54     Public Function Upload(ByVal mediaFile As FileInfo, _
55                        ByVal message As String, _
56                        ByRef content As String) As HttpStatusCode
57         'Message必須
58         If String.IsNullOrEmpty(message) Then message = ""
59         'Check filetype and size(Max 5MB)
60         If Array.IndexOf(pictureExt, mediaFile.Extension.ToLower) > -1 Then
61             If mediaFile.Length > MaxFileSize Then Throw New ArgumentException("File is too large.")
62         Else
63             Throw New ArgumentException("Service don't support this filetype.")
64         End If
65
66         Dim param As New Dictionary(Of String, String)
67         param.Add("key", ApiKey)
68         param.Add("message", message)
69         Dim binary As New List(Of KeyValuePair(Of String, FileInfo))
70         binary.Add(New KeyValuePair(Of String, FileInfo)("media", mediaFile))
71         Me.InstanceTimeout = 60000 'タイムアウト60秒
72
73         Return GetContent(PostMethod, _
74                           New Uri("http://api.twitpic.com/2/upload.xml"), _
75                           param, _
76                           binary, _
77                           content, _
78                           Nothing, _
79                           Nothing)
80     End Function
81
82     Public Function CheckValidExtension(ByVal ext As String) As Boolean
83         If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
84             Return True
85         End If
86         Return False
87     End Function
88
89     Public Function GetFileOpenDialogFilter() As String
90         Return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png"
91     End Function
92
93     Public Function GetFileType(ByVal ext As String) As UploadFileType
94         If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
95             Return UploadFileType.Picture
96         End If
97         Return UploadFileType.Invalid
98     End Function
99
100     Public Function IsSupportedFileType(ByVal type As UploadFileType) As Boolean
101         Return type.Equals(UploadFileType.Picture)
102     End Function
103
104     Public Function GetMaxFileSize(ByVal ext As String) As Long
105         If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
106             Return MaxFileSize
107         End If
108         Return -1
109     End Function
110
111     Public Sub New(ByVal accessToken As String, ByVal accessTokenSecret As String)
112         MyBase.New(New Uri("http://api.twitter.com/"), _
113                    New Uri("https://api.twitter.com/1/account/verify_credentials.json"))
114         Initialize(DecryptString(ConsumerKey), DecryptString(ConsumerSecretKey), accessToken, accessTokenSecret, "")
115     End Sub
116 End Class