forked from Pistos/Reby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
websearch.rb
549 lines (484 loc) · 21 KB
/
websearch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# websearch.rb
# Performs a search on the web using any of various search engines.
# By Pistos - irc.freenode.net#mathetes
# This is not a standalone Ruby script; it is meant to be run from Reby
# (http://purepistos.net/eggdrop/reby).
# Usage:
# !google [number of results] <search terms>
# !teoma [number of results] <search terms>
# !atw [number of results] <search terms>
# etc.
require "open-uri"
require "cgi"
require 'rubygems'
require 'hpricot'
begin
require 'rubyful_soup'
rescue Exception => e
require_gem 'rubyful_soup'
end
class WebSearch
VERSION = '1.1.5'
LAST_MODIFIED = '2009-05-07'
MAX_RESULTS = 5
ENGINE_GOOGLE = 0
ENGINE_TEOMA = 1
ENGINE_ALLTHEWEB = 2
ENGINE_ALTAVISTA = 3
ENGINE_WIKIPEDIA = 4
ENGINE_ETYMONLINE = 5
ENGINE_GEOSHELL_WIKI = 6
ENGINE_SYNONYM_COM = 7
ENGINE_BADPUNS_COM = 8
MAX_IRC_LINE_LENGTH = 400
GOOGLEFIGHT_VERBS = [
[ 1000.0, "completely DEMOLISHES" ],
[ 100.0, "utterly destroys" ],
[ 10.0, "destroys" ],
[ 5.0, "demolishes" ],
[ 3.0, "crushes" ],
[ 2.0, "shames" ],
[ 1.2, "beats" ],
[ 1.0, "barely beats" ],
]
def initialize
$reby.bind( "pub", "-", "!google", "google", "$websearch" )
$reby.bind( "pub", "-", "!teoma", "teoma", "$websearch" )
$reby.bind( "pub", "-", "!atw", "allTheWeb", "$websearch" )
$reby.bind( "pub", "-", "!alltheweb", "allTheWeb", "$websearch" )
$reby.bind( "pub", "-", "!alta", "altaVista", "$websearch" )
$reby.bind( "pub", "-", "!altavista", "altaVista", "$websearch" )
$reby.bind( "pub", "-", "!wiki", "wikipedia", "$websearch" )
$reby.bind( "pub", "-", "!wikip", "wikipedia", "$websearch" )
$reby.bind( "pub", "-", "!pedia", "wikipedia", "$websearch" )
$reby.bind( "pub", "-", "!wikipedia", "wikipedia", "$websearch" )
$reby.bind( "pub", "-", "!etym", "etymOnline", "$websearch" )
$reby.bind( "pub", "-", "!syn", "synonym", "$websearch" )
# $reby.bind( "pub", "-", "!pun", "badPuns", "$websearch" )
$reby.bind( "pub", "-", "!pun", "pun", "$websearch" )
$reby.bind( "pub", "-", "!googlefight", "googlefight", "$websearch" )
$reby.bind( "pub", "-", "!gf", "googlefight", "$websearch" )
$reby.bind( "pub", "-", "!meme", "meme", "$websearch" )
$reby.bind( 'pub', '-', '!gloss', 'gloss', '$websearch' )
$reby.bind( 'pub', '-', '!define', 'gloss', '$websearch' )
$reby.bind( 'pub', '-', '!dict', 'wordsmyth', '$websearch' )
$reby.bind( 'pub', '-', '?down', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', 'down?', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', '!down', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', '!down?', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', '?up', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', 'up?', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', '!up', 'downforme', '$websearch' )
$reby.bind( 'pub', '-', '!up?', 'downforme', '$websearch' )
$reby.bind( "pub", "-", "!docs", "searchGeoShellDocs", "$websearch" )
$reby.bind( "pub", "-", "!rubybook", "searchPickAxe", "$websearch" )
$reby.bind( "pub", "-", "!rubydoc", "searchRubyDoc", "$websearch" )
$reby.bind( "pub", "-", "!rw", "search_ramaze_wiki", "$websearch" )
$reby.bind( "pub", "-", "!ramaze", "search_ramaze_wiki", "$websearch" )
end
def searchSite( nick, userhost, handle, channel, args, site )
search( nick, channel, args.to_a.push( "site:#{site}" ) )
end
# -----------
# You can setup some custom searches here.
def searchPickAxe( nick, uhost, handle, chan, arg )
searchSite( nick, uhost, handle, chan, arg, "phrogz.net" )
end
def searchRubyDoc( nick, uhost, handle, chan, arg )
searchSite( nick, uhost, handle, chan, arg, "www.ruby-doc.org" )
end
# -----------
def google( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_GOOGLE )
end
def search_ramaze_wiki( nick, userhost, handle, channel, args )
search( nick, channel, args + " site:ramaze.net -site:darcs.ramaze.net -site:hg.ramaze.net -site:p.ramaze.net", ENGINE_GOOGLE )
end
def searchGeoShellDocs( nick, uhost, handle, chan, args )
search( nick, chan, args, ENGINE_GEOSHELL_WIKI )
end
def teoma( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_TEOMA )
end
def allTheWeb( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_ALLTHEWEB )
end
def altaVista( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_ALTAVISTA )
end
def wikipedia( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_WIKIPEDIA )
end
def etymOnline( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_ETYMONLINE )
end
def synonym( nick, userhost, handle, channel, args )
search( nick, channel, args, ENGINE_SYNONYM_COM )
end
def badPuns( nick, userhost, handle, channel, args )
getResults(
"http://www.badpuns.com/jokes.php?section=oneline&pos=random",
/<br><br>(.+?)<br><br>/m,
channel,
1
)
end
def pun( nick, userhost, handle, channel, args )
doc = Hpricot( open( "http://www.punoftheday.com/cgi-bin/randompun.pl" ) )
p = doc.search( '#main-content p' )[ 0 ].inner_text
put p, channel
end
def gloss( nick, userhost, handle, channel, args )
search( nick, channel, args, :search_glossary )
end
def wordsmyth( nick, userhost, handle, channel, args )
search( nick, channel, args, :search_wordsmyth )
end
def google_count( term_array )
terms = CGI.escape( term_array.join( ' ' ) )
doc = Hpricot( open( "http://www.google.com/search?q=#{terms}" ) )
doc.at( '#ssb//b[3]' ).inner_text.gsub( ',', '' ).to_i
end
def number_with_delimiter( number, delimiter="," )
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
end
def googlefight( nick, userhost, handle, channel, args )
a = args.split( /,/ )
if a.size != 2
a = args.split( /\bv(?:ersu)?s(?:\.|\b)/ )
if a.size != 2
a = args.split( /\s+/, 2 )
end
end
if a.size != 2
put( "#{nick}: !googlefight <term(s)> [vs | ,] <term(s)>", channel )
else
a.collect! { |t| t.strip }
count1 = google_count( a[ 0 ] )
count2 = google_count( a[ 1 ] )
ratio1 = ( count2 != 0 ) ? count1.to_f / count2 : 99
ratio2 = ( count1 != 0 ) ? count2.to_f / count1 : 99
ratio = [ ratio1, ratio2 ].max
verb = GOOGLEFIGHT_VERBS.find { |x| ratio > x[ 0 ] }[ 1 ]
c1 = number_with_delimiter( count1 )
c2 = number_with_delimiter( count2 )
if count1 > count2
msg = "#{a[0]} #{verb} #{a[1]}! (#{c1} to #{c2})"
else
msg = "#{a[1]} #{verb} #{a[0]}! (#{c2} to #{c1})"
end
put( "#{nick}: #{msg}", channel )
end
end
def meme( nick, userhost, handle, channel, args )
n = 1
if args.to_s.to_i > 0
n = args.to_s.to_i
end
memes = open( "http://meme.boxofjunk.ws/moar.txt" ).readlines
memes[ 0...n ].each do |meme|
put meme, channel
end
end
def downforme( nick, userhost, handle, channel, args )
site = args.to_s.downcase[ /([a-z0-9.-]+)($|\/)/, 1 ]
doc = Hpricot( open( "http://downforeveryoneorjustme.com/#{site}" ) )
put( "#{nick}: [#{site}] " + doc.at( 'div#container' ).children.select{ |e| e.text? }.join( ' ' ).gsub( /\s+/, ' ' ).strip, channel )
end
def splitput( channel, text )
text.scan( /.{1,400}/ ) do |text_part|
$reby.putserv "PRIVMSG #{channel} :#{text_part}"
end
end
def put( message, destination = @channel )
$reby.putserv "PRIVMSG #{destination} :#{message}"
end
def search( nick, channel, args, engine = ENGINE_GOOGLE )
num_results = 1
@channel = channel
args_array = args.split( /\s+/ )
if args_array.length < 1
$reby.putserv "PRIVMSG #{channel} :!google [number of results] <search terms>"
return
end
if args_array[ 0 ].to_i.to_s == args_array[ 0 ]
# A number of results has been specified
num_results = args_array[ 0 ].to_i
if num_results > MAX_RESULTS
num_results = MAX_RESULTS
end
arg = args_array[ 1..-1 ].join( "+" )
unescaped_arg = args_array[ 1..-1 ].join( " " )
else
arg = args_array.join( "+" )
unescaped_arg = args_array.join( " " )
end
arg = CGI.escape( arg )
$reby.log "arg: #{arg}"
case engine
when ENGINE_GOOGLE
max_results = num_results
open( "http://www.google.com/search?q=#{ CGI.escape( args ) }&safe=active" ) do |html|
text = html.read
File.open( "websearch.last", "w" ) { |f| f.puts text }
counter = 0
text.scan /<a href="?([^"]+)" class=l.*?>(.+?)<\/a>/m do |match|
url, title = match
title.gsub!( /<.+?>/, "" )
ua = unescaped_arg.gsub( /-?site:\S+/, '' ).strip
put "[#{ua}]: #{url} - #{title}"
counter += 1
if counter >= max_results
break
end
end
if counter == 0
put "(no results)"
end
end
when ENGINE_TEOMA
getResults(
"http://s.teoma.com/search?q=#{ arg }",
/<div id="result".+?<a href=".+?u=([^"]+)"/m,
channel,
num_results
)
when ENGINE_ALLTHEWEB
getResults(
"http://www.alltheweb.com/search?q=#{ arg }",
/<span class="resURL">(.+?)[ <]/m,
channel,
num_results
)
when ENGINE_ALTAVISTA
getResults(
"http://www.altavista.com/web/results?q=#{ arg }",
/<span class=ngrn>(.+?)[ <]/m,
channel,
num_results
)
when ENGINE_ETYMONLINE
getResults(
"http://www.etymonline.com/index.php?term=#{ arg }",
/<dt(?: class="highlight")?>(.+?)<\/dd>/m,
channel,
num_results,
arg
)
when ENGINE_SYNONYM_COM
#if arg =~ /[a-zA-Z -]/
begin
open( "http://thesaurus.reference.com/search?q=#{ CGI.escape( unescaped_arg ) }" ) do |html|
text = html.read
num_printed = 0
text.scan( /Main Entry:(.+?)Source:/m ) do |entr|
entry = entr[ 0 ]
entry.gsub!( /<[^>]+?>/, "" )
entry.gsub!( / /, " " )
main_entry = entry[ /([a-zA-Z -]+)/, 1 ].strip
if main_entry.downcase == unescaped_arg.downcase
definition = entry[ /Definition:(.+)/, 1 ].strip
syns = entry[ /Synonyms:(.+)/, 1 ].strip
if num_printed < 2 or channel == "#mathetes"
dest = "PRIVMSG #{channel}"
else
dest = "NOTICE #{nick}"
if num_printed == 2
$reby.putserv "PRIVMSG #{channel} :(more results given in private to #{nick})"
end
end
$reby.putserv "#{dest} :[#{unescaped_arg}] #{definition} - #{syns}"
num_printed += 1
end
end
if num_printed == 0
$reby.putserv "PRIVMSG #{channel} :[syn #{unescaped_arg}] No synonyms found."
end
end
rescue Exception => e
$reby.putserv "PRIVMSG #{channel} :[syn #{unescaped_arg}] No synonyms found."
$reby.log( e.message + "\n" + e.backtrace.join( "\n\t" ) )
end
#else
#$reby.putserv "PRIVMSG #{channel} :[syn] Invalid input."
#end
when ENGINE_WIKIPEDIA
open( "http://en.wikipedia.org/w/wiki.phtml?search=#{ arg }" ) do |html|
soup = BeautifulSoup.new( html.read )
heading_tag = soup.find( 'h1', :attrs => { 'class' => 'firstHeading' } )
if heading_tag
title = heading_tag.string
case title
when 'Main_Page'
$reby.putserv "PRIVMSG #{channel} :No wikipedia entries found for '#{arg}'."
when 'Search'
count = -2
soup.find_all( 'a', :attrs => { 'href' => %r{^/wiki/} } ).each do |a|
if count >= 0
$reby.putserv "PRIVMSG #{channel} :[#{arg}] http://en.wikipedia.org#{a['href']}"
end
count += 1
if count >= num_results
break
end
end
else
$reby.putserv "PRIVMSG #{channel} :[#{arg}] http://en.wikipedia.org/wiki/#{title}"
end
end
end
when ENGINE_GEOSHELL_WIKI
open( "http://docs.geoshell.org/dosearchsite.action?searchQuery.queryString=#{ arg }" ) do |html|
text = html.read
counter = 0
text.scan( /<a href="(\/display[^"]+).+?<br\/>.+?(<span.+?)<\/td>/m ) do |url,desc|
d = desc.gsub( /<[^>]+>/, "" )
$reby.putserv "PRIVMSG #{channel} :[#{arg}] http://docs.geoshell.org#{url} - #{d}"
counter += 1
if counter >= max_results
break
end
end
if counter == 0
$reby.putserv "PRIVMSG #{channel} :[#{args}] No results."
end
end
when :search_glossary
index = num_results
open( "http://www.google.com/search?q=define%3A+#{arg}" ) do |html|
text = html.read
if text =~ /No definitions were found for/
$reby.putserv "PRIVMSG #{channel} :No definitions found for #{arg}."
else
definition_text = text[ /<ul.*?>(.+)<\/ul>/m, 1 ]
if definition_text != nil
definitions = definition_text.scan( /li>\s*([^<>]+?)</ )
counter = 1
definitions.each do |defn|
if index <= counter
$reby.putserv "PRIVMSG #{channel} :" + CGI.unescapeHTML( defn.to_s )
break
end
counter += 1
end
end
end
end
when :search_wordsmyth
open( "http://www.wordsmyth.net/live/home.php?script=search&matchent=#{arg}&matchtype=exact" ) do |html|
parse_wordsmyth( html.read, channel )
end
end
end
def parse_wordsmyth( text, channel )
soup = BeautifulSoup.new( text )
not_found_p = soup.find( Proc.new { |el|
el.respond_to?( :name ) &&
el.name == 'p' &&
el.find_text( /Sorry, we could not find/ )
} )
if not_found_p
suggestions = []
not_found_p.find_all( 'a' ).each do |a|
suggestions << a.string
end
output = '(no results)'
if not suggestions.empty?
output << " Close matches: #{suggestions.join( ', ' )}"
end
splitput channel, output
return
end
maintable = soup.find( 'table', :attrs => { 'cellspacing'=>'0', 'border'=>"0", 'cellpadding'=>"2", 'width'=>"100%", 'bgcolor' => nil } )
wordtag = maintable.find( 'div', { :attrs => { 'class' => 'headword' } } )
if wordtag
word = wordtag.contents[ 0 ]
end
# Iterate through all <tr>s, find relevant bits.
output = ""
maintable.next_parsed_items do |tr|
next if not tr.respond_to?( :name ) or tr.name != 'tr'
main_td = tr.find( 'td', :attrs => { 'width' => '70%' } )
middle_td = tr.find( 'td', :attrs => { 'width' => '5%', 'valign' => 'baseline' } )
# Part of Speech
if tr[ 'bgcolor' ] == '#DDDDFF'
pos = main_td.span.string
if not output.empty?
splitput channel, output
end
output = "#{word} - [#{pos}]"
end
if tr[ 'bgcolor' ] == '#FFFFFF'
# Pronunciation
prontag = tr.find( 'div', :attrs => { 'class' => 'pron' } )
if prontag
syllabification = []
prontag.each do |syllable|
if syllable.respond_to? :string and syllable.string
syllable_class = syllable[ 'class' ]
if syllable_class
stress_level = syllable_class[ /(\d)/, 1 ].to_i
case stress_level
when 1
stress = "'"
when 2
stress = '"'
else
stress = ''
end
syllabification << stress + syllable.string
else
syllabification << syllable.string
end
end
end
output << " (" + syllabification.join( ' ' ) + ")"
end
# Definition
if main_td
def_span = main_td.find( 'span', :attrs => { 'style' => 'font-weight: normal;' } )
if def_span
output << " " + middle_td.span.string + " " + def_span.string
end
end
end
end
if not output.empty?
splitput channel, output
end
end
def getResults( search_url, regexp, channel, max_results, search_term = "" )
open( search_url ) do |html|
text = html.read
counter = 0
text.scan regexp do |url|
case url
when Array
url.collect! do |u|
u.gsub( /\n/m, " " ).gsub( /<.+?>/, "" )
end
when String
u.gsub!( /\n/m, " " )
u.gsub!( /<.+?>/, "" )
end
output = CGI.unescapeHTML( url.to_s )
while output.length > MAX_IRC_LINE_LENGTH
segment = output[ 0...MAX_IRC_LINE_LENGTH ]
output = output[ MAX_IRC_LINE_LENGTH..-1 ]
st = search_term.gsub( /-?site:\S+/, '' )
$reby.puthelp "PRIVMSG #{channel} :[#{st}] #{segment}"
end
$reby.puthelp "PRIVMSG #{channel} :#{output}"
counter += 1
if counter >= max_results
break
end
end
if counter == 0
st = search_term.gsub( /-?site:\S+/, '' )
$reby.puthelp "PRIVMSG #{channel} :[#{st}] No results found."
end
end
end
end
$websearch = WebSearch.new