egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

google autocomplete

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Support & Releases
View previous topic :: View next topic  
Author Message
x-y-no
Voice


Joined: 09 Aug 2009
Posts: 7
Location: Fort Lauderdale, FL

PostPosted: Sat Feb 27, 2010 11:45 pm    Post subject: google autocomplete Reply with quote

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:

# 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:

Quote:

[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

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Support & Releases All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber