Skip to content

Instantly share code, notes, and snippets.

@udonchan
Created May 3, 2010 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save udonchan/388158 to your computer and use it in GitHub Desktop.
Save udonchan/388158 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'uri'
require 'open-uri'
class Results
@l = nil
@cache = nil
def initialize
c = open('cache', 'r')
begin
@l = Marshal.load(c.read)
rescue Exception
@l = Hash::new
ensure
c.close
end
end
def open_cache?
@cache != nil
end
def open_cache
@cache = open('cache', 'wb') unless open_cache?
end
protected :open_cache
def write_cache
open_cache
@cache.puts(Marshal.dump(@l))
end
protected :write_cache
def close
@cache.close if open_cache?
end
def results(q)
if @l[q] == nil
@l[q] = open(URI.parse(URI.encode("http://www.google.gr/search?hl=en&q=#{q}")), 'User-Agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.4) Gecko/20100413 Firefox/3.6.4').read.scan(/Results <b>1<\/b> - <b>10<\/b> of about <b>(.*)<\/b> for <b>/).first.first.gsub(',', '').to_i
write_cache
end
@l[q]
end
end
r = Results::new
puts r.results('沢城みゆき')
r.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment