#include-once #include #include #include #include #include #Region Header #cs Title: Twitter UDF Library for AutoIt3 Filename: Twitter.au3 Description: Automate Twitter using OAuth and the Twitter API Author: seangriffin Version: V0.1 Last Update: 02/07/10 Requirements: AutoIt3 3.2 or higher Changelog: ---------02/07/10---------- v0.1 Initial release. #ce #EndRegion Header #Region Global Variables and Constants Global Const $_Twitter_html1 = _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "
" & @CRLF & _ "
" & @CRLF Global Const $_Twitter_html2 = _ "
" & @CRLF & _ "
" & @CRLF Global Const $_Twitter_html3 = _ "
" & @CRLF & _ "" & @CRLF & _ "" & @CRLF Global $_Twitter_oauth_consumer_key_html, $_Twitter_oauth_token_html, $_Twitter_oauth_signature_method_html Global $_Twitter_oauth_verifier_html, $_Twitter_oauth_timestamp_html, $_Twitter_oauth_nonce_html, $_Twitter_oauth_signature_html Global $_Twitter_url_html, $_Twitter_consumer_secret_html, $_Twitter_token_secret_html Global $_Twitter_message_html, $_Twitter_lat_html, $_Twitter_long_html #EndRegion Global Variables and Constants #Region Core functions ; #FUNCTION# ;=============================================================================== ; ; Name...........: _GUICtrlTwitter_Create() ; Description ...: Creates a Twitter control. ; Syntax.........: _GUICtrlTwitter_Create(ByRef $twitter, $left, $top, $width, $height) ; Parameters ....: $twitter - The embedded Twitter object, required by the _GUICtrlTwitter functions below. ; $left - The left side of the control. ; $top - The top of the control. ; $width - The width of the control. ; $height - The height of the control. ; Return values .: On Success - Returns the identifier (controlID) of the new control. ; On Failure - Returns False. ; Author ........: seangriffin ; Modified.......: ; Remarks .......: The main purpose of having a visual control for Twitter (such as this one) ; is for performing the required web-based username and password authentication ; (see the function "_GUICtrlTwitter_Authenticate"). ; Related .......: ; Link ..........: ; Example .......: Yes ; ; ;========================================================================================== func _GUICtrlTwitter_Create(ByRef $twitter, $left, $top, $width, $height) dim $twitter_ctrl if IsObj($twitter) = False Then $twitter = _IECreateEmbedded () $twitter_ctrl = GUICtrlCreateObj($twitter, $left, $top, $width, $height) _IENavigate($twitter, "about:blank") EndIf Return $twitter_ctrl EndFunc ; #FUNCTION# ;=============================================================================== ; ; Name...........: _GUICtrlTwitter_Authenticate() ; Description ...: Performs the entire OAuth / Twitter user authentication process ; (getting a request token, user login and PIN retrieval, and getting an access token). ; Syntax.........: _GUICtrlTwitter_Authenticate($twitter, $twitter_ctrl, $consumer_key, $consumer_secret, $hide_token_authetication = True) ; Parameters ....: $twitter - The Twitter object from the function "_GUICtrlTwitter_Create". ; $twitter_ctrl - The Twitter control from the function "_GUICtrlTwitter_Create". ; $consumer_key - The consumer key of the client application (calling this UDF and) registered with Twitter. ; $consumer_secret - The consumer secret of the client application (calling this UDF and) registered with Twitter. ; $hide_token_authetication - True = Hide the Twitter control whilst token authentication is occurring. ; False = Show the Twitter control whilst token authentication is occurring. ; Return values .: On Success - Returns True. ; On Failure - Returns False. ; Author ........: seangriffin ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; ; ;========================================================================================== func _GUICtrlTwitter_Authenticate($twitter, $twitter_ctrl, $consumer_key, $consumer_secret, $hide_token_authetication = True) dim $html ; Step 1 - Get a Request Token $_Twitter_oauth_consumer_key_html = " consumer key:
" & @CRLF $_Twitter_oauth_signature_method_html = " signature method: " & @CRLF $_Twitter_oauth_timestamp_html = " " & @CRLF $_Twitter_oauth_nonce_html = " " & @CRLF $_Twitter_oauth_signature_html = " " & @CRLF $_Twitter_url_html = " URL:
" & @CRLF $_Twitter_consumer_secret_html = " consumer secret:
" & @CRLF $_Twitter_token_secret_html = " " & @CRLF $html = $_Twitter_html1 & $_Twitter_oauth_consumer_key_html & $_Twitter_oauth_signature_method_html & $_Twitter_oauth_timestamp_html & $_Twitter_oauth_nonce_html & $_Twitter_oauth_signature_html & $_Twitter_html2 & $_Twitter_url_html & $_Twitter_consumer_secret_html & $_Twitter_token_secret_html & $_Twitter_html3 if $hide_token_authetication = True Then GUICtrlSetState($twitter_ctrl, $GUI_HIDE) _IEDocWriteHTML($twitter, $html) ; Submit the form $form = _IEFormGetObjByName($twitter, "request") $submit = _IEFormElementGetObjByName($form, "submit") _IEAction($submit, "click") ; Get the response _IELoadWait($twitter) $response = _IEBodyReadText($twitter) $response_part = StringSplit($response, "&") $oauth_token = StringReplace($response_part[1], "oauth_token=", "") $oauth_token_secret = StringReplace($response_part[2], "oauth_token_secret=", "") ; Step 2 - User login and PIN retrieval _IENavigate($twitter, "https://twitter.com/oauth/authorize?oauth_token=" & $oauth_token) if $hide_token_authetication = True Then GUICtrlSetState($twitter_ctrl, $GUI_SHOW) Do Sleep(250) $response = _IEBodyReadText($twitter) Until StringInStr($response, "successfully granted access") if $hide_token_authetication = True Then GUICtrlSetState($twitter_ctrl, $GUI_HIDE) $number = StringRegExp($response, "[0-9]+", 1) $pin = $number[0] ; Step 3 - Get an Access Token $_Twitter_oauth_token_html = " request token:
" & @CRLF $_Twitter_oauth_verifier_html = " PIN: " & @CRLF $_Twitter_url_html = " URL:
" & @CRLF $_Twitter_token_secret_html = " token secret: " & @CRLF $html = $_Twitter_html1 & $_Twitter_oauth_consumer_key_html & $_Twitter_oauth_token_html & $_Twitter_oauth_signature_method_html & $_Twitter_oauth_verifier_html & $_Twitter_oauth_timestamp_html & $_Twitter_oauth_nonce_html & $_Twitter_oauth_signature_html & $_Twitter_html2 & $_Twitter_url_html & $_Twitter_consumer_secret_html & $_Twitter_token_secret_html & $_Twitter_html3 _IEDocWriteHTML($twitter, $html) ; Submit the form $form = _IEFormGetObjByName($twitter, "request") $submit = _IEFormElementGetObjByName($form, "submit") _IEAction($submit, "click") ; Get the response _IELoadWait($twitter) $response = _IEBodyReadText($twitter) $response_part = StringSplit($response, "&") $oauth_token = StringReplace($response_part[1], "oauth_token=", "") $oauth_token_secret = StringReplace($response_part[2], "oauth_token_secret=", "") $user_id = StringReplace($response_part[3], "user_id=", "") $screen_name = StringReplace($response_part[4], "screen_name=", "") $_Twitter_oauth_token_html = " request token:
" & @CRLF $_Twitter_token_secret_html = " token secret: " & @CRLF Return True EndFunc ; #FUNCTION# ;=============================================================================== ; ; Name...........: _GUICtrlTwitter_UpdateStatus() ; Description ...: Posts a Tweet (updates the status) for the authenticated person. ; Syntax.........: _GUICtrlTwitter_UpdateStatus(ByRef $twitter, $message, $lat, $long) ; Parameters ....: $twitter - The Twitter object from the function "_GUICtrlTwitter_Create". ; $message - The message (Tweet) to post. ; $lat - The latitude of the location this tweet refers to. ; Will not be used if an empty string. ; $long - The longitude of the location this tweet refers to. ; Will not be used if an empty string. ; Return values .: On Success - Returns an array with the latitude and longitude of the address. ; On Failure - Returns False. ; Author ........: seangriffin ; Modified.......: ; Remarks .......: The function "_GUICtrlTwitter_Authenticate" must have be called prior to ; calling this function. ; Related .......: ; Link ..........: ; Example .......: Yes ; ; ;========================================================================================== func _GUICtrlTwitter_UpdateStatus(ByRef $twitter, $message, $lat = "", $long = "") $_Twitter_message_html = " message:
" & @CRLF if StringLen($lat) > 0 Then $_Twitter_lat_html = " latitude:
" & @CRLF Else $_Twitter_lat_html = "" EndIf if StringLen($long) > 0 Then $_Twitter_long_html = " longitude:
" & @CRLF Else $_Twitter_long_html = "" EndIf $_Twitter_url_html = " URL:
" & @CRLF $html = $_Twitter_html1 & $_Twitter_message_html & $_Twitter_lat_html & $_Twitter_long_html & $_Twitter_oauth_consumer_key_html & $_Twitter_oauth_token_html & $_Twitter_oauth_signature_method_html & $_Twitter_oauth_timestamp_html & $_Twitter_oauth_nonce_html & $_Twitter_oauth_signature_html & $_Twitter_html2 & $_Twitter_url_html & $_Twitter_consumer_secret_html & $_Twitter_token_secret_html & $_Twitter_html3 _IEDocWriteHTML($twitter, $html) ; Submit the form $form = _IEFormGetObjByName($twitter, "request") $submit = _IEFormElementGetObjByName($form, "submit") _IEAction($submit, "click") EndFunc ; #FUNCTION# ;=============================================================================== ; ; Name...........: _GUICtrlTwitter_Search() ; Description ...: Returns tweets that match a specified query. ; Syntax.........: _GUICtrlTwitter_Search($search_text, $result_type = "mixed") ; Parameters ....: $search_text - the search query ; $result_type - "mixed": Include both popular and real time results in the response. ; "recent": return only the most recent results in the response ; "popular": return only the most popular results in the response ; Return values .: On Success - Returns a "Scripting.Dictionary" object of details for the listing. ; The key of each dictionary item is formatted as follows: ; "entry." ; On Failure - Returns False. ; Author ........: seangriffin ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; ; ;========================================================================================== func _GUICtrlTwitter_Search($search_text, $result_type = "mixed") Local $search_result = ObjCreate("Scripting.Dictionary") $search_text = StringReplace($search_text, " ", "+") $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET","http://search.twitter.com/search.atom?q=" & $search_text & "&result_type=" & $result_type) $oHTTP.Send() $HTMLSource = $oHTTP.Responsetext $entry = StringSplit($HTMLSource, "", 1) _ArrayDelete($entry, 0) _ArrayDelete($entry, 0) $entry_num = 1 for $each in $entry $entry_attribute = StringSplit($each, @LF, 1) _ArrayDelete($entry_attribute, 0) _ArrayDelete($entry_attribute, 0) _ArrayDelete($entry_attribute, UBound($entry_attribute - 2)) _ArrayDelete($entry_attribute, UBound($entry_attribute - 2)) for $each_attribute in $entry_attribute if StringInStr($each_attribute, ">") > 0 And (StringInStr($each_attribute, ">") <> StringLen($each_attribute)) Then $each_attribute_part = StringSplit($each_attribute, "<>") ;_ArrayDisplay($each_attribute_part) $search_result.item("entry" & $entry_num & "." & $each_attribute_part[2]) = $each_attribute_part[3] EndIf Next $entry_num = $entry_num + 1 Next return $search_result EndFunc