#!/usr/bin/env ruby
#
# Author: yoosee at init dot org
# License: GPLv2
# Created: 2005-09-04 22:12:48 JST
#
require 'cgi'
require 'open-uri'
require 'zlib'
require 'amazon-a2s'
$KCODE='UTF-8'
WISHLIST_TOKEN= "3G393KUU1QUHV"
num_display = 3
num_fetch_items = 10
cachedir = '/tmp/yoosee/amazon'
#WISHLIST_URI="http://www.amazon.co.jp/exec/obidos/registry/#{WISHLIST_TOKEN}/"
WISHLIST_URI="http://www.amazon.co.jp/gp/registry/wishlist/#{WISHLIST_TOKEN}/"
def fetch(uri)
opts = {
"Accept-Encoding"=>"gzip, deflate",
"User-Agent"=>'Mozilla/6.0 UriCheckerBot'
}
page, meta, content_type, status, message = nil, nil, nil, nil, nil
begin
page = timeout(30) { URI.parse(uri).open(opts) {|f|
content_type = f.content_type
f.read
}
}
rescue OpenURI::HTTPError
STDERR.puts 'HTTP Error'
return ''
rescue StandardError, TimeoutError
STDERR.puts 'Error'
return ''
end
return '' unless content_type == 'text/html' or content_type ==
nil
if /\A\x1f\x8b/ =~ page
begin
page = Zlib::GzipReader.new(StringIO.new(page)).read || ''
rescue Zlib::Error
end
end
page
end
def build_product_html(product)
text = String.new
title = " #{product.title}"
image_text = ""
image_text << "
\n"
text << image_text
text << "#{product.title}
\n"
text << "
\n"
=begin
amazon_base = "http://images-jp.amazon.com/images/G/09/x-locale/common/customer-reviews/"
rating_total = 0
if product['total_customer_reviews'] && product['total_customer_reviews'] > 0
average_stars = ""
text << "#{average_stars} (#{product['total_customer_reviews']}レビュー)
\n"
end
text << '
'
=end
return text
end
aws_info = {
:AWSAccessKeyId => "1X7MZVP4YBE35YP64PG2",
:AssociateTag => "yooseenet-22",
}
aws_secret_key = "VDJL3Lefr+DpYwUSHMjdK5kDMj/v+ZU7KvmQY9uz"
aws_region = "jp"
aws = AmazonA2S.new(aws_info, aws_secret_key, aws_region)
aws.cache_dir = cachedir
page_max = (num_fetch_items / 10).floor
products = Array.new
=begin
(1..page_max).each do |page|
begin
products += request.wishlist_search(WISHLIST_TOKEN, HEAVY, page).products
rescue Request::SearchError
end
end
#products.flatten!
=end
# products is list of ASIN, not Amazon::product
wishlist_cachefile = "#{cachedir}/wishlistcache.txt"
if !File.exist?(wishlist_cachefile) ||
File.mtime(wishlist_cachefile) < Time.now - (24*3600)
fetch(WISHLIST_URI).split(/\n/).each do |line|
if(/ASIN\/([0-9A-Z]+)\// =~ line)
products.push($1)
elsif(/gp\/product\/([0-9A-Z]+)[\?\/]/ =~ line)
products.push($1)
elsif(/\/dp\/([0-9A-Z]+)[\?\/]/ =~ line)
products.push($1)
end
end
products.uniq!
#puts products.join(", ")
File.open(wishlist_cachefile, "w") do |file|
file.puts products.join(",")
end
else
File.open(wishlist_cachefile).each do |line|
products = line.split(/,/) unless line.empty?
end
end
exit if products.empty?
num_items = num_fetch_items > products.length ? num_fetch_items : products.length
num_display = products.length if products.length < num_display
html = String.new
(0..num_items-1).sort_by{rand}[0..num_display-1].each do |i|
# html << build_product_html(products[i]) if products[i]
next if products[i].empty?
item = aws.lookup products[i]
html << build_product_html(item)
end
html << "