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>
9 ' This file is part of Tween.
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)
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
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.
29 Imports System.Threading
31 Public Class HttpTwitter
38 Private Const ConsumerKey As String = "tLbG3uS0BIIE8jm1mKzKOfZ6EgUOmWVM"
41 '''OAuthの署名作成用秘密コンシューマーデータ
43 Private Const ConsumerSecret As String = "M0IMsbl2722iWa+CGPVcNeQmE+TFpJk8B/KW9UUTk3eLOl9Ij005r52JNxVukTzM"
45 '''OAuthのアクセストークン取得先URI
47 Private Const AccessTokenUrlXAuth As String = "https://api.twitter.com/oauth/access_token"
49 Private Shared _protocol As String = "http://"
51 Private Const PostMethod As String = "POST"
52 Private Const GetMethod As String = "GET"
54 Private httpCon As IHttpConnection 'HttpConnectionApi or HttpConnectionOAuth
55 Private httpConVar As New HttpVarious
57 Private Enum AuthMethod
61 Private connectionType As AuthMethod = AuthMethod.Basic
63 Public Sub Initialize(ByVal accessToken As String, _
64 ByVal accessTokenSecret As String, _
65 ByVal username As String)
67 Dim con As New HttpOAuthApiProxy
68 Static tk As String = ""
69 Static tks As String = ""
70 Static un As String = ""
71 If tk <> accessToken OrElse tks <> accessTokenSecret OrElse _
72 un <> username OrElse connectionType <> AuthMethod.OAuth Then
73 ' 以前の認証状態よりひとつでも変化があったらhttpヘッダより読み取ったカウントは初期化
75 tks = accessTokenSecret
78 con.Initialize(DecryptString(ConsumerKey), DecryptString(ConsumerSecret), accessToken, accessTokenSecret, username, "screen_name")
80 connectionType = AuthMethod.OAuth
83 Public Sub Initialize(ByVal username As String, _
84 ByVal password As String)
86 Dim con As New HttpConnectionBasic
87 Static un As String = ""
88 Static pw As String = ""
89 If un <> username OrElse pw <> password OrElse connectionType <> AuthMethod.Basic Then
90 ' 以前の認証状態よりひとつでも変化があったらhttpヘッダより読み取ったカウントは初期化
94 con.Initialize(username, password)
96 connectionType = AuthMethod.Basic
99 Public ReadOnly Property AccessToken() As String
101 If httpCon IsNot Nothing Then
102 If connectionType = AuthMethod.Basic Then Return ""
103 Return DirectCast(httpCon, HttpConnectionOAuth).AccessToken
110 Public ReadOnly Property AccessTokenSecret() As String
112 If httpCon IsNot Nothing Then
113 If connectionType = AuthMethod.Basic Then Return ""
114 Return DirectCast(httpCon, HttpConnectionOAuth).AccessTokenSecret
121 Public ReadOnly Property AuthenticatedUsername() As String
123 If httpCon IsNot Nothing Then
124 Return httpCon.AuthUsername
131 Public ReadOnly Property Password() As String
133 If httpCon IsNot Nothing Then
135 If connectionType = AuthMethod.Basic Then Return DirectCast(httpCon, HttpConnectionBasic).Password
143 Public Function AuthUserAndPass(ByVal username As String, ByVal password As String, ByRef content As String) As HttpStatusCode
144 If connectionType = AuthMethod.Basic Then
145 Return httpCon.Authenticate(CreateTwitterUri("/1/account/verify_credentials.json"), username, password, content)
147 Return httpCon.Authenticate(New Uri(AccessTokenUrlXAuth), username, password, content)
151 Public Sub ClearAuthInfo()
152 If connectionType = AuthMethod.Basic Then
153 Me.Initialize("", "")
155 Me.Initialize("", "", "")
159 Public Shared WriteOnly Property UseSsl() As Boolean
160 Set(ByVal value As Boolean)
162 _protocol = "https://"
164 _protocol = "http://"
169 Public Function UpdateStatus(ByVal status As String, ByVal replyToId As Long, ByRef content As String) As HttpStatusCode
170 Dim param As New Dictionary(Of String, String)
171 param.Add("status", status)
172 If connectionType = AuthMethod.Basic Then param.Add("source", "Tween")
173 If replyToId > 0 Then param.Add("in_reply_to_status_id", replyToId.ToString)
175 Return httpCon.GetContent(PostMethod, _
176 CreateTwitterUri("/1/statuses/update.json"), _
183 Public Function DestroyStatus(ByVal id As Long) As HttpStatusCode
184 Return httpCon.GetContent(PostMethod, _
185 CreateTwitterUri("/1/statuses/destroy/" + id.ToString + ".json"), _
192 Public Function SendDirectMessage(ByVal status As String, ByVal sendto As String, ByRef content As String) As HttpStatusCode
193 Dim param As New Dictionary(Of String, String)
194 param.Add("text", status)
195 param.Add("screen_name", sendto)
197 Return httpCon.GetContent(PostMethod, _
198 CreateTwitterUri("/1/direct_messages/new.json"), _
205 Public Function DestroyDirectMessage(ByVal id As Long) As HttpStatusCode
206 Return httpCon.GetContent(PostMethod, _
207 CreateTwitterUri("/1/direct_messages/destroy/" + id.ToString + ".json"), _
214 Public Function RetweetStatus(ByVal id As Long, ByRef content As String) As HttpStatusCode
215 Return httpCon.GetContent(PostMethod, _
216 CreateTwitterUri("/1/statuses/retweet/" + id.ToString() + ".json"), _
223 Public Function ShowUserInfo(ByVal screenName As String, ByRef content As String) As HttpStatusCode
224 Dim param As New Dictionary(Of String, String)
225 param.Add("screen_name", screenName)
226 Return httpCon.GetContent(GetMethod, _
227 CreateTwitterUri("/1/users/show.json"), _
230 TwitterApiInfo.HttpHeaders, _
231 AddressOf GetApiCallback)
234 Public Function CreateFriendships(ByVal screenName As String, ByRef content As String) As HttpStatusCode
235 Dim param As New Dictionary(Of String, String)
236 param.Add("screen_name", screenName)
238 Return httpCon.GetContent(PostMethod, _
239 CreateTwitterUri("/1/friendships/create.json"), _
246 Public Function DestroyFriendships(ByVal screenName As String, ByRef content As String) As HttpStatusCode
247 Dim param As New Dictionary(Of String, String)
248 param.Add("screen_name", screenName)
250 Return httpCon.GetContent(PostMethod, _
251 CreateTwitterUri("/1/friendships/destroy.json"), _
258 Public Function CreateBlock(ByVal screenName As String, ByRef content As String) As HttpStatusCode
259 Dim param As New Dictionary(Of String, String)
260 param.Add("screen_name", screenName)
262 Return httpCon.GetContent(PostMethod, _
263 CreateTwitterUri("/1/blocks/create.json"), _
270 Public Function DestroyBlock(ByVal screenName As String, ByRef content As String) As HttpStatusCode
271 Dim param As New Dictionary(Of String, String)
272 param.Add("screen_name", screenName)
274 Return httpCon.GetContent(PostMethod, _
275 CreateTwitterUri("/1/blocks/destroy.json"), _
282 Public Function ReportSpam(ByVal screenName As String, ByRef content As String) As HttpStatusCode
283 Dim param As New Dictionary(Of String, String)
284 param.Add("screen_name", screenName)
286 Return httpCon.GetContent(PostMethod, _
287 CreateTwitterUri("/1/report_spam.json"), _
294 Public Function ShowFriendships(ByVal souceScreenName As String, ByVal targetScreenName As String, ByRef content As String) As HttpStatusCode
295 Dim param As New Dictionary(Of String, String)
296 param.Add("source_screen_name", souceScreenName)
297 param.Add("target_screen_name", targetScreenName)
299 Return httpCon.GetContent(GetMethod, _
300 CreateTwitterUri("/1/friendships/show.json"), _
303 TwitterApiInfo.HttpHeaders, _
304 AddressOf GetApiCallback)
307 Public Function ShowStatuses(ByVal id As Long, ByRef content As String) As HttpStatusCode
308 Return httpCon.GetContent(GetMethod, _
309 CreateTwitterUri("/1/statuses/show/" + id.ToString() + ".json"), _
312 TwitterApiInfo.HttpHeaders, _
313 AddressOf GetApiCallback)
316 Public Function CreateFavorites(ByVal id As Long, ByRef content As String) As HttpStatusCode
317 Return httpCon.GetContent(PostMethod, _
318 CreateTwitterUri("/1/favorites/create/" + id.ToString() + ".json"), _
325 Public Function DestroyFavorites(ByVal id As Long, ByRef content As String) As HttpStatusCode
326 Return httpCon.GetContent(PostMethod, _
327 CreateTwitterUri("/1/favorites/destroy/" + id.ToString() + ".json"), _
334 Public Function HomeTimeline(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
335 Dim param As New Dictionary(Of String, String)
337 param.Add("count", count.ToString())
340 param.Add("max_id", max_id.ToString())
343 param.Add("since_id", since_id.ToString())
346 param.Add("include_entities", "true")
348 Return httpCon.GetContent(GetMethod, _
349 CreateTwitterUri("/1/statuses/home_timeline.json"), _
352 TwitterApiInfo.HttpHeaders, _
353 AddressOf GetApiCallback)
356 Public Function UserTimeline(ByVal user_id As Long, ByVal screen_name As String, ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
357 Dim param As New Dictionary(Of String, String)
359 If (user_id = 0 AndAlso String.IsNullOrEmpty(screen_name)) OrElse
360 (user_id <> 0 AndAlso Not String.IsNullOrEmpty(screen_name)) Then Return HttpStatusCode.BadRequest
363 param.Add("user_id", user_id.ToString())
365 If Not String.IsNullOrEmpty(screen_name) Then
366 param.Add("screen_name", screen_name)
369 param.Add("count", count.ToString())
372 param.Add("max_id", max_id.ToString())
375 param.Add("since_id", since_id.ToString())
378 param.Add("include_rts", "true")
379 param.Add("include_entities", "true")
381 Return httpCon.GetContent(GetMethod, _
382 CreateTwitterUri("/1/statuses/user_timeline.json"), _
385 TwitterApiInfo.HttpHeaders, _
386 AddressOf GetApiCallback)
389 Public Function PublicTimeline(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
390 Dim param As New Dictionary(Of String, String)
392 param.Add("count", count.ToString())
395 param.Add("max_id", max_id.ToString())
398 param.Add("since_id", since_id.ToString())
401 param.Add("include_entities", "true")
403 Return httpCon.GetContent(GetMethod, _
404 CreateTwitterUri("/1/statuses/public_timeline.json"), _
407 TwitterApiInfo.HttpHeaders, _
408 AddressOf GetApiCallback)
411 Public Function Mentions(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
412 Dim param As New Dictionary(Of String, String)
414 param.Add("count", count.ToString())
417 param.Add("max_id", max_id.ToString())
420 param.Add("since_id", since_id.ToString())
423 param.Add("include_entities", "true")
425 Return httpCon.GetContent(GetMethod, _
426 CreateTwitterUri("/1/statuses/mentions.json"), _
429 TwitterApiInfo.HttpHeaders, _
430 AddressOf GetApiCallback)
433 Public Function DirectMessages(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
434 Dim param As New Dictionary(Of String, String)
436 param.Add("count", count.ToString())
439 param.Add("max_id", max_id.ToString())
442 param.Add("since_id", since_id.ToString())
445 Return httpCon.GetContent(GetMethod, _
446 CreateTwitterUri("/1/direct_messages.json"), _
449 TwitterApiInfo.HttpHeaders, _
450 AddressOf GetApiCallback)
453 Public Function DirectMessagesSent(ByVal count As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
454 Dim param As New Dictionary(Of String, String)
456 param.Add("count", count.ToString())
459 param.Add("max_id", max_id.ToString())
462 param.Add("since_id", since_id.ToString())
465 Return httpCon.GetContent(GetMethod, _
466 CreateTwitterUri("/1/direct_messages/sent.json"), _
469 TwitterApiInfo.HttpHeaders, _
470 AddressOf GetApiCallback)
473 Public Function Favorites(ByVal count As Integer, ByRef content As String) As HttpStatusCode
474 Dim param As New Dictionary(Of String, String)
475 If count <> 20 Then param.Add("count", count.ToString())
477 Return httpCon.GetContent(GetMethod, _
478 CreateTwitterUri("/1/favorites.json"), _
481 TwitterApiInfo.HttpHeaders, _
482 AddressOf GetApiCallback)
485 Public Function Search(ByVal words As String, ByVal lang As String, ByVal rpp As Integer, ByVal page As Integer, ByVal sinceId As Long, ByRef content As String) As HttpStatusCode
486 Dim param As New Dictionary(Of String, String)
487 If Not String.IsNullOrEmpty(words) Then param.Add("q", words)
488 If Not String.IsNullOrEmpty(lang) Then param.Add("lang", lang)
489 If rpp > 0 Then param.Add("rpp", rpp.ToString())
490 If page > 0 Then param.Add("page", page.ToString())
491 If sinceId > 0 Then param.Add("since_id", sinceId.ToString)
493 If param.Count = 0 Then Return HttpStatusCode.BadRequest
495 Return httpConVar.GetContent(GetMethod, _
496 CreateTwitterSearchUri("/search.atom"), _
503 Public Function FollowerIds(ByVal cursor As Long, ByRef content As String) As HttpStatusCode
504 Dim param As New Dictionary(Of String, String)
505 param.Add("cursor", cursor.ToString())
507 Return httpCon.GetContent(GetMethod, _
508 CreateTwitterUri("/1/followers/ids.json"), _
511 TwitterApiInfo.HttpHeaders, _
512 AddressOf GetApiCallback)
515 Public Function RateLimitStatus(ByRef content As String) As HttpStatusCode
516 Return httpCon.GetContent(GetMethod, _
517 CreateTwitterUri("/1/account/rate_limit_status.json"), _
524 Public Function GetLists(ByVal user As String, ByVal cursor As Long, ByRef content As String) As HttpStatusCode
525 Dim param As New Dictionary(Of String, String)
526 param.Add("cursor", cursor.ToString)
527 Return httpCon.GetContent(GetMethod, _
528 CreateTwitterUri("/1/" + user + "/lists.json"), _
531 TwitterApiInfo.HttpHeaders, _
532 AddressOf GetApiCallback)
535 Public Function PostListID(ByVal user As String, ByVal list_id As String, ByVal name As String, ByVal mode As String, ByVal description As String, ByRef content As String) As HttpStatusCode
536 Dim param As New Dictionary(Of String, String)
537 If name IsNot Nothing Then param.Add("name", name)
538 If mode IsNot Nothing Then param.Add("mode", mode)
539 If description IsNot Nothing Then param.Add("description", description)
541 Return httpCon.GetContent(PostMethod, _
542 CreateTwitterUri("/1/" + user + "/lists/" + list_id + ".json"), _
549 Public Function DeleteListID(ByVal user As String, ByVal list_id As String, ByRef content As String) As HttpStatusCode
550 Dim param As New Dictionary(Of String, String)
551 param.Add("_method", "DELETE")
553 Return httpCon.GetContent(PostMethod, _
554 CreateTwitterUri("/1/" + user + "/lists/" + list_id + ".json"), _
561 Public Function GetListsSubscriptions(ByVal user As String, ByVal cursor As Long, ByRef content As String) As HttpStatusCode
562 Dim param As New Dictionary(Of String, String)
563 param.Add("cursor", cursor.ToString)
564 Return httpCon.GetContent(GetMethod, _
565 CreateTwitterUri("/1/" + user + "/lists/subscriptions.json"), _
568 TwitterApiInfo.HttpHeaders, _
569 AddressOf GetApiCallback)
572 Public Function GetListsStatuses(ByVal user As String, ByVal list_id As String, ByVal per_page As Integer, ByVal max_id As Long, ByVal since_id As Long, ByRef content As String) As HttpStatusCode
573 '認証なくても取得できるが、protectedユーザー分が抜ける
574 Dim param As New Dictionary(Of String, String)
576 param.Add("per_page", per_page.ToString())
579 param.Add("max_id", max_id.ToString())
582 param.Add("since_id", since_id.ToString())
585 Return httpCon.GetContent(GetMethod, _
586 CreateTwitterUri("/1/" + user + "/lists/" + list_id + "/statuses.json"), _
589 TwitterApiInfo.HttpHeaders, _
590 AddressOf GetApiCallback)
593 Public Function PostLists(ByVal user As String, ByVal listname As String, ByVal isPrivate As Boolean, ByVal description As String, ByRef content As String) As HttpStatusCode
594 Dim mode As String = "public"
599 Dim param As New Dictionary(Of String, String)
600 param.Add("name", listname)
601 param.Add("mode", mode)
602 If Not String.IsNullOrEmpty(description) Then
603 param.Add("description", description)
605 Return httpCon.GetContent(PostMethod, _
606 CreateTwitterUri("/1/" + user + "/lists.json"), _
613 Public Function GetListMembers(ByVal user As String, ByVal list_id As String, ByVal cursor As Long, ByRef content As String) As HttpStatusCode
614 Dim param As New Dictionary(Of String, String)
615 param.Add("cursor", cursor.ToString())
616 Return httpCon.GetContent(GetMethod, _
617 CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
620 TwitterApiInfo.HttpHeaders, _
621 AddressOf GetApiCallback)
624 Public Function PostListMembers(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
625 Dim param As New Dictionary(Of String, String)
627 Return httpCon.GetContent(PostMethod, _
628 CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
635 Public Function DeleteListMembers(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
636 Dim param As New Dictionary(Of String, String)
638 param.Add("_method", "DELETE")
639 Return httpCon.GetContent(PostMethod, _
640 CreateTwitterUri("/1/" + user + "/" + list_id + "/members.json"), _
647 Public Function GetListMembersID(ByVal user As String, ByVal list_id As String, ByVal id As String, ByRef content As String) As HttpStatusCode
648 Return httpCon.GetContent(GetMethod, _
649 CreateTwitterUri("/1/" + user + "/" + list_id + "/members/" + id + ".json"), _
652 TwitterApiInfo.HttpHeaders, _
653 AddressOf GetApiCallback)
656 Public Function Statusid_retweeted_by_ids(ByVal statusid As Long, ByVal count As Integer, ByVal page As Integer, ByRef content As String) As HttpStatusCode
657 Dim param As New Dictionary(Of String, String)
659 param.Add("count", count.ToString())
662 param.Add("page", page.ToString())
665 Return httpCon.GetContent(GetMethod, _
666 CreateTwitterUri("/1/statuses/" + statusid.ToString + "/retweeted_by/ids.json"), _
669 TwitterApiInfo.HttpHeaders, _
670 AddressOf GetApiCallback)
673 Public Function UpdateProfile(ByVal name As String, ByVal url As String, ByVal location As String, ByVal description As String, ByRef content As String) As HttpStatusCode
674 Dim param As New Dictionary(Of String, String)
676 param.Add("name", name)
677 param.Add("url", url)
678 param.Add("location", location)
679 param.Add("description", description)
681 Return httpCon.GetContent(PostMethod, _
682 CreateTwitterUri("/1/account/update_profile.json"), _
689 Public Function UpdateProfileImage(ByVal imageFile As FileInfo, ByRef content As String) As HttpStatusCode
690 Dim binary As New List(Of KeyValuePair(Of String, FileInfo))
691 binary.Add(New KeyValuePair(Of String, FileInfo)("image", imageFile))
693 Return httpCon.GetContent(PostMethod, _
694 CreateTwitterUri("/1/account/update_profile_image.json"), _
702 Public Function GetRelatedResults(ByVal id As Long, ByRef content As String) As HttpStatusCode
703 '認証なくても取得できるが、protectedユーザー分が抜ける
705 Return httpCon.GetContent(GetMethod, _
706 CreateTwitterUri("/1/related_results/show/" + id.ToString + ".json"), _
709 TwitterApiInfo.HttpHeaders, _
710 AddressOf GetApiCallback)
715 Private Shared _twitterUrl As String = "api.twitter.com"
716 Private Shared _TwitterSearchUrl As String = "search.twitter.com"
717 Private Shared _twitterStreamUrl As String = "userstream.twitter.com"
719 Private Function CreateTwitterUri(ByVal path As String) As Uri
720 Return New Uri(String.Format("{0}{1}{2}", _protocol, _twitterUrl, path))
723 Private Function CreateTwitterSearchUri(ByVal path As String) As Uri
724 Return New Uri(String.Format("{0}{1}{2}", _protocol, _TwitterSearchUrl, path))
727 Private Function CreateTwitterStreamUri(ByVal path As String) As Uri
728 Return New Uri(String.Format("{0}{1}{2}", "https://", _twitterStreamUrl, path))
731 Public Shared WriteOnly Property TwitterUrl() As String
732 Set(ByVal value As String)
734 HttpOAuthApiProxy.ProxyHost = value
738 Public Shared WriteOnly Property TwitterSearchUrl() As String
739 Set(ByVal value As String)
740 _TwitterSearchUrl = value
745 Private Sub GetApiCallback(ByVal sender As Object, ByRef code As HttpStatusCode, ByRef content As String)
746 If code < HttpStatusCode.InternalServerError Then
747 TwitterApiInfo.ParseHttpHeaders(TwitterApiInfo.HttpHeaders)
751 Public Function UserStream(ByRef content As Stream,
752 ByVal allAtReplies As Boolean,
753 ByVal trackwords As String,
754 ByVal userAgent As String) As HttpStatusCode
755 Dim param As New Dictionary(Of String, String)
758 param.Add("replies", "all")
761 If Not String.IsNullOrEmpty(trackwords) Then
762 param.Add("track", trackwords)
765 Return httpCon.GetContent(GetMethod, _
766 CreateTwitterStreamUri("/2/user.json"), _
772 Public Sub RequestAbort()
773 httpCon.RequestAbort()
776 Public Function Clone() As Object Implements System.ICloneable.Clone
777 Dim myCopy As New HttpTwitter
778 If Me.connectionType = AuthMethod.Basic Then
779 myCopy.Initialize(Me.AuthenticatedUsername, Me.Password)
781 myCopy.Initialize(Me.AccessToken, Me.AccessTokenSecret, Me.AuthenticatedUsername)