Skip to content

Instantly share code, notes, and snippets.

@kakutani
Created August 15, 2008 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakutani/5551 to your computer and use it in GitHub Desktop.
Save kakutani/5551 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'nkf'
$KCODE = "u" if VERSION < "1.9.0"
class AmazonItem
attr_accessor :asin
def initialize(asin)
@asin = asin
end
def url
"http://amazon.jp/dp/%s" % @asin
end
def title
@title ||= Hpricot(content).search("#btAsinTitle").inner_text
end
def rank
return @rank if @rank
rank = Hpricot(content).search("#SalesRank").inner_text.scan(/([\d,]*)位/u)[0][0]
@rank = Integer(rank.tr(",","")) rescue nil
end
private
def content
@doc ||= NKF.nkf("-w", open(url).read)
end
end
def write_sales_rank_to(out, asin)
item = AmazonItem.new(asin)
[out, $stdout].each do |o|
o.write("#{item.asin}\t#{Time.now.to_i}\t#{item.rank}\n")
end
end
TSV_BASE = File.expand_path("amazon.co.jp/sales_rank", ENV["HOME"])
def crowl_sales_rank(asin, fname)
path = "#{TSV_BASE}/#{fname}.tsv"
File.open(path, 'a') do |out|
write_sales_rank_to(out, asin)
end
end
entries = {
"4774127280" => "lifehack-press",
"4873113202" => "java2ruby",
"4274066940" => "agile_practice",
"4873113660" => "interface_oriented_design",
}
entries.each do |asin, fname|
crowl_sales_rank(asin, fname)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment