ニコニコ動画をIDから検索するAPI with PHP

ニコニコ動画のIDから動画を検索します。
ちょっとした小ネタとして。
サンプルはこちら
http://moeten.info/maidcafe/?m=nicoid2title&nicoid=sm1231592
ソースはこちら
キャッシュやhtmlsqlを使用しています。

<?php //にこにこIDからタイトルとかをゲット
    $mail = 'めるあど';
    $pwd  = 'ぱすわーど';
    //検索ID
    //$id = "sm1231592";
    $id = "sm" . intval ( str_replace( "sm", "" , $_GET['nicoid'] ) );
    //キャッシュ
    $result = include_once 'Cache/Lite.php';
    if( $result === false ){
        echo 'PEAR Cache_lite ライブラリが呼び出せるよう設定されていません。';
        exit();
    }
    $options = array(
                     'cacheDir' => 'mycache/',
                     'lifeTime' => $cache_period,
                     );
    $cache_lite = new Cache_Lite( $options );
    $cache_id = md5($id);
    $cached_data = $cache_lite->get($cache_id);
    if( $cached_data ){
        $XML = $cached_data;
    }else{
        include( "htmlsql.class.php" ) ;
        include( "snoopy.class.php" ) ;
        //ニコニコへログインとか
        require_once 'HTTP/Client.php';
        $conf = array('mail' => $mail, 'password' => $pwd);
        preg_match('/(sm\d+)$/', @$id, $matches);
        $video_id = $matches[1];
        $client = new HTTP_Client();
        $client->setDefaultHeader('Keep-Alive', 4);
        $client->post('https://secure.nicovideo.jp/secure/login?site=niconico', $conf);
        $res = $client->currentResponse();
        $client->get("http://www.nicovideo.jp/watch/{$video_id}");
        $client->get("http://www.nicovideo.jp/api/getflv?v={$video_id}");
        $res = $client->currentResponse();
        parse_str($res['body'], $q);
        //タグ検索
        $client->get("http://www.nicovideo.jp/tag/{$id}");
        $res = $client->currentResponse();
        parse_str($res['body'], $q);
        $body = $res['body'];
        //文字処理(正規表現できないヘタレですorz
        $wsql = new htmlsql();
        if (!$wsql->connect('string', $body)){
            print 'Error while connecting: ' . $wsql->error;
            exit;
        }
        if (!$wsql->query('SELECT * FROM p WHERE $class="TXT12" ')){
            print "Query error: " . $wsql->error;
            exit;
        }
        foreach($wsql->fetch_array() as $row){
            if( $row['class'] == "TXT12" ){
                $body = $row['text'];
            break;
            }
        }
        //もちっと処理
        $wsql->connect('string', $body);
        $wsql->query('SELECT * FROM a');
        foreach($wsql->fetch_array() as $row){
            $title =$row['text'] ;
            $url = $row['href'] ;
        }
        $body = strip_tags( $body );
        $img = "http://tn-skr.smilevideo.jp/smile?i=" . intval( str_replace( "sm" , "" , $id ) );
        $url   = h( $url );
        $title = h( $title );
        $body  = h( $body );
        $imt = h( $img );
        //XMLで配信
        $XML = <<<EOD
<result>
<nicoid>{$id}</nicoid>
<url>{$url}</url>
<title>{$title}</title>
<img>{$img}</img>
<body>{$body}</body>
</result>
EOD;
        $cache_lite->save( $XML , $cache_id);
    }
    header ("Content-Type: text/xml; charset=UTF-8");
    echo $XML;
    exit;
?>