# # googleplot - a tool to plot a graph of Google's results count # # Copyright (C) 2003 Satoru Takabayashi # All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # # You can redistribute it and/or modify it under the terms of # the GNU General Public License version 2. # require 'mingplot' require 'net/http' class GooglePlotInfo < MingPlotInfoBase include Commify def initialize (key, count) @key = key @count = count end attr_reader :key attr_reader :count def title @key end def web_host "www.google.com" end def to_html g = SimpleHtmlGenerator.new g.ul(:class => "chart_info") { g.strong { "Google Count: " } + commify(@count) } end def search_url sprintf("http://%s/search?q=%s&ie=UTF-8&oe=UTF-8" , web_host, CGI.escape(key)) end end class GooglePlotSearcher def initialize (config = {}) @service_host = "www.google.com" end public def search (key) query = CGI.escape(key) Net::HTTP.start(@service_host, 80) {|http| response = http.get2("/search?q=#{query}&ie=UTF-8&oe=UTF-8") if m = /\bswrnum=(\d+)\b/.match(response.body) count = m[1].to_i return GooglePlotInfo.new(key, count) end } return nil end end class GooglePlotStat < MingPlotStatBase def initialize (key, config) super(key, config) end def draw_chart_internal (data, file_name) chart = MingChart.new(:flash_font => flash_font, :x_time_scale => true, :grid => true) data = data.map {|time, info| count = if info.is_a?(GooglePlotInfo) info.count else info # old format end [ time, count ] } chart.add_data(data) chart.draw chart.save(chart_file_name) end public def get_info search = GooglePlotSearcher.new search.search(key) end def service_name "google" end end