OSDN Git Service

MessageFormをMessageBoxに置き換え
[opentween/open-tween.git] / OpenTween / PictureService.vb
1 ' OpenTween - 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 OpenTween.
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.Net
28 Imports System.Xml
29
30 Public Class PictureService
31     Private tw As Twitter
32
33     Public Function Upload(ByRef filePath As String, ByRef message As String, ByVal service As String) As String
34         Dim file As FileInfo
35         Try
36             file = New FileInfo(filePath)
37         Catch ex As NotSupportedException
38             Return "Err:" + ex.Message
39         End Try
40         If Not file.Exists Then Return "Err:File isn't exists."
41         Dim st As AppendSettingDialog = AppendSettingDialog.Instance
42         Dim ret As String = ""
43         Dim upResult As Boolean = False
44         Select Case service
45             Case "TwitPic"
46                 ret = UpToTwitPic(file, message, upResult)
47             Case "img.ly"
48                 ret = UpToimgly(file, message, upResult)
49                 'Case "TwitVideo"
50                 '    ret = UpToTwitVideo(file, message, upResult)
51             Case "yfrog"
52                 ret = UpToyfrog(file, message, upResult)
53             Case "Plixi"
54                 ret = UpToPlixi(file, message, upResult)
55         End Select
56         If upResult Then filePath = ""
57         Return ret
58     End Function
59
60     Public Function IsValidExtension(ByVal ext As String, ByVal service As String) As Boolean
61         Dim ret As Boolean = False
62         Select Case service
63             Case "TwitPic"
64                 ret = (New TwitPic(tw.AccessToken, tw.AccessTokenSecret)).CheckValidExtension(ext)
65             Case "img.ly"
66                 ret = (New imgly(tw.AccessToken, tw.AccessTokenSecret)).CheckValidExtension(ext)
67                 'Case "TwitVideo"
68                 '    ret = (New TwitVideo).CheckValidExtension(ext)
69             Case "yfrog"
70                 ret = (New yfrog(tw.AccessToken, tw.AccessTokenSecret)).CheckValidExtension(ext)
71             Case "Plixi"
72                 ret = (New Plixi(tw.AccessToken, tw.AccessTokenSecret)).CheckValidExtension(ext)
73         End Select
74         Return ret
75     End Function
76
77     Public Function GetFileOpenDialogFilter(ByVal service As String) As String
78         Dim ret As String = ""
79         Select Case service
80             Case "TwitPic"
81                 ret = (New TwitPic(tw.AccessToken, tw.AccessTokenSecret)).GetFileOpenDialogFilter
82             Case "img.ly"
83                 ret = (New imgly(tw.AccessToken, tw.AccessTokenSecret)).GetFileOpenDialogFilter
84                 'Case "TwitVideo"
85                 '    ret = (New TwitVideo).GetFileOpenDialogFilter
86             Case "yfrog"
87                 ret = (New yfrog(tw.AccessToken, tw.AccessTokenSecret)).GetFileOpenDialogFilter
88             Case "Plixi"
89                 ret = (New Plixi(tw.AccessToken, tw.AccessTokenSecret)).GetFileOpenDialogFilter
90         End Select
91         Return ret
92     End Function
93
94     Public Function GetFileType(ByVal ext As String, ByVal service As String) As UploadFileType
95         Dim ret As UploadFileType = UploadFileType.Invalid
96         Select Case service
97             Case "TwitPic"
98                 ret = (New TwitPic(tw.AccessToken, tw.AccessTokenSecret)).GetFileType(ext)
99             Case "img.ly"
100                 ret = (New imgly(tw.AccessToken, tw.AccessTokenSecret)).GetFileType(ext)
101                 'Case "TwitVideo"
102                 '    ret = (New TwitVideo).GetFileType(ext)
103             Case "yfrog"
104                 ret = (New yfrog(tw.AccessToken, tw.AccessTokenSecret)).GetFileType(ext)
105             Case "Plixi"
106                 ret = (New Plixi(tw.AccessToken, tw.AccessTokenSecret)).GetFileType(ext)
107         End Select
108         Return ret
109     End Function
110
111     Public Function IsSupportedFileType(ByVal type As UploadFileType, ByVal service As String) As Boolean
112         Dim ret As Boolean = False
113         Select Case service
114             Case "TwitPic"
115                 ret = (New TwitPic(tw.AccessToken, tw.AccessTokenSecret)).IsSupportedFileType(type)
116             Case "img.ly"
117                 ret = (New imgly(tw.AccessToken, tw.AccessTokenSecret)).IsSupportedFileType(type)
118                 'Case "TwitVideo"
119                 '    ret = (New TwitVideo).IsSupportedFileType(type)
120             Case "yfrog"
121                 ret = (New yfrog(tw.AccessToken, tw.AccessTokenSecret)).IsSupportedFileType(type)
122             Case "Plixi"
123                 ret = (New Plixi(tw.AccessToken, tw.AccessTokenSecret)).IsSupportedFileType(type)
124         End Select
125         Return ret
126     End Function
127
128     Public Function GetMaxFileSize(ByVal ext As String, ByVal service As String) As Long
129         Dim ret As Long = -1
130         Select Case service
131             Case "TwitPic"
132                 ret = (New TwitPic(tw.AccessToken, tw.AccessTokenSecret)).GetMaxFileSize(ext)
133             Case "img.ly"
134                 ret = (New imgly(tw.AccessToken, tw.AccessTokenSecret)).GetMaxFileSize(ext)
135                 'Case "TwitVideo"
136                 '    ret = (New TwitVideo).GetMaxFileSize(ext)
137             Case "yfrog"
138                 ret = (New yfrog(tw.AccessToken, tw.AccessTokenSecret)).GetMaxFileSize(ext)
139             Case "Plixi"
140                 ret = (New TwitPic(tw.AccessToken, tw.AccessTokenSecret)).GetMaxFileSize(ext)
141         End Select
142         Return ret
143     End Function
144
145     Private Function UpToTwitPic(ByVal file As FileInfo, ByRef message As String, ByRef resultUpload As Boolean) As String
146         Dim content As String = ""
147         Dim ret As HttpStatusCode
148         'TwitPicへの投稿
149         Dim svc As New TwitPic(tw.AccessToken, tw.AccessTokenSecret)
150         Try
151             ret = svc.Upload(file, message, content)
152         Catch ex As Exception
153             Return "Err:" + ex.Message
154         End Try
155         Dim url As String = ""
156         If ret = HttpStatusCode.OK Then
157             Dim xd As XmlDocument = New XmlDocument()
158             Try
159                 xd.LoadXml(content)
160                 'URLの取得
161                 url = xd.SelectSingleNode("/image/url").InnerText
162             Catch ex As XmlException
163                 Return "Err:" + ex.Message
164             End Try
165         Else
166             Return "Err:" + ret.ToString
167         End If
168         'アップロードまでは成功
169         resultUpload = True
170         'Twitterへの投稿
171         '投稿メッセージの再構成
172         If message.Length + url.Length + 1 > 140 Then
173             message = message.Substring(0, 140 - url.Length - 1) + " " + url
174         Else
175             message += " " + url
176         End If
177         Return tw.PostStatus(message, 0)
178     End Function
179
180     Private Function UpToyfrog(ByVal file As FileInfo, ByRef message As String, ByRef resultUpload As Boolean) As String
181         Dim content As String = ""
182         Dim ret As HttpStatusCode
183         'yfrogへの投稿
184         Dim svc As New yfrog(tw.AccessToken, tw.AccessTokenSecret)
185         Try
186             ret = svc.Upload(file, message, content)
187         Catch ex As Exception
188             Return "Err:" + ex.Message
189         End Try
190         Dim url As String = ""
191         If ret = HttpStatusCode.OK Then
192             Dim xd As XmlDocument = New XmlDocument()
193             Try
194                 xd.LoadXml(content)
195                 'URLの取得
196                 url = xd.SelectSingleNode("/rsp/mediaurl").InnerText
197             Catch ex As XmlException
198                 Return "Err:" + ex.Message
199             End Try
200         Else
201             Return "Err:" + ret.ToString
202         End If
203         'アップロードまでは成功
204         resultUpload = True
205         'Twitterへの投稿
206         '投稿メッセージの再構成
207         If message.Length + url.Length + 1 > 140 Then
208             message = message.Substring(0, 140 - url.Length - 1) + " " + url
209         Else
210             message += " " + url
211         End If
212         Return tw.PostStatus(message, 0)
213     End Function
214
215     Private Function UpToimgly(ByVal file As FileInfo, ByRef message As String, ByRef resultUpload As Boolean) As String
216         Dim content As String = ""
217         Dim ret As HttpStatusCode
218         'img.lyへの投稿
219         Dim svc As New imgly(tw.AccessToken, tw.AccessTokenSecret)
220         Try
221             ret = svc.Upload(file, message, content)
222         Catch ex As Exception
223             Return "Err:" + ex.Message
224         End Try
225         Dim url As String = ""
226         If ret = HttpStatusCode.OK Then
227             Dim xd As XmlDocument = New XmlDocument()
228             Try
229                 xd.LoadXml(content)
230                 'URLの取得
231                 url = xd.SelectSingleNode("/image/url").InnerText
232             Catch ex As XmlException
233                 Return "Err:" + ex.Message
234             End Try
235         Else
236             Return "Err:" + ret.ToString
237         End If
238         'アップロードまでは成功
239         resultUpload = True
240         'Twitterへの投稿
241         '投稿メッセージの再構成
242         If message.Length + url.Length + 1 > 140 Then
243             message = message.Substring(0, 140 - url.Length - 1) + " " + url
244         Else
245             message += " " + url
246         End If
247         Return tw.PostStatus(message, 0)
248     End Function
249
250     Private Function UpToPlixi(ByVal file As FileInfo, ByRef message As String, ByRef resultUpload As Boolean) As String
251         Dim content As String = ""
252         Dim ret As HttpStatusCode
253         'Plixiへの投稿
254         Dim svc As New Plixi(tw.AccessToken, tw.AccessTokenSecret)
255         Try
256             ret = svc.Upload(file, message, content)
257         Catch ex As Exception
258             Return "Err:" + ex.Message
259         End Try
260         Dim url As String = ""
261         If ret = HttpStatusCode.Created Then
262             Dim xd As XmlDocument = New XmlDocument()
263             Try
264                 xd.LoadXml(content)
265                 'MediaUrlの取得
266                 url = xd.ChildNodes().Item(0).ChildNodes(2).InnerText
267             Catch ex As XmlException
268                 Return "Err:" + ex.Message
269             End Try
270         Else
271             Return "Err:" + ret.ToString
272         End If
273         'アップロードまでは成功
274         resultUpload = True
275         'Twitterへの投稿
276         '投稿メッセージの再構成
277         If message.Length + url.Length + 1 > 140 Then
278             message = message.Substring(0, 140 - url.Length - 1) + " " + url
279         Else
280             message += " " + url
281         End If
282         Return tw.PostStatus(message, 0)
283     End Function
284
285     'Private Function UpToTwitVideo(ByVal file As FileInfo, ByRef message As String, ByRef resultUpload As Boolean) As String
286     '    Dim content As String = ""
287     '    Dim ret As HttpStatusCode
288     '    'TwitVideoへの投稿
289     '    Dim svc As New TwitVideo
290     '    Try
291     '        ret = svc.Upload(file, message, "", tw.Username, tw.UserIdNo, content)
292     '    Catch ex As Exception
293     '        Return "Err:" + ex.Message
294     '    End Try
295     '    Dim url As String = ""
296     '    If ret = HttpStatusCode.OK Then
297     '        Dim xd As XmlDocument = New XmlDocument()
298     '        Try
299     '            xd.LoadXml(content)
300     '            Dim rslt As String = xd.SelectSingleNode("/rsp/@status").Value
301     '            If rslt = "ok" Then
302     '                'URLの取得
303     '                url = xd.SelectSingleNode("/rsp/mediaurl").InnerText
304     '            Else
305     '                Return "Err:" + xd.SelectSingleNode("/rsp/err/@msg").Value
306     '            End If
307     '        Catch ex As XmlException
308     '            Return "Err:" + ex.Message
309     '        End Try
310     '    Else
311     '        Return "Err:" + ret.ToString
312     '    End If
313     '    'アップロードまでは成功
314     '    resultUpload = True
315     '    'Twitterへの投稿
316     '    '投稿メッセージの再構成
317     '    If message.Length + url.Length + 1 > 140 Then
318     '        message = message.Substring(0, 140 - url.Length - 1) + " " + url
319     '    Else
320     '        message += " " + url
321     '    End If
322     '    Return tw.PostStatus(message, 0)
323     'End Function
324
325     Public Sub New(ByVal twInstance As Twitter)
326         tw = twInstance
327     End Sub
328 End Class