Jump to content

Google Suggest UDF/GUI


ConsultingJoe
 Share

Recommended Posts

NEWER VERSION (FASTER) (uses INetGetSource/Plain Text)

#include <HTTP.au3>
#include <INET.au3>
#include <GUIConstants.au3>

_INetGetSource( "google.com/complete/search?q=anything&output=firefox" )

$text = ""
$Form1 = GUICreate("Google Suggest", 153, 200, 193, 115)
$Input1 = GUICtrlCreateInput("", 0, 8, 153, 20)
$list1 = GUICtrlCreateList("", 0, 30, 153, 175)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If $text <> GUICtrlRead( $Input1 ) Then
        $text = GUICtrlRead( $Input1 )
        $data = _suggest( $text )
        GUICtrlSetData( $list1, "" )
        GUICtrlSetData( $list1, StringReplace( $data, ",", "|" ) )
    EndIf
WEnd

Func _suggest($text);NEEDED for UDF
    $read = _INetGetSource( "google.com/complete/search?q="&_HTTPEncodeString($text)&"&output=firefox" )
    $read = StringReplace( $read, "&apos;", "'" )
    $read = StringReplace( $read, "[", "" )
    $read = StringReplace( $read, "]", "" )
    $read = StringReplace( $read, '"', "" )
    Return $read
EndFuncoÝ÷ ØâÃDE"5"ÎXD[ºØZ°*.Û¬zÄÂ?õÌ-«­¢+Ø¥¹±Õ±ÐíU%
½¹ÍѹÑ̹ÔÌÐì(¥¹±Õ±ÐíMÑÉ¥¹¹ÔÌÐì(¥¹±Õ±ÐíAɽÍ̹ÔÌÐì(¥¹±Õ±ÐíÉÉä¹ÔÌÐì(¥¹±Õ±Ðí!QQ@¹ÔÌÐì((I¥½¸MQIP-½U$ÍÑ¥½¸½É´ô(ÀÌØí½É´ÄôU%
ÉÑ ÅÕ½Ðí½½±MÕÍÐÅÕ½Ðì°ÌÐÜ°ÈÀÀ°ÄäÌ°ÄÄÔ¤(ÀÌØí%¹ÁÕÐÄôU%
Ñɱ
ÉÑ%¹ÁÕÐ ÅÕ½ÐìÅÕ½Ðì°à°ÈаÈÌÌ°ÈÀ¤(ÀÌØí
½µ¼ÄôU%
Ñɱ
ÉÑ1¥ÍÐ ÅÕ½ÐìÅÕ½Ðì°à°ÐаÈÌÌ°ÄÐÔ¤(ÀÌØí
½µ¼ÄÈôU%
Ñɱ
ÉÑ1¥ÍÐ ÅÕ½ÐìÅÕ½Ðì°ÈÐÀ°ÐаÄÀÀ°ÄÐÔ¤(ÀÌØí   ÕÑѽ¸ÄôU%
Ñɱ
ÉÑ    ÕÑѽ¸ ÅÕ½ÐíMÉ ½½±ÅÕ½Ðì°ÈÐÔ°Èаàä°Ää°À¤)U%MÑMÑÑ¡M]}M!=¤(ÀÌØí¥¹ÁÕÑÑáÐôÅÕ½ÐìÅÕ½Ðì)±½°ÀÌØí½½Ñ°ÀÌØí½½½Õ¹Ðí9½ÈU(¹I¥½¸9-½U$ÍÑ¥½¸()]¡¥±Ä(ÀÌØíÕ¥µÍôU%Ñ5Í ¤(MÝ¥Ñ ÀÌØíÕ¥µÍ(
ÍÀÌØí  ÕÑѽ¸Ä(͹ÉÅÕÍÐ ¤(
ÍÀÌØí
½µ¼Ä(U%
ÑɱMÑMÑÑ ÀÌØí%¹ÁÕÐÄ°ÀÌØíU%}=
UL¤(U%
ÑɱMÑÑ ÀÌØí%¹ÁÕÐÄ°U%
ÑɱI ÀÌØí
½µ¼Ä¤¤(
ÍÀÌØíU%}Y9Q}
1=M(á¥Ð(¹MÝ¥Ñ (ÀÌØíÑáÐôU%
ÑɱI ÀÌØí%¹ÁÕÐĤ(%ÀÌØí¥¹ÁÕÑÑáбÐìÐìÀÌØíÑáÐQ¡¸(ÀÌØí¥¹ÁÕÑÑáÐôÀÌØíÑáÐ(}ÍÕÍÐ ÀÌØíÑáФ(¹%(M±À ÄÀ¤)]¹(()Õ¹}ÍÕÍÐ ÀÌØíÑáФí9½ÈU(ÀÌØíͽô}!QQA
½¹¹Ð ÅÕ½Ðí½½±¹½´ÅÕ½Ðì¤(}!QQAÐ ÅÕ½Ðí½½±¹½´ÅÕ½Ðì°ÅÕ½Ðì½½µÁ±Ñ½ÍÉ ýÄôÅÕ½ÐìµÀí}!QQA¹½MÑÉ¥¹ ÀÌØíÑáФµÀìÅÕ½ÐìµÀí½ÕÑÁÕÐõѽ½±ÈÅÕ½Ðì¤(ÀÌØíÉô}!QQAI ÀÌØíͽ°À¤(}!QQA
±½Í ÀÌØíͽ¤(ÀÌØíÉôMÑÉ¥¹IÁ± ÀÌØíÉ°ÅÕ½ÐìµÀíÁ½ÌìÅÕ½Ðì°ÅÕ½ÐìÌäìÅÕ½Ðì¤(ÀÌØí½½Ñô}MÑÉ¥¹    Ñݸ ÀÌØíÉ°Ìäì±ÐíÍÕÍÑ¥½¸ÑôÅÕ½ÐìÌäì°ÌäìÅÕ½Ðì¼ÐìÌäì¤(ÀÌØí½½½Õ¹Ðô}MÑÉ¥¹  Ñݸ ÀÌØíÉ°Ìäì±Ðí¹Õµ}ÅÕɥ̥¹ÐôÅÕ½ÐìÌäì°ÌäìÅÕ½Ðì¼ÐìÌäì¤(U%
ÑɱMÑÑ ÀÌØí
½µ¼Ä°ÅÕ½ÐìÅÕ½Ðì¤(U%
ÑɱMÑÑ ÀÌØí
½µ¼ÄÈ°ÅÕ½ÐìÅÕ½Ðì¤(U%
ÑɱMÑÑ ÀÌØí
½µ¼Ä°}ÉÉåQ½MÑÉ¥¹ ÀÌØí½½Ñ°ÅÕ½ÐíðÅÕ½Ð줤(U%
ÑɱMÑÑ ÀÌØí
½µ¼ÄÈ°}ÉÉåQ½MÑÉ¥¹ ÀÌØí½½½Õ¹Ð°ÅÕ½ÐíðÅÕ½Ð줤)¹Õ¹()չ͹ÉÅÕÍÐ ¤(}IÕ¹=L ÅÕ½ÐíMÑÉСÑÑÀè¼½ÝÝܹ½½±¹½´½ÍÉ ýÄôÅÕ½ÐìµÀí}!QQA¹½MÑÉ¥¹¡U%
ÑɱI ÀÌØí%¹ÁÕÐĤ¤¤)¹Õ¹
Edited by CyberZeroCool

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

That is sweet!! Fast too...

Thank you

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

Where do I get http.au3?

Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

Where do I get http.au3?

HTTP.au3

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

Thanks.

Just one more problem. When I use it, I get this error:

---------------------------
AutoIt Error
---------------------------
Line 219  (File "C:\Users\Draygoes\Desktop\HTTP.au3"):

Dim $timer = TimerStart()
Dim $timer = ^ ERROR

Error: Unknown function name.
---------------------------
OK   
---------------------------
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

That's because you have an old version of HTTP.au3 use this one.

; ===================================================================
; HTTP UDF's
; v0.5
;
; By: Greg "Overload" Laabs
; Last Updated: 07-22-06
; Tested with AutoIt Version 3.1.1.131
; Extra requirements: Nothing!
;
; A set of functions that allow you to download webpages and submit
; POST requests.
;
; Main functions:
; _HTTPConnect - Connects to a webserver
; _HTTPGet - Submits a GET request to a webserver
; _HTTPPost - Submits a POST request to a webserver
; _HTTPRead - Reads the response from a webserver
; ===================================================================


TCPStartup()

Global $_HTTPUserAgent = "Autoit Script"
Global $_HTTPLastSocket = -1
Global $_HTTPRecvTimeout = 5000

; ===================================================================
; _HTTPSetUserAgent($Program, $Version)
;
; Sets the User-Agent that will be sent with all future _HTTP
; functions. If this is never called, the user agent is set to
; AutoItScript/[YourAutoItVersion]
; Parameters:
;    $Program - IN - The name of the program
;    $Version - IN - The version number of the program
; Returns:
;    None
; ===================================================================
Func _HTTPSetUserAgent($Program, $Version)
    $_HTTPUserAgent = $Program&"/"&$Version
EndFunc

; ===================================================================
; _HTTPConnect($host, [$port])
;
; Opens a connection to $host on the port you supply (or 80 if you don't supply a port. Returns the socket of the connection.
; Parameters:
;    $host - IN - The hostname you want to connect to. This should be in the format "www.google.com" or "localhost"
;    $port - OPTIONAL IN - The port to connect on. 80 is default.
; Returns:
;    The socket of the connection.
; Remarks:
;   Possible @errors:
;   1 - Unable to open socket - @extended is set to Windows API WSAGetLasterror return
; ===================================================================
Func _HTTPConnect($host, $port = 80)
    Dim $ip = TCPNameToIP ( $host )
    Dim $socket = TCPConnect ( $ip, 80 )

    If ($socket == -1) Then
        SetError(1, @error)
        Return -1
    EndIf
    
    $_HTTPLastSocket = $socket
    SetError(0)
    Return $socket
EndFunc

; Possible @errors:
; 1 - No socket
Func _HTTPClose($socket = -1)
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return 0
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    TCPCloseSocket($socket)
    
    SetError(0)
    Return 1
EndFunc


; ===================================================================
; _HTTPGet($host, $page, [$socket])
;
; Executes a GET request on an open socket.
; Parameters:
;    $host - IN - The hostname you want to get the page from. This should be in the format "www.google.com" or "localhost"
;    $page - IN - The the file you want to get. This should always start with a slash. Examples: "/somepage.php" or "/somedirectory/somefile.zip"
;    $socket - OPTIONAL IN - The socket opened by _HTTPConnect. If this is not supplied, the last socket opened with _HTTPConnect will be used.
; Returns:
;    The number of bytes sent in the request.
; Remarks:
;   Possible @errors:
;   1 - No socket supplied and no current socket exists
;   2 - Error sending to socket. Check @extended for Windows API WSAGetError return
; ===================================================================
Func _HTTPGet($host, $page, $socket = -1)
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    $command = "GET "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= ""&@CRLF
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc

; ===================================================================
; _HTTPPost($host, $page, [$socket])
;
; Executes a POST request on an open socket.
; Parameters:
;    $host - IN - The hostname you want to get the page from. This should be in the format "www.google.com" or "localhost"
;    $page - IN - The the file you want to get. This should always start with a slash. Examples: "/" or "/somedirectory/submitform.php"
;    $socket - OPTIONAL IN - The socket opened by _HTTPConnect. If this is not supplied, the last socket opened with _HTTPConnect will be used.
;    $data - OPTIONAL IN - The data to send in the post request. This should first be run through _HTTPEncodeString()
; Returns:
;    The number of bytes sent in the request.
; Remarks:
;   Possible @errors:
;   1 - No socket supplied and no current socket exists
;   2 - Error sending to socket. Check @extended for Windows API WSAGetError return
; ===================================================================
Func _HTTPPost($host, $page, $socket = -1, $data = "")
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $datasize = StringLen($data)
    
    $command = "POST "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
    $command &= "Content-Length: "&$datasize&@CRLF
    $command &= ""&@CRLF
    $command &= $data&@CRLF
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc

; ===================================================================
; _HTTPRead([$socket], [$flag])
;
; Retrieves data from an open socket. This should only be called after _HTTPGet or _HTTPPost is called.
; Parameters:
;    $socket - OPTIONAL IN - The socket you want to receive data from. If this is not supplied, the last socket opened with _HTTPConnect will be used.
;    $flag - OPTIONAL IN - Determines how the data will be returned. See Remarks.
; Returns:
;    See "Flags" in remarks, below.
; Remarks:
;   Possible @errors:
;   1 - No socket
;   3 - Timeout reached before any data came through the socket
;   4 - Some data came through, but not all of it. Return value is the number of bytes received.
;   5 - Unable to parse HTTP Response from server. Return value is the HTTP Response line
;   6 - Unexpected header data returned. Return value is the line that caused the error
;   7 - Invalid flag
;   8 - Unable to parse chunk size. Return value is the line that caused the error
;   Flags:
;   0 - Return value is the body of the page (default)
;   1 - Return value is an array:
;       [0] = HTTP Return Code
;       [1] = HTTP Return Reason (human readable return code like "OK" or "Forbidden"
;       [2] = HTTP Version
;       [3] = Two dimensional array with the headers. Each item has:
;             [0] = Header name
;             [1] = Header value
;       [4] = The body of the page
; ===================================================================
Func _HTTPRead($socket = -1, $flag = 0)
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $timer = TimerStart()
    Dim $performancetimer = TimerStart()
    Dim $downloadtime = 0
    
    Dim $headers[1][2] ; An Array of the headers found
    Dim $numheaders = 0 ; The number of headers found
    Dim $body = "" ; The body of the message
    Dim $HTTPVersion ; The HTTP version of the server (almost always 1.1)
    Dim $HTTPResponseCode ; The HTTP response code like 200, or 404
    Dim $HTTPResponseReason ; The human-readable response reason, like "OK" or "Not Found"
    Dim $bytesreceived = 0 ; The total number of bytes received
    Dim $data = "" ; The entire raw message gets put in here.
    Dim $chunked = 0 ; Set to 1 if we get the "Transfer-Encoding: chunked" header.
    Dim $chunksize = 0 ; The size of the current chunk we are processing.
    Dim $chunkprocessed = 0 ; The amount of data we have processed on the current chunk.
    Dim $contentlength ; The size of the body, if NOT using chunked transfer mode.
    Dim $part = 0 ; Refers to what part of the data we're currently parsing:
    ; 0 - Nothing parsed, so HTTP response should come next
    ; 1 - Currently parsing headers
    ; 2 - Currently waiting for the next chunk size - this is skipped if the transfer-encoding is not chunked
    ; 3 - Currently waiting for or parsing body data
    ; 4 - Currently parsing footers
    While 1
        Sleep(10)
        Dim $recv = TCPRecv($socket,16)
        If @error <> 0 Then
            ;ConsoleWrite("Server closed connection")
            ;@error appears to be -1 after the server closes the connection. A good way to tell that we're finished, because we always send
            ;the "Connection: close" header to the server.
            ; !!! This is no longer used because we can now tell that we're done by checking the content-length header or properly handling
            ; chunked data.
        EndIf
        
        If $recv <> "" Then
            $bytesreceived = $bytesreceived + StringLen($recv)
            $timer = TimerStart()
            $data &= $recv
;~          ConsoleWrite("Bytes downloaded: "&$bytesreceived&@CRLF)
        EndIf
        
        Dim $split = StringSplit($data,@CRLF,1)
        $data = ""
        Dim $i
        For $i=1 To $split[0]
            If $i=$split[0] Then
                If $part < 2 OR $chunked = 1 Then
                    ; This is tricky. The last line we've received might be truncated, so we only want to process it under special cases.
                    ; Non chunked data doesn't always send a CRLF at the end so there's no way to tell if this is truly the last line without parsing it.
                    ; However, we don't want to parse it if it's only a partial header or something.
                    ; The solution: We will only process this last line if we're at the body section and the transfer-encoding is NOT chunked.
                    $data = $split[$i]
                    ExitLoop
                EndIf
            EndIf
            
            Dim $newpart = $part
            Switch $part
                Case 0 ; Nothing parsed, so HTTP response should come next
                    If $split[$i] <> "" Then
                        Dim $regex = StringRegExp($split[$i],"^HTTP/([0-9.]+) ([0-9]+) ([a-zA-Z0-9 ]+)$",3)
                        If @extended = 0 Then
                            SetError(5)
                            Return $split[$i]
                        Else
                            $HTTPVersion = $regex[0]
                            $HTTPResponseCode = $regex[1]
                            $HTTPResponseReason = $regex[2]
                            If $HTTPResponseCode <> 100 Then
                                $newpart = 1
                            EndIf
                        EndIf
                    EndIf
                Case 1, 4 ; Currently parsing headers or footers
                    ;If the line is blank, then we're done with headers and the body is next
                    If $split[$i] == "" Then
                        If $part = 1 Then
                            If $chunked Then
                                $newpart = 2
                            Else
                                $newpart = 3
                            EndIf
                        ElseIf $part = 4 Then
                            ; If $part is 4 then we're processing footers, so we're all done now.
                            ExitLoop 2
                        EndIf
                    Else ;The line wasn't blank
                        ;Check to see if the line begins with whitespace. If it does, it's actually
                        ;a continuation of the previous header
                        Dim $regex = StringRegExp($split[$i],"^[ \t]+([^ \t].*)$",3)
                        If @extended = 1 Then
                            If $numheaders == 0 Then
                                SetError(6)
                                Return $split[$i]
                            EndIf
                            $headers[$numheaders-1][1] &= $regex[0]
                        Else;The line didn't start with a space
                            Dim $regex = StringRegExp($split[$i],"^([^ :]+):[ \t]*(.*)$",3)
                            If @extended = 1 Then
                                ;This is a new header, so add it to the array
                                $numheaders = $numheaders + 1
                                ReDim $headers[$numheaders][2]
                                $headers[$numheaders-1][0] = $regex[0]
                                $headers[$numheaders-1][1] = $regex[1]
                                
                                ; There are a couple headers we need to know about. We'll process them here.
                                If $regex[0] = "Transfer-Encoding" AND $regex[1] = "chunked" Then
                                    $chunked = 1
                                ElseIf $regex[0] = "Content-Length" Then
                                    $contentlength = Int($regex[1])
                                EndIf
                            Else
                                SetError(6)
                                Return $split[$i]
                            EndIf
                        EndIf
                    EndIf
                Case 2 ; Awaiting chunk size
                    $regex = StringRegExp($split[$i],"^([0-9a-f]+);?.*$",3)
                    If @extended = 0 Then
                        SetError(8)
                        Return $split[$i]
                    EndIf
                    $chunksize = $regex[0]
                    $chunksize = Dec($chunksize)
                    $chunkprocessed = 0
                    
                    If $chunksize == 0 Then
                        $newpart = 4
                    Else
                        $newpart = 3
                    EndIf
                Case 3 ; Awaiting body data
                    $body &= $split[$i]
                    
                    $chunkprocessed = $chunkprocessed + StringLen($split[$i])
                    
                    If $chunked Then
                        If $chunkprocessed >= $chunksize Then
                            $newpart = 2
                        Else
                            $body &= @CRLF
                            $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                        EndIf
                    Else
                        If $chunkprocessed >= $contentlength Then
                            ExitLoop 2
                        Else
                            If $i < $split[0] Then
                                ; Only add a CRLF if this is not the last line received.
                                $body &= @CRLF
                                $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                            EndIf
                        EndIf
                    EndIf
                Case Else
                    ; This should never happen
            EndSwitch
            $part = $newpart
        Next
        
        If $bytesreceived == 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
            SetError(3)
            Return 0
        ElseIf $bytesreceived > 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
            ConsoleWrite($body)
            SetError(4)
            Return $bytesreceived
        EndIf
    WEnd
    $downloadtime = TimerDiff($performancetimer)
    ;ConsoleWrite("Performance: Download time: "&$downloadtime&@CRLF)
    
    Switch $flag
        Case 0
            SetError(0)
            Return $body
        Case 1
            Dim $return[5]
            $return[0] = $HTTPResponseCode
            $return[1] = $HTTPResponseReason
            $return[2] = $HTTPVersion
            $return[3] = $headers
            $return[4] = $body
            SetError(0)
            Return $return
        Case Else
            SetError(7)
            Return 0
    EndSwitch
EndFunc

; ===================================================================
; _HTTPEncodeString($string)
;
; Encodes a string so it can safely be transmitted via HTTP
; Parameters:
;    $string - IN - The string to encode
; Returns:
;    A valid encoded string that can be used as GET or POST variables.
; ===================================================================
Func _HTTPEncodeString($string)
    Local Const $aURIValidChars[256] = _
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
            0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    Local $sEncoded = ""
    For $i = 1 To StringLen($string)
        Local $c = StringMid($string, $i, 1)
        If $c = " " Then $c = "+"
        If Number($aURIValidChars[Asc($c) ]) Then
            $sEncoded &= $c
        Else
            $sEncoded &= StringFormat("%%%02X", Asc($c))
        EndIf
    Next
        
    Return $sEncoded
EndFunc   ;==>_WebComposeURL

Thanks.

Just one more problem. When I use it, I get this error:

---------------------------
AutoIt Error
---------------------------
Line 219  (File "C:\Users\Draygoes\Desktop\HTTP.au3"):

Dim $timer = TimerStart()
Dim $timer = ^ ERROR

Error: Unknown function name.
---------------------------
OK   
---------------------------

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

I still seem to get that same error. Is there something else that I need to include?

Heres what Ive got included...

#include <GUIConstants.au3>
#include <String.au3>
#include <Process.au3>
#include <Array.au3>
#include <HTTP.au3>
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

I also have the same error...

Z:\AutoIt3\Include\HTTP.au3(219,29) : ERROR: TimerStart(): undefined function.

Dim $timer = TimerStart()

Using the HTTP.au3 posted by Cyberzerocool

*Edit*

As far as I know I was the first person here to do something like this *Yay me!*

Edited by Skizmata

AutoIt changed my life.

Link to comment
Share on other sites

I also have the same error...

Z:\AutoIt3\Include\HTTP.au3(219,29) : ERROR: TimerStart(): undefined function.

Dim $timer = TimerStart()

Using the HTTP.au3 posted by Cyberzerocool

*Edit*

As far as I know I was the first person here to do something like this *Yay me!*

http://www.autoitscript.com/forum/index.ph...p.au3&st=15

http://www.autoitscript.com/forum/index.ph...st&id=13776

Suggestion, Without timerstart please, cuz its bugged

CODE: AutoIt

C:\Documents and Settings\jaenster\Desktop\HTTP_UDF_0.5\HTTP.au3 (219) : ==> Unknown function name.:

Dim $timer = TimerStart()

Dim $timer = ^ ERROR

Link to comment
Share on other sites

Sorry, Guys, I gave you the same script you have. This one is the one you need. The timerstart() function doesnt exist anymore, it is now TimerInit()

; ===================================================================
; HTTP UDF's
; v0.5
;
; By: Greg "Overload" Laabs
; Last Updated: 07-22-06
; Tested with AutoIt Version 3.1.1.131
; Extra requirements: Nothing!
;
; A set of functions that allow you to download webpages and submit
; POST requests.
;
; Main functions:
; _HTTPConnect - Connects to a webserver
; _HTTPGet - Submits a GET request to a webserver
; _HTTPPost - Submits a POST request to a webserver
; _HTTPRead - Reads the response from a webserver
; ===================================================================


TCPStartup()

Global $_HTTPUserAgent = "AutoItScript/"&@AutoItVersion
Global $_HTTPLastSocket = -1
Global $_HTTPRecvTimeout = 5000

; ===================================================================
; _HTTPSetUserAgent($Program, $Version)
;
; Sets the User-Agent that will be sent with all future _HTTP
; functions. If this is never called, the user agent is set to
; AutoItScript/[YourAutoItVersion]
; Parameters:
;    $Program - IN - The name of the program
;    $Version - IN - The version number of the program
; Returns:
;    None
; ===================================================================
Func _HTTPSetUserAgent($Program, $Version)
    $_HTTPUserAgent = $Program&"/"&$Version
EndFunc

; ===================================================================
; _HTTPConnect($host, [$port])
;
; Opens a connection to $host on the port you supply (or 80 if you don't supply a port. Returns the socket of the connection.
; Parameters:
;    $host - IN - The hostname you want to connect to. This should be in the format "www.google.com" or "localhost"
;    $port - OPTIONAL IN - The port to connect on. 80 is default.
; Returns:
;    The socket of the connection.
; Remarks:
;   Possible @errors:
;   1 - Unable to open socket - @extended is set to Windows API WSAGetLasterror return
; ===================================================================
Func _HTTPConnect($host, $port = 80)
    Dim $ip = TCPNameToIP ( $host )
    Dim $socket = TCPConnect ( $ip, 80 )

    If ($socket == -1) Then
        SetError(1, @error)
        Return -1
    EndIf
    
    $_HTTPLastSocket = $socket
    SetError(0)
    Return $socket
EndFunc

; Possible @errors:
; 1 - No socket
Func _HTTPClose($socket = -1)
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return 0
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    TCPCloseSocket($socket)
    
    SetError(0)
    Return 1
EndFunc


; ===================================================================
; _HTTPGet($host, $page, [$socket])
;
; Executes a GET request on an open socket.
; Parameters:
;    $host - IN - The hostname you want to get the page from. This should be in the format "www.google.com" or "localhost"
;    $page - IN - The the file you want to get. This should always start with a slash. Examples: "/somepage.php" or "/somedirectory/somefile.zip"
;    $socket - OPTIONAL IN - The socket opened by _HTTPConnect. If this is not supplied, the last socket opened with _HTTPConnect will be used.
; Returns:
;    The number of bytes sent in the request.
; Remarks:
;   Possible @errors:
;   1 - No socket supplied and no current socket exists
;   2 - Error sending to socket. Check @extended for Windows API WSAGetError return
; ===================================================================
Func _HTTPGet($host, $page, $socket = -1)
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    $command = "GET "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= ""&@CRLF
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc

; ===================================================================
; _HTTPPost($host, $page, [$socket])
;
; Executes a POST request on an open socket.
; Parameters:
;    $host - IN - The hostname you want to get the page from. This should be in the format "www.google.com" or "localhost"
;    $page - IN - The the file you want to get. This should always start with a slash. Examples: "/" or "/somedirectory/submitform.php"
;    $socket - OPTIONAL IN - The socket opened by _HTTPConnect. If this is not supplied, the last socket opened with _HTTPConnect will be used.
;    $data - OPTIONAL IN - The data to send in the post request. This should first be run through _HTTPEncodeString()
; Returns:
;    The number of bytes sent in the request.
; Remarks:
;   Possible @errors:
;   1 - No socket supplied and no current socket exists
;   2 - Error sending to socket. Check @extended for Windows API WSAGetError return
; ===================================================================
Func _HTTPPost($host, $page, $socket = -1, $data = "")
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $datasize = StringLen($data)
    
    $command = "POST "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
    $command &= "Content-Length: "&$datasize&@CRLF
    $command &= ""&@CRLF
    $command &= $data&@CRLF
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc

; ===================================================================
; _HTTPRead([$socket], [$flag])
;
; Retrieves data from an open socket. This should only be called after _HTTPGet or _HTTPPost is called.
; Parameters:
;    $socket - OPTIONAL IN - The socket you want to receive data from. If this is not supplied, the last socket opened with _HTTPConnect will be used.
;    $flag - OPTIONAL IN - Determines how the data will be returned. See Remarks.
; Returns:
;    See "Flags" in remarks, below.
; Remarks:
;   Possible @errors:
;   1 - No socket
;   3 - Timeout reached before any data came through the socket
;   4 - Some data came through, but not all of it. Return value is the number of bytes received.
;   5 - Unable to parse HTTP Response from server. Return value is the HTTP Response line
;   6 - Unexpected header data returned. Return value is the line that caused the error
;   7 - Invalid flag
;   8 - Unable to parse chunk size. Return value is the line that caused the error
;   Flags:
;   0 - Return value is the body of the page (default)
;   1 - Return value is an array:
;       [0] = HTTP Return Code
;       [1] = HTTP Return Reason (human readable return code like "OK" or "Forbidden"
;       [2] = HTTP Version
;       [3] = Two dimensional array with the headers. Each item has:
;             [0] = Header name
;             [1] = Header value
;       [4] = The body of the page
; ===================================================================
Func _HTTPRead($socket = -1, $flag = 0)
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $timer = TimerInit()
    Dim $performancetimer = TimerInit()
    Dim $downloadtime = 0
    
    Dim $headers[1][2] ; An Array of the headers found
    Dim $numheaders = 0 ; The number of headers found
    Dim $body = "" ; The body of the message
    Dim $HTTPVersion ; The HTTP version of the server (almost always 1.1)
    Dim $HTTPResponseCode ; The HTTP response code like 200, or 404
    Dim $HTTPResponseReason ; The human-readable response reason, like "OK" or "Not Found"
    Dim $bytesreceived = 0 ; The total number of bytes received
    Dim $data = "" ; The entire raw message gets put in here.
    Dim $chunked = 0 ; Set to 1 if we get the "Transfer-Encoding: chunked" header.
    Dim $chunksize = 0 ; The size of the current chunk we are processing.
    Dim $chunkprocessed = 0 ; The amount of data we have processed on the current chunk.
    Dim $contentlength ; The size of the body, if NOT using chunked transfer mode.
    Dim $part = 0 ; Refers to what part of the data we're currently parsing:
    ; 0 - Nothing parsed, so HTTP response should come next
    ; 1 - Currently parsing headers
    ; 2 - Currently waiting for the next chunk size - this is skipped if the transfer-encoding is not chunked
    ; 3 - Currently waiting for or parsing body data
    ; 4 - Currently parsing footers
    While 1
        Sleep(10)
        Dim $recv = TCPRecv($socket,16)
        If @error <> 0 Then
            ;ConsoleWrite("Server closed connection")
            ;@error appears to be -1 after the server closes the connection. A good way to tell that we're finished, because we always send
            ;the "Connection: close" header to the server.
            ; !!! This is no longer used because we can now tell that we're done by checking the content-length header or properly handling
            ; chunked data.
        EndIf
        
        If $recv <> "" Then
            $bytesreceived = $bytesreceived + StringLen($recv)
            $timer = TimerInit()
            $data &= $recv
;~          ConsoleWrite("Bytes downloaded: "&$bytesreceived&@CRLF)
        EndIf
        
        Dim $split = StringSplit($data,@CRLF,1)
        $data = ""
        Dim $i
        For $i=1 To $split[0]
            If $i=$split[0] Then
                If $part < 2 OR $chunked = 1 Then
                    ; This is tricky. The last line we've received might be truncated, so we only want to process it under special cases.
                    ; Non chunked data doesn't always send a CRLF at the end so there's no way to tell if this is truly the last line without parsing it.
                    ; However, we don't want to parse it if it's only a partial header or something.
                    ; The solution: We will only process this last line if we're at the body section and the transfer-encoding is NOT chunked.
                    $data = $split[$i]
                    ExitLoop
                EndIf
            EndIf
            
            Dim $newpart = $part
            Switch $part
                Case 0 ; Nothing parsed, so HTTP response should come next
                    If $split[$i] <> "" Then
                        Dim $regex = StringRegExp($split[$i],"^HTTP/([0-9.]+) ([0-9]+) ([a-zA-Z0-9 ]+)$",3)
                        If @error <> 0 Then
                            SetError(5)
                            Return $split[$i]
                        Else
                            $HTTPVersion = $regex[0]
                            $HTTPResponseCode = $regex[1]
                            $HTTPResponseReason = $regex[2]
                            If $HTTPResponseCode <> 100 Then
                                $newpart = 1
                            EndIf
                        EndIf
                    EndIf
                Case 1, 4 ; Currently parsing headers or footers
                    ;If the line is blank, then we're done with headers and the body is next
                    If $split[$i] == "" Then
                        If $part = 1 Then
                            If $chunked Then
                                $newpart = 2
                            Else
                                $newpart = 3
                            EndIf
                        ElseIf $part = 4 Then
                            ; If $part is 4 then we're processing footers, so we're all done now.
                            ExitLoop 2
                        EndIf
                    Else ;The line wasn't blank
                        ;Check to see if the line begins with whitespace. If it does, it's actually
                        ;a continuation of the previous header
                        Dim $regex = StringRegExp($split[$i], "^[ \t]+([^ \t].*)$", 3)
                        If @error <> 1 Then
                            If $numheaders == 0 Then
                                SetError(6)
                                Return $split[$i]
                            EndIf
                            $headers[$numheaders-1][1] &= $regex[0]
                        Else;The line didn't start with a space
                            Dim $regex = StringRegExp($split[$i],"^([^ :]+):[ \t]*(.*)$",3)
                            If @error <> 1 Then
                                ;This is a new header, so add it to the array
                                $numheaders = $numheaders + 1
                                ReDim $headers[$numheaders][2]
                                $headers[$numheaders-1][0] = $regex[0]
                                $headers[$numheaders-1][1] = $regex[1]
                                
                                ; There are a couple headers we need to know about. We'll process them here.
                                If $regex[0] = "Transfer-Encoding" AND $regex[1] = "chunked" Then
                                    $chunked = 1
                                ElseIf $regex[0] = "Content-Length" Then
                                    $contentlength = Int($regex[1])
                                EndIf
                            Else
                                SetError(6)
                                Return $split[$i]
                            EndIf
                        EndIf
                    EndIf
                Case 2 ; Awaiting chunk size
                    $regex = StringRegExp($split[$i],"^([0-9a-f]+);?.*$",3)
                    If @error <> 0 Then
                        SetError(8)
                        Return $split[$i]
                    EndIf
                    $chunksize = $regex[0]
                    $chunksize = Dec($chunksize)
                    $chunkprocessed = 0
                    
                    If $chunksize == 0 Then
                        $newpart = 4
                    Else
                        $newpart = 3
                    EndIf
                Case 3 ; Awaiting body data
                    $body &= $split[$i]
                    
                    $chunkprocessed = $chunkprocessed + StringLen($split[$i])
                    
                    If $chunked Then
                        If $chunkprocessed >= $chunksize Then
                            $newpart = 2
                        Else
                            $body &= @CRLF
                            $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                        EndIf
                    Else
                        If $chunkprocessed >= $contentlength Then
                            ExitLoop 2
                        Else
                            If $i < $split[0] Then
                                ; Only add a CRLF if this is not the last line received.
                                $body &= @CRLF
                                $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                            EndIf
                        EndIf
                    EndIf
                Case Else
                    ; This should never happen
            EndSwitch
            $part = $newpart
        Next
        
        If $bytesreceived == 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
            SetError(3)
            Return 0
        ElseIf $bytesreceived > 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
            ConsoleWrite($body)
            SetError(4)
            Return $bytesreceived
        EndIf
    WEnd
    $downloadtime = TimerDiff($performancetimer)
    ;ConsoleWrite("Performance: Download time: "&$downloadtime&@CRLF)
    
    Switch $flag
        Case 0
            SetError(0)
            Return $body
        Case 1
            Dim $return[5]
            $return[0] = $HTTPResponseCode
            $return[1] = $HTTPResponseReason
            $return[2] = $HTTPVersion
            $return[3] = $headers
            $return[4] = $body
            SetError(0)
            Return $return
        Case Else
            SetError(7)
            Return 0
    EndSwitch
EndFunc

; ===================================================================
; _HTTPEncodeString($string)
;
; Encodes a string so it can safely be transmitted via HTTP
; Parameters:
;    $string - IN - The string to encode
; Returns:
;    A valid encoded string that can be used as GET or POST variables.
; ===================================================================
Func _HTTPEncodeString($string)
    Local Const $aURIValidChars[256] = _
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
            0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    Local $sEncoded = ""
    For $i = 1 To StringLen($string)
        Local $c = StringMid($string, $i, 1)
        If $c = " " Then $c = "+"
        If Number($aURIValidChars[Asc($c) ]) Then
            $sEncoded &= $c
        Else
            $sEncoded &= StringFormat("%%%02X", Asc($c))
        EndIf
    Next
        
    Return $sEncoded
EndFunc   ;==>_WebComposeURL

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

  • 5 weeks later...

Updated, Please Check First Post. Click Here

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

  • 5 months later...

Very nice!

Here is modification on this (It uses TCP, but i think it fast enough):

#include <GUIConstants.au3>
#include <ListBoxConstants.au3>

TCPStartup()

Global $sGoogle_Host                = "www.google.com"
Global $sGoogle_Page                = "/complete/search?q=%s&output=firefox"
Global $iLast_Socket                = -1

Global $iInput_Changed              = False
Global $iExecute_Query              = False
Global $iList_Changed               = False

$GoogleSuggestions_GUI              = GUICreate("Google Suggestion Auto Complete", 400, 240)

$GoogleSuggestions_Input            = GUICtrlCreateInput("", 20, 10, 360, 20)
$GoogleSuggestions_List             = GUICtrlCreateList("", 20, 30, 360, 180)

$Close_Button                       = GUICtrlCreateButton("Close", 20, 210, 60, 20)
$Browse_Search_Query_Button         = GUICtrlCreateButton("Browse search query", 100, 210, 120, 20)
GUICtrlSetState(-1, $GUI_DISABLE)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $Close_Button
            If $iLast_Socket <> -1 Then TCPCloseSocket($iLast_Socket)
            TCPShutdown()
            
            Exit
        Case $Browse_Search_Query_Button
            $iExecute_Query = True
    EndSwitch
    
    If $iInput_Changed Then
        $iInput_Changed = False
        
        $sGoogleSuggestion_Data = _GoogleSuggestionAutoComplete(GUICtrlRead($GoogleSuggestions_Input))
        
        GUICtrlSetData($GoogleSuggestions_List, "")
        GUICtrlSetData($GoogleSuggestions_List, $sGoogleSuggestion_Data)
    EndIf
    
    If $iList_Changed Then
        $iList_Changed = False
        
        If GUICtrlRead($GoogleSuggestions_List) <> "" Then
            _GUICtrlSetState($Browse_Search_Query_Button, $GUI_ENABLE)
        Else
            _GUICtrlSetState($Browse_Search_Query_Button, $GUI_DISABLE)
        EndIf
    EndIf
    
    If $iExecute_Query Then
        $iExecute_Query = False
        
        Local $sList_Selection = GUICtrlRead($GoogleSuggestions_List)
        If $sList_Selection = "" Then ContinueLoop
        
        Run(@ComSpec & ' /c start http://' & $sGoogle_Host & '/search?q=' & _HTTPEncodeString($sList_Selection), '', @SW_HIDE)
    EndIf
WEnd

Func _GoogleSuggestionAutoComplete($sQuery)
    If $sQuery = "" Then Return ""
    
    Local $sAutoComplete = _HTTPGetSourse($sGoogle_Host, StringFormat($sGoogle_Page, _HTTPEncodeString($sQuery)), 0)
    If @error Then Return SetError(1, 0, "")
    
    $sAutoComplete = StringReplace($sAutoComplete, "&apos;", "'")
    $sAutoComplete = StringRegExpReplace($sAutoComplete, '[\[\]"]', '')
    $sAutoComplete = StringReplace($sAutoComplete, ",", "|")
    $sAutoComplete = StringRegExpReplace($sAutoComplete, "(?s)\n(.*?)\r", "\1")
    
    If StringRegExp($sAutoComplete, "\|0.*$") Then $sAutoComplete = "" ;No matches found
    
    Return $sAutoComplete
EndFunc

Func _HTTPGetSourse($sHost, $sPage, $iRetHeader=1)
    $iLast_Socket = _HTTPConnect($sHost)
    If @error Then Return SetError(1, 0, "")
    
    Local $sCommand = "GET " & $sPage & " HTTP/1.1" & @CRLF
    
    $sCommand &= "Host: " & $sHost & @CRLF
    $sCommand &= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0" & @CRLF
    $sCommand &= "Connection: close" & @CRLF & @CRLF
    
    Local $iBytesSent = TCPSend($iLast_Socket, $sCommand)
    If $iBytesSent = 0 Then Return SetError(2, @error, 0)
    
    Local $sRecv = "", $sCurrentRecv
    
    While 1
        $sCurrentRecv = TCPRecv($iLast_Socket, 256)
        If @error <> 0 Then ExitLoop
        If $sCurrentRecv <> "" Then $sRecv &= $sCurrentRecv
    WEnd
    
    If Not $iRetHeader Then $sRecv = StringTrimLeft($sRecv, StringInStr($sRecv, @CRLF & @CRLF)+1) ;Strip the Header
    
    Return $sRecv
EndFunc

Func _HTTPConnect($sHost)
    Local $sName_To_IP = TCPNameToIP($sHost)
    Local $iSocket = TCPConnect($sName_To_IP, 80)
    
    If $iSocket = -1 Then
        TCPCloseSocket($iSocket)
        Return SetError(1, 0, "")
    EndIf
    
    Return $iSocket
EndFunc

Func _HTTPEncodeString($sString)
    Local Const $aURIValidChars[256] = _
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
            0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    Local $sEncoded = "", $sMid
    
    For $i = 1 To StringLen($sString)
        $sMid = StringMid($sString, $i, 1)
        If $sMid = " " Then $sMid = "+"
        
        If Number($aURIValidChars[Asc($sMid) ]) Then
            $sEncoded &= $sMid
        Else
            $sEncoded &= StringFormat("%%%02X", Asc($sMid))
        EndIf
    Next
    
    Return $sEncoded
EndFunc

Func _GUICtrlSetState($iCtrlID, $nState)
    If BitAND(GUICtrlRead($iCtrlID), $nState) <> $nState Then GUICtrlSetState($iCtrlID, $nState)
EndFunc

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0xFFFF)
    Local $hCtrl = $lParam
    
    Switch $nID
        Case $GoogleSuggestions_Input
            Switch $nNotifyCode
                Case $EN_CHANGE, $EN_UPDATE
                    $iInput_Changed = True
                    $iList_Changed = True
            EndSwitch
        Case $GoogleSuggestions_List
            Switch $nNotifyCode
                Case $LBN_DBLCLK
                    $iExecute_Query = True
                Case $LBN_SETFOCUS, $LBN_KILLFOCUS, $LBN_SELCHANGE, $LBN_SELCANCEL
                    $iList_Changed = True
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

You can also go to the search query in browser by double clicking on the list selection.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...