OSDN Git Service

d170493f1016985a6409cc4817ff829ee3cbb63f
[opentween/open-tween.git] / Tween / Connection / imgly.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 imgly
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 pictureExt() As String = {".jpg", _
47                                     ".jpeg", _
48                                     ".gif", _
49                                     ".png"}
50
51     Private Const MaxFileSize As Long = 4 * 1024 * 1024
52
53     Public Function Upload(ByVal mediaFile As FileInfo, _
54                        ByVal message As String, _
55                        ByRef content As String) As HttpStatusCode
56         'Message必須
57         If String.IsNullOrEmpty(message) Then message = ""
58         'Check filetype and size(Max 4MB)
59         If Array.IndexOf(pictureExt, mediaFile.Extension.ToLower) > -1 Then
60             If mediaFile.Length > MaxFileSize Then Throw New ArgumentException("File is too large.")
61         Else
62             Throw New ArgumentException("Service don't support this filetype.")
63         End If
64
65         Dim param As New Dictionary(Of String, String)
66         param.Add("message", message)
67         Dim binary As New List(Of KeyValuePair(Of String, FileInfo))
68         binary.Add(New KeyValuePair(Of String, FileInfo)("media", mediaFile))
69         Me.InstanceTimeout = 60000 'タイムアウト60秒
70
71         Return GetContent(PostMethod, _
72                           New Uri("http://img.ly/api/2/upload.xml"), _
73                           param, _
74                           binary, _
75                           content, _
76                           Nothing, _
77                           Nothing)
78     End Function
79
80     Public Function CheckValidExtension(ByVal ext As String) As Boolean
81         If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
82             Return True
83         End If
84         Return False
85     End Function
86
87     Public Function GetFileOpenDialogFilter() As String
88         Return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png"
89     End Function
90
91     Public Function GetFileType(ByVal ext As String) As UploadFileType
92         If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
93             Return UploadFileType.Picture
94         End If
95         Return UploadFileType.Invalid
96     End Function
97
98     Public Function IsSupportedFileType(ByVal type As UploadFileType) As Boolean
99         Return type.Equals(UploadFileType.Picture)
100     End Function
101
102     Public Function GetMaxFileSize(ByVal ext As String) As Long
103         If Array.IndexOf(pictureExt, ext.ToLower) > -1 Then
104             Return MaxFileSize
105         End If
106         Return -1
107     End Function
108
109     Public Sub New(ByVal accessToken As String, ByVal accessTokenSecret As String)
110         MyBase.New(New Uri("http://api.twitter.com/"), _
111                    New Uri("https://api.twitter.com/1/account/verify_credentials.json"))
112         Initialize(DecryptString(ConsumerKey), DecryptString(ConsumerSecretKey), accessToken, accessTokenSecret, "")
113     End Sub
114 End Class