Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 88 lines (73 sloc) 2.602 kB
54a434b Initial import.
Pistos authored
1 # google.rb
2
3 # Performs a google search.
4 # By Pistos - irc.freenode.net#geoshell
5
6 # This is not a standalone Ruby script; it is meant to be run from Reby
7 # (http://purepistos.net/eggdrop).
8
9 # Usage:
10 # !google [number of results] <search terms>
11
12 require "open-uri"
13
14 class Google
15 def initialize
16 @MAX_RESULTS = 5
17 $reby.bind( "pub", "-", "!google", "search", "$google" )
18 $reby.bind( "pub", "-", "!docs", "searchGeoShellDocs", "$google" )
19 $reby.bind( "pub", "-", "!rubybook", "searchPickAxe", "$google" )
20 $reby.bind( "pub", "-", "!rubydoc", "searchRubyDoc", "$google" )
21 end
22
23 def searchSite( nick, userhost, handle, channel, args, site )
24 search( nick, userhost, handle, channel, args.to_a.push( "site:#{site}" ) )
25 end
26
27 # -----------
28 # You can setup some custom searches here.
29
30 def searchGeoShellDocs( nick, uhost, handle, chan, arg )
31 searchSite( nick, uhost, handle, chan, arg, "docs.geoshell.com" )
32 end
33 def searchPickAxe( nick, uhost, handle, chan, arg )
34 searchSite( nick, uhost, handle, chan, arg, "phrogz.net" )
35 end
36 def searchRubyDoc( nick, uhost, handle, chan, arg )
37 searchSite( nick, uhost, handle, chan, arg, "www.ruby-doc.org" )
38 end
39
40 def search( nick, userhost, handle, channel, args )
41 num_results = 1
42
43 if args.class == Array
44 if args.length < 1
45 $reby.putserv "PRIVMSG #{channel} :!google [number of results] <search terms>"
46 return
47 end
48 if args[ 0 ].to_i.to_s == args[ 0 ]
49 # A number of results has been specified
50 num_results = args[ 0 ].to_i
51 if num_results > @MAX_RESULTS
52 num_results = @MAX_RESULTS
53 end
54 arg = args[ 1..-1 ].join( "+" )
55 else
56 arg = args.join( "+" )
57 end
58 else
59 arg = args
60 end
61
62 open( "http://www.google.com/search?q=#{ arg }" ) do |html|
63 text = html.read
64 counter = 0
65 text.scan /<p class=g><a href=([^>]+)>/m do |url|
66 $reby.putserv "PRIVMSG #{channel} :#{url}"
67 counter += 1
68 if counter >= num_results
69 break
70 end
71 end
72 end
73
74 end
75 end
76
77 class String
78 def escapeGoogle( str )
79 newstring = str.gsub( /\s+/, "+" )
80 newstring.gsub!( /[^\w\.]/ ) do |match|
81 "%%%02X" % match[ 0 ]
82 end
83 return newstring
84 end
85 end
86
87 $google = Google.new
Something went wrong with that request. Please try again.