-
-
Save hitode909/388724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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