Bk1の背表紙イメージをISBNから取得できたので、ついでにセブンアンドワイもやってみた。
セブンアンドワイはISBNから検索できるので、検索結果から商品IDを取得する
<?php
/***
* セブンアンドワイの商品IDをISBNからLookUpする。
*
*/
class Utena_SevenAndY {
const server_enc = "euc-jp";
protected $cache_dir;
protected $cache_lite;
public function __construct($cache_dir=null){
require_once "Cache/Lite.php";
if($cache_dir!=null){
$this->cache_dir = $cache_dir;
}else{
$this->cache_dir = "/tmp/";
}
$this->cache_lite = new Cache_Lite( array("cacheDir"=>$this->cache_dir) );
}
protected function Scraping($isbn){
$url = "http://www.7andy.jp/books/search_result"
."?kword_in=&kword_out=&title=&info=&writer="
."&publisher=&code=$isbn&ctgy=&publish_sy="
."&publish_sm=&publish_ey=&publish_em=&x=28"
."&y=17&sort=0&disp=0&extract=0&oop=on";
$search = '/<input type="hidden" name="product_cd__1" value="([0-9]+)">/';
$html = $this->httpReuest($url);
preg_match($search, $html, $matches);
$accd = $matches[1];
//このほかに、タイトルや価格を抜きたい・・
// 7andYのHTMLはServletでできてるようだ
//tableレイアウトでClassやIDが無いのでちょっと面倒かも
return array($accd);
}
public function ItemLookUp($isbn){
$data = $this->Scraping($isbn);
$accd = $data[0];
$img_prefix = substr($accd, -1, 1);
$book->SmallImage->URL = "http://img.7andy.jp/bks/images/m{$img_prefix}/{$accd}.JPG";
$book->MediumImage->URL = "http://img.7andy.jp/bks/images/m{$img_prefix}/{$accd}.JPG";
$book->LargeImage->URL = "http://img.7andy.jp/bks/images/i{$img_prefix}/{$accd}.JPG";
return $book;
}
public function httpReuest($url){
$cache = $this->cache_lite->get($url);
if($cache){
return $cache;
}
require_once "HTTP/Client.php";
$cli = new HTTP_Client();
$header["UserAgent"] = "bookmacro.com/Spider (hatena_id=takuya_1st)";
$header["UA-CPU"] = "x86";
$header["Referer"] = "http://www.7andy.jp/books/search";
$cli->get($url);
$response = $cli->currentResponse();
$html = $response["body"];
$html = mb_convert_encoding($html, "utf-8", SevenAndY::server_enc);
$this->cache_lite->save($html, $url);
return $html;
}
}
/////////////////////
//test
////////////////////
//
$seven_y = new Utena_SevenAndY("/tmp/7andY/");
$ret = $seven_y->ItemLookUp("4592142039");
var_dump($ret);
セブンアンドワイの場合Bk1ほど確実に情報が抜ける訳じゃないので、Webサービスとして無理矢理利用するのはちょっと無理かも。
Bk1とセブンアンドワイとAmazonから画像を集めれば、自分のローカルコレクションが充実しました。
ただ、画像のサイズがAmazonとセブンアンドワイで大きく違うので、実際はImage関数でリサイズ処理している。