Jump to content

Google Functions


Beege
 Share

Recommended Posts

Heres four short and quick functions that use google that might come in handy for some people. Ideas came from Travis and IchBistTod scripts. Can anyone think of any more?

#include <inet.au3>
#include <array.au3>

$suggestions = _GoogleSuggestions('autoit s')
_ArrayDisplay($suggestions, 'Suggestions')

$Definitions = _GoogleDefine("Pulp")
_ArrayDisplay($Definitions, 'Pulp ')

$Translate = _GoogleTranslate("Hello how are you?")
MsgBox(0, 'English to Spanish  ', "Hello how are you?" & @CRLF & $Translate)

$convert = _GoogleUnitConvert(10, 'gallons', 'liters')
MsgBox(0,'Gallons to Liters', '10 Gallons = ' &$convert& ' Liters')


Func _GoogleSuggestions($sSuggest); Retruns an Array of Suggestions
    Local $sSource, $aResults;, $aQueries
    $sSource = _INetGetSource("http://google.com/complete/search?output=toolbar&q=" & $sSuggest)
    If @error Then Return SetError(1)
    $aResults = StringRegExp($sSource, '<CompleteSuggestion><suggestion data="(.*?)"/>', 3)
    ;~ $sQueries = StringRegExp($source, '"/><num_queries int="(\d{0,})"/>', 3)
    Return $aResults
EndFunc   ;==>_GoogleSuggestions

Func _GoogleDefine($sKeyWord)
    Local $aDefintions, $sSource
    $sSource = _INetGetSource("http://www.google.com/search?q=define%3A" & $sKeyWord)
    If @error Then Return SetError(1)
    $aDefintions = StringRegExp($sSource, "<li>(.*?)<br>", 1)
    Return StringSplit(StringRegExpReplace($aDefintions[0], '(<li>)', '|'), '|')
EndFunc   ;==>_GoogleDefine

Func _GoogleTranslate($sText, $sTo = "es", $sFrom = "en")
    Local $sTranslation, $sUrl, $sSource
    $sUrl = StringFormat("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s", $sText, $sFrom, $sTo)
    $sSource = _INetGetSource($sUrl)
    $sTranslation = StringRegExp(BinaryToString($sSource, 4), '"translatedText":"([^"]+)"', 1)
    Return $sTranslation[0]
EndFunc   ;==>_GoogleTranslate
;~ spanish = es, Albanian = sq, Arabic = ar, Bulgarian = bg,Catalan = ca, Croatian = hr, Czech = cs,Danish = da
;~ dutch = nl,Estonian = et,Filipino = tl, Finnish = fi, French = fr, Galician = gl,German = de,Greek = el
;~ Hebrew = iw,Hindi = hi - no, Hungarian = hu,Indonesian = id, Italian = it, Latvian = lv,Vietnamese = vi
;~ Turkish = tr,Swedish = sv,Russian = ru, Portuguese = pt, English = en

Func _GoogleUnitConvert($sValue, $sFrom, $sTo)
    Local $sSource
    $sSource = _INetGetSource("http://www.google.com/search?q=" & $sValue & "%20" & StringLower($sFrom) & "%20in%20" & StringLower($sTo))
    If @error Then Return SetError(1)
    $sSource = StringRegExp($sSource, '<h2(.*?)More about calculator.</a>', 1)
    $sSource = StringRegExp($sSource[0], '=\s(.*?)\s[a-z]*(\s?[a-z]*?)</b>', 1)
    Return StringReplace($sSource[0], Chr(160), '')
EndFunc   ;==>_GoogleUnitConvert
Edited by bchris01
Link to comment
Share on other sites

Haha, nice, there's a few others that I had fun with too, but yours is by far quite eloquent.

I decided to have some fun.

Very simple interface, but i'm going to impliment it inside of one of my apps for quick usage :)

Does: Autosuggest based on what you type (minimum 3 letters), auto define, auto translate. All based on what you type, as you type it.

Screenshot:

Posted Image

#include <inet.au3>
#include <array.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
$test = GUICreate("test", 800, 800, -1, -1)
$input = GUICtrlCreateInput("", 5, 10, 550, 30)
$list = GUICtrlCreateListView("Suggestion", 5, 40, 260, 700)
_GUICtrlListView_SetColumnWidth($list, 0, 245)

$defines = GUICtrlCreateEdit("", 265, 192, 530, 550, $ES_MULTILINE, 0)
$edit = GUICtrlCreateEdit("", 265, 40, 295, 150, $ES_MULTILINE, 0)
$from = GUICtrlCreateCombo("en - English", 560, 13, 90, 20)
GUICtrlSetData(-1, "es - Spanish|sq - Albanian|ar - Arabic|bg - Bulgarian|ca - Catalan|hr - Croatian|cs - Czeh|da - Danish|nl - Dutch|et - Estonian|tl - Filipino|fi - Finish|fr - French|gl - Galacian|de - German|el - Greek|iw - Hebrew|hi - Hindi|hu - Hungarian|id - Indonesian|it - Italian|lv - Latvian|vi - Vietnamese|tr - Turkish|sv - Sweedish|ru - Russian|pt - Portuguese")
$to = GUICtrlCreateCombo("es - Spanish", 650, 13, 90, 20)
GUICtrlSetData(-1, "en - English|sq - Albanian|ar - Arabic|bg - Bulgarian|ca - Catalan|hr - Croatian|cs - Czeh|da - Danish|nl - Dutch|et - Estonian|tl - Filipino|fi - Finish|fr - French|gl - Galacian|de - German|el - Greek|iw - Hebrew|hi - Hindi|hu - Hungarian|id - Indonesian|it - Italian|lv - Latvian|vi - Vietnamese|tr - Turkish|sv - Sweedish|ru - Russian|pt - Portuguese")
GUICtrlCreateLabel("", 563, 40, 235, 150)
GUICtrlSetData(-1, "spanish = es, Albanian = sq, Arabic = ar, Bulgarian = bg,Catalan = ca, Croatian = hr, Czech = cs,Danish = da" & _
        "dutch = nl,Estonian = et,Filipino = tl, Finnish = fi, French = fr, Galician = gl,German = de,Greek = el" & _
        "Hebrew = iw,Hindi = hi - no, Hungarian = hu,Indonesian = id, Italian = it, Latvian = lv,Vietnamese = vi" & _
        "Turkish = tr,Swedish = sv,Russian = ru, Portuguese = pt, English = en")
GUISetState()
$olddata = ""

ConsoleWrite(_GUICtrlListView_GetSelectedCount($list) > 0 & @CRLF)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case - 3
            Exit
        Case $from, $to
            $Translate = _GoogleTranslate(GUICtrlRead($input), StringLeft(GUICtrlRead($to), 2), StringLeft(GUICtrlRead($from), 2))
            GUICtrlSetData($edit, $Translate)
    EndSwitch
    If _GUICtrlListView_GetSelectedCount($list) <> False Then
        $found = _GUICtrlListView_GetItemTextString($list, -1)
        GUICtrlSetData($input, $found)
    EndIf
    If GUICtrlRead($input) <> "" Then
        $data = GUICtrlRead($input)
        If $olddata <> $data Then
            If StringLen($data) < 3 Then
            Else
                $suggestions = _GoogleSuggestions($data)
                _GUICtrlListView_DeleteAllItems($list)
                GUICtrlSetData($defines, "")
                GUICtrlSetData($edit, "")

                For $i = 1 To UBound($suggestions) - 1
                    GUICtrlCreateListViewItem($suggestions[$i], $list)
                    ConsoleWrite($suggestions[$i] & @CRLF)
                Next
                $Definitions = _GoogleDefine($data)
                For $i = 0 To UBound($Definitions) - 1
                    GUICtrlSetData($defines, $i & ": " & $Definitions[$i] & @CRLF, $defines)
                Next

                $Translate = _GoogleTranslate($data, StringLeft(GUICtrlRead($to), 2), StringLeft(GUICtrlRead($from), 2))
                GUICtrlSetData($edit, $Translate)


            EndIf
        EndIf
        $olddata = $data
    EndIf

WEnd


$suggestions = _GoogleSuggestions('autoit s')
_ArrayDisplay($suggestions, 'Suggestions')

$Definitions = _GoogleDefine("Pulp")
_ArrayDisplay($Definitions, 'Pulp ')

$Translate = _GoogleTranslate("Hello how are you?")
MsgBox(0, 'English to Spanish  ', "Hello how are you?" & @CRLF & $Translate)


Func _GoogleSuggestions($sSuggest); Retruns an Array of Suggestions
    Local $sSource, $aResults;, $aQueries
    $sSource = _INetGetSource("http://google.com/complete/search?output=toolbar&q=" & $sSuggest)
    If @error Then Return SetError(1)
    $aResults = StringRegExp($sSource, '<CompleteSuggestion><suggestion data="(.*?)"/>', 3)
;~ $sQueries = StringRegExp($source, '"/><num_queries int="(\d{0,})"/>', 3)
    Return $aResults
EndFunc   ;==>_GoogleSuggestions

Func _GoogleDefine($sKeyWord)
    Local $aDefintions, $sSource
    $sSource = _INetGetSource("http://www.google.com/search?q=define%3A" & $sKeyWord)
    If @error Then Return SetError(1)
    $aDefintions = StringRegExp($sSource, "<li>(.*?)<br>", 1)
    If IsArray($aDefintions) Then
        Return StringSplit(StringRegExpReplace($aDefintions[0], '(<li>)', '|'), '|')
    Else
        Return ""
    EndIf
EndFunc   ;==>_GoogleDefine

Func _GoogleTranslate($sText, $sTo = "es", $sFrom = "en")
    Local $sTranslation, $sUrl, $sSource
    $sUrl = StringFormat("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s", $sText, $sFrom, $sTo)
    $sSource = _INetGetSource($sUrl)
    $sTranslation = StringRegExp(BinaryToString($sSource, 4), '"translatedText":"([^"]+)"', 1)
    If IsArray($sTranslation) Then
        Return $sTranslation[0]
    Else
        Return ""
    EndIf
EndFunc   ;==>_GoogleTranslate

;~ spanish = es, Albanian = sq, Arabic = ar, Bulgarian = bg,Catalan = ca, Croatian = hr, Czech = cs,Danish = da
;~ dutch = nl,Estonian = et,Filipino = tl, Finnish = fi, French = fr, Galician = gl,German = de,Greek = el
;~ Hebrew = iw,Hindi = hi - no, Hungarian = hu,Indonesian = id, Italian = it, Latvian = lv,Vietnamese = vi
;~ Turkish = tr,Swedish = sv,Russian = ru, Portuguese = pt, English = en
[c Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Link to comment
Share on other sites

Link to comment
Share on other sites

I agree. 5 stars from me too. I really like the translator. I have seen many people try to use sites like babelfish, but I haven't seen many people try google. These are very good results from a different source. I like the suggestions and the translator very much.

Link to comment
Share on other sites

Thanks for the comments guys. In the future I would like to add one more function. _GoogleUnitConvert(). Im always using googles unit converter..

Link to comment
Share on other sites

Heres the _GoogleUnitConvert() I said I would do. If your wondering about why I used stringlower() on $sTo and $sFrom, its because Google is picky! Apparently no capital letters are allowed. "10 miles in feet" works fine, but "10 Miles in feet" will not work. Ya.. kinda dumb.

#include <inet.au3>
#include <array.au3>

$convert = _GoogleUnitConvert(.03,'kilowatt hours', 'joules')
MsgBox(0,'Kilowatt Hours to Joules', '.03 Kilowatt Hours = ' & $convert & ' Joules')
MsgBox(0,'Gallons to Liters', '10 Gallons = ' &_GoogleUnitConvert(10, 'gallons', 'liters')& ' Liters')

Func _GoogleUnitConvert($sValue, $sFrom, $sTo)
    Local $sSource
    $sSource = _INetGetSource("http://www.google.com/search?q=" & $sValue & "%20" & StringLower($sFrom) & "%20in%20" & StringLower($sTo))
    If @error Then Return SetError(1)
    $sSource = StringRegExp($sSource, '<h2(.*?)More about calculator.</a>', 1)
    $sSource = StringRegExp($sSource[0], '=\s(.*?)\s[a-z]*(\s?[a-z]*?)</b>', 1)
    Return StringReplace($sSource[0], Chr(160), '')
EndFunc   ;==>_GoogleUnitConvert
Edited by bchris01
Link to comment
Share on other sites

  • 5 months later...
  • 4 months later...

Heres the _GoogleUnitConvert() I said I would do. If your wondering about why I used stringlower() on $sTo and $sFrom, its because Google is picky! Apparently no capital letters are allowed. "10 miles in feet" works fine, but "10 Miles in feet" will not work. Ya.. kinda dumb.

#include <inet.au3>
#include <array.au3>

$convert = _GoogleUnitConvert(.03,'kilowatt hours', 'joules')
MsgBox(0,'Kilowatt Hours to Joules', '.03 Kilowatt Hours = ' & $convert & ' Joules')
MsgBox(0,'Gallons to Liters', '10 Gallons = ' &_GoogleUnitConvert(10, 'gallons', 'liters')& ' Liters')

Func _GoogleUnitConvert($sValue, $sFrom, $sTo)
    Local $sSource
    $sSource = _INetGetSource("http://www.google.com/search?q=" & $sValue & "%20" & StringLower($sFrom) & "%20in%20" & StringLower($sTo))
    If @error Then Return SetError(1)
    $sSource = StringRegExp($sSource, '<h2(.*?)More about calculator.</a>', 1)
    $sSource = StringRegExp($sSource[0], '=\s(.*?)\s[a-z]*(\s?[a-z]*?)</b>', 1)
    Return StringReplace($sSource[0], Chr(160), '')
EndFunc   ;==>_GoogleUnitConvert

Is it possible to use the _GoogleUnitConvert to convert the money .

I have tried this line that give me an error:

MsgBox(0,'Gallons to Liters', '1 dollar = ' &_GoogleUnitConvert(1, 'dollar', 'euro')& ' euro')

I have tried also that but i have the same issue:

MsgBox(0,'Gallons to Liters', '1 dollar = ' &_GoogleUnitConvert(1, 'usd', 'eur')& ' euro')

Thanks for all help

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...