Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (99 sloc) 3.663 kB
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
1 # locate.rb
2
3 # Does a geographic search by nick.
4 # By Pistos - irc.freenode.net#mathetes
5
6 # This is not a standalone Ruby script; it is meant to be run from Reby
7 # (http://purepistos.net/eggdrop/reby).
8
9 # Usage:
10 # !locate <nick>
11
12 require 'open-uri'
7f7ab60 Added require 'rubygems' line.
Pistos authored
13 require 'rubygems'
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
14 require 'mechanize'
15 require 'cgi'
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
16
17 class GeoLocate
18 def initialize
19 $reby.bind( "pub", "-", "!locate", "locate", "$locate" )
cf19e2a Only responds to WHOIS when requests are made. See ticket:63.
Pistos authored
20 @requests = 0
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
21 @agent = WWW::Mechanize.new
cf19e2a Only responds to WHOIS when requests are made. See ticket:63.
Pistos authored
22 end
f71f64a Meaningless update to locate.rb.
Pistos authored
23
cf19e2a Only responds to WHOIS when requests are made. See ticket:63.
Pistos authored
24 def bindWhoisResponse
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
25 $reby.bind( "raw", "-", "311", "locate_ip", "$locate" )
26 end
cf19e2a Only responds to WHOIS when requests are made. See ticket:63.
Pistos authored
27 def unbindWhoisResponse
28 $reby.unbind( "raw", "-", "311", "locate_ip", "$locate" )
29 end
f71f64a Meaningless update to locate.rb.
Pistos authored
30
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
31 def locate_ip( from, keyword, text )
cf19e2a Only responds to WHOIS when requests are made. See ticket:63.
Pistos authored
32 return if(
33 keyword != "311" or
34 @ip_channel.empty? or
35 $reby.isbotnick( @ip_nick )
36 )
37 @requests -= 1
38 if @requests <= 0
39 unbindWhoisResponse
40 @requests = 0
41 end
f71f64a Meaningless update to locate.rb.
Pistos authored
42
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
43 ip_address = text.split()[ 3 ]
f71f64a Meaningless update to locate.rb.
Pistos authored
44
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
45 threads = []
46 country = ""
47 region = ""
48 city = ""
f71f64a Meaningless update to locate.rb.
Pistos authored
49
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
50 begin
f71f64a Meaningless update to locate.rb.
Pistos authored
51 # doc = Hpricot( open( "http://www.geoip.co.uk/?IP=#{ip_address}" ) )
52 # data = doc.at('#mapinfo .textleft').to_enum( :traverse_text ).zip( doc.at('#mapinfo .textright' ).to_enum(:traverse_text)).map{ |a,b|
53 # [ a.inner_text.strip,b.inner_text.delete(':').strip ]
54 # }
55 # country = data[ 3 ][ 1 ]
56 # region = data[ 5 ][ 1 ]
57
58 doc = Hpricot( open( "http://www.geobytes.com/IpLocator.htm?GetLocation&ipaddress=#{ip_address}" ) )
59 country = doc.at( "[@name='ro-no_bots_pls13']" )[ 'value' ]
60 region = doc.at( "[@name='ro-no_bots_pls15']" )[ 'value' ]
61 city = doc.at( "[@name='ro-no_bots_pls17']" )[ 'value' ]
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
62 rescue Exception => e
f71f64a Meaningless update to locate.rb.
Pistos authored
63 $reby.log e.message
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
64 end
f71f64a Meaningless update to locate.rb.
Pistos authored
65
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
66 if not city.empty?
67 put "I estimate that #{@ip_nick} is somewhere near #{city}, #{region}, #{country}.", @ip_channel
0ef2180 Added region to locator output.
Pistos authored
68 t = time_in( city, region, country )
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
69 if t
70 put "Local time in #{@time_place} is #{t}.", @ip_channel
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
71 end
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
72 elsif not country.empty?
73 put "I estimate that #{@ip_nick} is somewhere in #{country}.", @ip_channel
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
74 else
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
75 put "Unable to !locate #{@ip_nick}.", @ip_channel
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
76 end
77 end
f71f64a Meaningless update to locate.rb.
Pistos authored
78
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
79 def put( message, destination = ( @channel || 'Pistos' ) )
80 $reby.putserv "PRIVMSG #{destination} :#{message}"
81 end
f71f64a Meaningless update to locate.rb.
Pistos authored
82
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
83 def locate( nick, userhost, handle, channel, args )
84 @ip_nick = args.split()[ 0 ]
85 @ip_channel = channel
86 $reby.putquick "PRIVMSG #{@ip_channel} :Searching for #{@ip_nick} ..."
cf19e2a Only responds to WHOIS when requests are made. See ticket:63.
Pistos authored
87 @requests += 1
88 bindWhoisResponse
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
89 $reby.putserv "WHOIS #{@ip_nick}"
90 end
f71f64a Meaningless update to locate.rb.
Pistos authored
91
0ef2180 Added region to locator output.
Pistos authored
92 def time_in( city, region, country )
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
93 @time_place = nil
0ef2180 Added region to locator output.
Pistos authored
94 place = CGI.escape( "#{city}, #{region}, #{country}" )
ad1e769 Changed locate.rb from RubyfulSoup to Mechanize.
Pistos authored
95 search_results = @agent.get "http://www.timeanddate.com/search/results.html?query=#{place}"
96 links = search_results.links.find_all { |l| l.text =~ /Current local time in/ }
97 link = links.find { |l| l.text =~ /#{city}/i }
98 if not link
99 link = links.find { |l| l.text =~ /#{country}/i }
100 if not link
101 link = links.first
102 end
103 end
104 if link
105 page = @agent.click( link )
106 s = page.at( "h1[text()*='Current local time in']" ).inner_text
107 if s
108 @time_place = s[ /time in (.+)/, 1 ]
109 end
110 page.at( '#ct' ).inner_text
111 end
112 end
93c2b33 Added new !locate script, replacing old tcl-ruby combo scripts.
Pistos authored
113 end
114
115 $locate = GeoLocate.new
Something went wrong with that request. Please try again.