ニコニコ動画のサムネイル情報を取得するPEAR::Services_Nicovideo
にインスパ(r
ということで,PEARパッケージにしてみました.
内容は大体パクリです.業界ではこういうのをインスパイアといいます.(たぶん)
ニコニコ動画のAPIは今後増えるかもしれないので,Nicovideoにして,thuminfoとか定数にしておいた.ちうかPEARパッケージ初めて作ったからちゃんとできてるのかどうかわからん.
今後増えたらFactoryとか作ってそっちから呼び出したほうが良いのかもしれないです..*1
(追記)とおもったら,オススメ動画とかflvも既に取得できるのか!?修正しなきゃー http://d.hatena.ne.jp/picas/20080202/1201955339
(さらに追記)しかもさらにちょっと調べてみたら,URLはwww.*だけじゃないっぽげ.こりゃあ定数も作り直さなきゃいけんかー>< http://tetetep.blog112.fc2.com/blog-entry-181.html
(さらにさらに追記)もうちょっとちゃんとしたものを作成中.オススメ動画情報にも対応中なので,もうすぐ0.2.0でリリースしまする.(ほかのは非公式っぽいのでまだ対応しない方向で...)
インストール
こんなかんじで.
% sudo pear install http://pear.strk.jp/Services_Nicovideo-0.1.0.tgz
使い方
<?php require_once "Services/Nicovideo.php"; $nico = new Services_Nicovideo(); try { var_dump($nico->getThumbInfo("sm2191960")); } catch (Exception $e) { echo $e->getMessage() ."\n"; }
結果
こんなん.
% php nico.php array(12) { ["video_id"]=> string(9) "sm2191960" ["title"]=> string(54) "英語の時間に流れた曲が面白かったので" ["description"]=> string(204) "『バイト先でジャンボと呼ばれて困っています』そんな事よりこの曲聞いてくれよ。卑猥だと思わないか。あの外人教師・・・やってくれるぜ・・・。" ["thumbnail_url"]=> string(43) "http://tn-skr.smilevideo.jp/smile?i=2191960" ["first_retrieve"]=> string(25) "2008-02-01T02:44:40+09:00" ["length"]=> string(4) "2:20" ["view_counter"]=> string(3) "268" ["comment_num"]=> string(2) "32" ["mylist_counter"]=> string(1) "9" ["last_res_body"]=> string(115) "オナニーズ★プレイ オナニーズ☆プレイ オナニーズ★プレイ これは伸びるwwww " ["watch_url"]=> string(39) "http://www.nicovideo.jp/watch/sm2191960" ["thumb_type"]=> string(5) "video" }
ソース
なんかrequestとパースと例外をどこでどうすればよいかよくわからなかったなあ.アドバイスくだしあ><
<?php class Services_Nicovideo { const URL = "http://www.nicovideo.jp/api/"; const THUMBINFO = "getthumbinfo/"; function __construct() { } function getThumbInfo ($smid) { $html = $this->_request(self::URL . self::THUMBINFO. $smid); if (!is_null($html)) { return $this->_parse($html); } else { return null; } } private function _request ($url) { require_once 'HTTP/Request.php'; $req = new HTTP_Request($url); if (!PEAR::isError($req->sendRequest())) { return $req->getResponseBody(); } else { throw new Exception (); } } private function _parse ($html) { $xml = simplexml_load_string($html); if ($xml->attributes()->status == "ok") { return (array)$xml->thumb; } else if ($xml->attributes()->status == "fail") { throw new Exception($xml->error->code . " " . $xml->error->description); } return null; } }
*1:覚えたてのアレ!