# -*- Mode: ruby ; Coding: sjis-dos -*-
# $Id: amazondvd.rb,v 1.9 2002-09-06 00:21:35+09 waka Exp $
# http://wakahiko.cool.ne.jp/
=begin
※ほとんど amazon.rb のコメントそのままです。:-)
(DVD:BCBA-0618) (CD:AMCY-7125) みたいな
(DVD:背表紙にある記号) (CD:背表紙にある記号)
という形式の文字列があると
amazon からDVD,PS2データやらパッケージ画像やらを引っ張ってきて置き換えます。
ndiary.conf の LOG_DIRECTORY で指定したディレクトリに amzcacheDVD というディレクトリを
作っておくと取得したデータをキャッシュしておくようになります。
キャッシュの有効期限もスクリプト内で指定しているので必要に応じて変更してください。
ndiary.conf で
PROXY = 'proxy.hoge.com:8080'
と指定しておくと、情報取得時プロクシを経由するようになります。
=end
class Module
def attr_my_accessordvd( name )
module_eval %-
def #{name.id2name}()
get_dvddata
@#{name.id2name}
end
-
end
end
class Amazondvd
require 'net/http'
attr_accessor :dvdid
attr_accessor :cddvdps2
attr_my_accessordvd :title
attr_my_accessordvd :actor
attr_my_accessordvd :maker
attr_my_accessordvd :publisher
attr_my_accessordvd :director
attr_my_accessordvd :size
attr_my_accessordvd :pubdate
attr_my_accessordvd :price
attr_my_accessordvd :desc
attr_my_accessordvd :writer
attr_my_accessordvd :image_url
attr_my_accessordvd :image_width
attr_my_accessordvd :image_height
attr_my_accessordvd :media
attr_my_accessordvd :site
attr_reader :cache_dir
attr_accessor :expire
attr_accessor :proxy
def initialize
@expire = 14
@proxy = nil
end
def load_cache
if @flag or
(File::directory?(@cache_dir.to_s) and File::readable?(@cache_dir + @dvdid) and
File::mtime(@cache_dir + @dvdid) < Time::now.to_i - 3600 * 24 * @expire) then
return
end
@flag = true
read_cache
end
def read_cache
begin
open(@cache_dir + @dvdid){ |f|
@title, @actor, @publisher, @maker, @size, @pubdate, @price, @desc, @writer, @image_url,@image_width, @image_height, @media, @director, @site, @cddvdps2 = f.read.split(/\t/)
}
rescue
@flag = nil
end
end
def save_cache
return unless File::directory?(@cache_dir.to_s)
begin
open(@cache_dir + @dvdid, 'w'){ |f|
f.write [ @title, @actor, @publisher, @maker,@size, @pubdate, @price, @desc, @writer, @image_url,@image_width, @image_height, @media, @director, @site, @cddvdps2 ].join("\t")
}
rescue
#
end
end
def httpconnect(str)
if @proxy then
proxy, port = @proxy.split(':')
port = port.to_i
else
proxy = port = nil
end
Net::HTTP::Proxy(proxy, port).new( str.to_s, 80 )
end
def cache_dir=(dir)
dir << '/' unless dir[-1,1] == '/'
@cache_dir = dir
end
def large_image_url
get_dvddata
@image_url.sub('MZZZZZZZ','LZZZZZZZ')
end
def get_dvddata
load_cache
return if @flag
dvdid = @dvdid
cddvdps2 = @cddvdps2
path = "/exec/obidos/search-handle-url/index=blended%26field-keywords=#{dvdid.gsub(/\_/,'%3D')}"
url = "www.amazon.co.jp"
http = httpconnect(url)
response , = http.get2(path)
if response.code == '302' then
path = response.header['location'].sub('http://www.amazon.co.jp', '')
response , = http.get2(path)
end
if %r|^
\n]+>)| =~ response.body then
path2=$1
end
if path2.to_s.empty? then
if cddvdps2 == 'DVD' then
path = "/php/item/#{dvdid}"
http = httpconnect("www.jp.playstation.com")
response , = http.get2(path)
if response.code == '302' then
path = response.header['location'].sub('http://www.jp.playstation.com', '')
response , = http.get2(path)
end
if %r|(\w+)| =~ response.body then
keyword_trade = $1
end
if %r| | 発売日 | \n([\d\/]+[\d])| =~ response.body then
keyword_date = $1
end
keyword = keyword_trade + " " + keyword_date
end
# Google!!
# ' ' ===> %20 ; '?' ===> %3f ; '+' ===> %2b ; '%' ===> %25
# '&' ===> %26 ; '=' ===> %3d ; '/' ===> %2f ; ':' ===> %3a
path = "/search%3fhl=ja%26inlang=ja%26ie=Shift_JIS%26q=site%3Awww.amazon.co.jp%2b#{keyword.gsub(/\ /,'%2b')}%2bイメージを拡大"
url = "www.google.com"
http = httpconnect(url)
response , = http.get2(path)
if response.code == '302' then
path = response.header['location'].sub('http://www.google.com', '')
response , = http.get2(path)
end
if %r| | .+]+)>Amazon.co.jp:| =~ response.body then
path2 = $1
@site = "Google: #{keyword} イメージを拡大"
else
path = "/search%3fhl=ja%26inlang=ja%26ie=Shift_JIS%26q=site%3Awww.amazon.co.jp%2b#{keyword_trade.gsub(/\ /,'%2b')}"
url = "www.google.com"
http = httpconnect(url)
response , = http.get2(path)
if response.code == '302' then
path = response.header['location'].sub('http://www.google.com', '')
response , = http.get2(path)
end
if %r|.+]+)>Amazon.co.jp:| =~ response.body then
path2 = $1
@site = "Google: #{keyword_trade}"
else
@site = ""
@title = "#{keyword_trade}"
save_cache
return if @title
end
end
else
@site = "Amazon"
end
url = "www.amazon.co.jp"
http = httpconnect(url)
response , = http.get2(path2)
if response.code == '302' then
path2 = response.header['location'].sub('http://www.amazon.co.jp', '')
response , = http.get2(path2)
end
#--------------------------------------------------
# Title
#--------------------------------------------------
if %r|^Amazon.co.jp:(.+?)$| =~ response.body then
@title = "#{$1.strip}"
end
#--------------------------------------------------
# Maker
#--------------------------------------------------
if %r|^[^<]+
(.+)
| =~ response.body then
@maker = $1
end
if @maker.nil? then
if %r|(\w+) - ASIN:| =~ response.body then
@maker = $1
end
end
#--------------------------------------------------
# Image
#--------------------------------------------------
if %r|
定価: [^¥]+(¥[\d,]+)| =~ response.body then
@price = $1
end
# 定価表示ではなく価格のみ表示の場合
if @price.nil? then
if %r|価格:[^¥]+(¥[\d,]+)| =~ response.body then
@price = $1
end
end
#--------------------------------------------------
# Actor
#--------------------------------------------------
if %r|出演: (.*)| =~ response.body then
@actor = $1
end
#--------------------------------------------------
# Director
#--------------------------------------------------
if %r|監督: (.*)| =~ response.body then
@director = $1
end
#--------------------------------------------------
# Description
#--------------------------------------------------
if %r"(出版社/著者からの内容紹介|内容(「.+?」データベースより)|メーカーより|作品内容)
+(.+?) *<[pP]>" =~ response.body then
@writer =$1
@desc = $2
end
#--------------------------------------------------
# Published Date
#--------------------------------------------------
#(2002/05/24)
:DVD,CD
#発売日:2002/03/28
:PS2
if %r"^(?:発売日:|\()([\d\/]+[\d])\)?
" =~ response.body then
@pubdate = $1
end
#--------------------------------------------------
# Size(ディスク枚数)
#--------------------------------------------------
if %r"^ディスク枚数:(.+)
" =~ response.body then
@size = $1
end
#--------------------------------------------------
# CD,DVD or PS2?
#--------------------------------------------------
if %r|この(\w+)に\w+レビューを書| =~ response.body then
@media = $1.gsub(/商品/,'DVD (PlayStation2)')
end
if @title.nil? then
read_cache
else
save_cache
@flag = true
end
end
end
class Filter
def amazondvd(str, type)
case type
when :P, :UL, :DL
str.gsub!(/[\((](CD|DVD):(.+)[\))]/i){
cddvdps2 = $1
dvdid = $2.gsub(/^([A-Za-z]+)(\d+)$/, "\\1-\\2")
amzdvd = Amazondvd::new
amzdvd.cache_dir = @diary.logDirectory + 'amzcacheDVD'
amzdvd.expire = 14 # キャッシュの有効日数
amzdvd.dvdid = dvdid
amzdvd.cddvdps2 = cddvdps2
body = img = desc = media = maker = director = size = desc = ''
unless amzdvd.title.nil?
unless amzdvd.image_url.to_s.empty? then
img = "
(.*)<\/a>/)[0]}\" style=\"margin-right: 2.0em; margin-left: 2.0em; padding: 10px 10px;\">"
else
img = "No Image
"
end
title = "『#{amzdvd.title}』" unless amzdvd.title.to_s.empty?
media = "#{amzdvd.media}\n" unless amzdvd.media.to_s.empty?
maker = "#{amzdvd.maker}\n" unless amzdvd.maker.to_s.empty?
# publisher = "#{amzdvd.publisher}\n" unless amzdvd.publisher.to_s.empty?
actor = "\n出演者: #{amzdvd.actor}" unless amzdvd.actor.to_s.empty?
director = "\n監督: #{amzdvd.director}" unless amzdvd.director.to_s.empty?
pubdate = "\n発売日: #{amzdvd.pubdate}" unless amzdvd.pubdate.to_s.empty?
price = ", 価格: #{amzdvd.price.gsub(/¥/,'\\')}" unless amzdvd.price.to_s.empty?
size = ", 枚数: #{amzdvd.size}" unless amzdvd.size.to_s.empty?
desc = "\n\n#{amzdvd.desc}" unless amzdvd.desc.to_s.empty?
writer = "\n\n--#{amzdvd.writer}--" unless amzdvd.writer.to_s.empty?
site ="\n#{amzdvd.site}" unless amzdvd.site.to_s.empty?
body = "| #{img} | #{title} #{media} #{maker} #{actor} #{director} #{pubdate} #{price} #{size} #{writer} #{desc} #{site} |
"
end
body
}
end
end
end