This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

google autocomplete

Support & discussion of released scripts, and announcements of new releases.
Post Reply
x
x-y-no
Voice
Posts: 7
Joined: Sun Aug 09, 2009 12:13 pm
Location: Fort Lauderdale, FL

google autocomplete

Post by x-y-no »

Somebody in the #eggdrop irc channel last night asked about a google autocomplete script and it seemed to me it would be a fun thing to try and write, so I whipped this out this evening ...

It seems to work OK, but I suck at tcl so I'm sure there's a lot of things that could be improved. For one thing, I tried to write the parsing with regex but kept screwing it up so I devolved to string.

Also, it would be neat to add country codes, i.e. if the user puts ".fr" in the search, change the url to contain "hl=fr" in place of "hl=en". That one I'm sure I can do - just didn't bother yet.

Anyway, I'd love some constructive criticism of my code as well as any suggestions for other features.

(EDIT to add: Obviously, I ripped the HTTP stuff out of scripts by incith and/or speechles - all credit for what I got right belongs there)

Enjoy!

Code: Select all

# autocomplete v.0.1

# usage: !autocomplete <phrase>

set AutoCompleteVer 0.1
package require http
setudef flag autocomplete

namespace eval autocomplete {
    bind pub - !autocomplete ::autocomplete::do_autocomplete
    bind pub - !ac ::autocomplete::do_autocomplete

    proc do_autocomplete {nick uhost handle channel arg {re 0}} {

        if {[lsearch -exact [channel info $channel] "+autocomplete"] != -1} {
          set where $channel
        } else {
          set where $nick
        }
    
        # set up http 

        set baseurl "http://www.google.com/complete/search?hl=en&q="

        regsub -all " " $arg "%20" foo
        set query $baseurl
        append query $foo

        set ua "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e"
        set http [::http::config -useragent $ua -urlencoding "utf-8"]
        # stole this bit from rosc2112 on egghelp forums
        # borrowed is a better term, all procs eventually need this error handler.
        catch {set http [::http::geturl "$query" -timeout [expr 1000 * 10]]} error

        if {[string match -nocase "*couldn't open socket*" $error]} {
            dccbroadcast "Socket Error accessing $query"
            return
        }
        if { [::http::status $http] == "timeout" } {
            dccbroadcast "Connection has timed out..." 
            return
        }

        # CHECK CHECK
        upvar #0 $http state
        set html [::http::data $http]
        ::http::cleanup $http

        set data [split $html {[}]
        set linecount 0
        foreach line $data {
            # skip first three lines
            incr linecount
            if {$linecount > 3} {
               set f1 [string first {","} $line]
               set f2 [string last {","} $line]
               set e1 [expr $f1-1]
               set s2 [expr $f1+3]
               set e2 [expr $f2-1]
   
               set foo [string range $line 1 $e1]
               set bar [string range $line $s2 $e2]
               set b "\002"
               set s "  - "
               set out $b$foo$b$s$bar
               putserv "PRIVMSG $where : $out"
            }
        }
    }
}

Sample output:
[21:46] <x-y-no> !autocomplete how can i get my girlfriend
[21:46] <@s2k_testing> how can i get my girlfriend to shave - 340,000 results
[21:46] <@s2k_testing> how can i get my girlfriend to sleep with me - 8,260,000 results
[21:46] <@s2k_testing> how can i get my girlfriend to lose weight - 944,000 results
[21:46] <@s2k_testing> how can i get my girlfriend to break up with me - 12,700,000 results
[21:46] <@s2k_testing> how can i get my girlfriend back - 28,100,000 results
[21:46] <@s2k_testing> how can i get my girlfriend in the mood - 1,470,000 results
[21:46] <@s2k_testing> how can i get my girlfriend to love me again - 17,600,000 results
[21:46] <@s2k_testing> how can i get my girlfriend pregnant - 3,940,000 results
[21:46] <@s2k_testing> how can i get my girlfriend to - 28,600,000 results
[21:46] <@s2k_testing> how can i get my girlfriend to kiss me - 2,810,000 results
Post Reply