Skip to content

Instantly share code, notes, and snippets.

@hitode909
Forked from udonchan/results.rb
Created May 3, 2010 23:28

Revisions

  1. @udonchan udonchan revised this gist May 3, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions results.rb
    Original file line number Diff line number Diff line change
    @@ -24,12 +24,12 @@ def open_cache?
    end

    def open_cache
    @cache = open('cache', 'wb')
    @cache = open('cache', 'wb') unless open_cache?
    end
    protected :open_cache

    def write_cache
    open_cache unless open_cache?
    open_cache
    @cache.puts(Marshal.dump(@l))
    end
    protected :write_cache
  2. @udonchan udonchan revised this gist May 3, 2010. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions results.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    class Results

    @l = nil
    @cache =nil
    @cache = nil

    def initialize
    c = open('cache', 'r')
    @@ -47,5 +47,7 @@ def results(q)
    end
    end

    puts Results::new.results('沢城みゆき')
    r = Results::new
    puts r.results('沢城みゆき')
    r.close

  3. @udonchan udonchan revised this gist May 3, 2010. 1 changed file with 37 additions and 12 deletions.
    49 changes: 37 additions & 12 deletions results.rb
    Original file line number Diff line number Diff line change
    @@ -4,23 +4,48 @@
    require 'open-uri'

    class Results
    def Results.results(q)
    cache = open('cache', 'r')

    @l = nil
    @cache =nil

    def initialize
    c = open('cache', 'r')
    begin
    l = Marshal.load(cache.read)
    @l = Marshal.load(c.read)
    rescue Exception
    l = Hash::new
    @l = Hash::new
    ensure
    cache.close
    c.close
    end
    if l[q] == nil
    cache = open('cache', 'wb')
    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
    cache.puts(Marshal.dump(l))
    cache.close
    end

    def open_cache?
    @cache != nil
    end

    def open_cache
    @cache = open('cache', 'wb')
    end
    protected :open_cache

    def write_cache
    open_cache unless 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]
    @l[q]
    end
    end

    puts Results::results('沢城みゆき')
    puts Results::new.results('沢城みゆき')

  4. @udonchan udonchan revised this gist May 3, 2010. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion results.rb
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,21 @@

    class Results
    def Results.results(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
    cache = open('cache', 'r')
    begin
    l = Marshal.load(cache.read)
    rescue Exception
    l = Hash::new
    ensure
    cache.close
    end
    if l[q] == nil
    cache = open('cache', 'wb')
    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
    cache.puts(Marshal.dump(l))
    cache.close
    end
    l[q]
    end
    end

  5. @udonchan udonchan created this gist May 3, 2010.
    12 changes: 12 additions & 0 deletions results.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    #!/usr/bin/env ruby
    # -*- coding: utf-8 -*-
    require 'uri'
    require 'open-uri'

    class Results
    def Results.results(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
    end
    end

    puts Results::results('沢城みゆき')