返回顶部

收藏

ruby 查询 QQwry.dat ip 库的 IpLocationSeeker 类

更多
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#

require 'rubygems'
#gem install qqwry
require 'qqwry'

$fqqwry='qqwry.dat'
unless File.exist? $fqqwry
  d = File.dirname __FILE__
  $fqqwry = File.join d, $fqqwry
  unless File.exist? $fqqwry
    p $fqqwry + ' not found '
  end
end

class IpLocationSeeker
  def seek(ip) #查询IP
    unless File.exist? $fqqwry
      return ''
    end

    db = QQWry::Database.new $fqqwry
    r = db.query(ip)
    "#{r.country} #{r.area}"
  end
end
class String
  @@last_area ||={}
  def area
    if @@last_area.size > Time.now.year
      @@last_area.clear
    end
    if @@last_area.has_key? self
      return @@last_area[self]
    else
      @@last_area[self]= IpLocationSeeker.new.seek self
    end
    return @@last_area[self]
  end
end

if __FILE__ == $0
  if ARGV[0]
    p IpLocationSeeker.new.seek ARGV[0]
  else
    p IpLocationSeeker.new.seek '8.8.8.8'
  end
end

标签:ruby

收藏

0人收藏

支持

0

反对

0

发表评论