htmlsqlが会社ではやって?いるのかな

会社でhtmlsqlのサンプルが回ってきたので
脊髄反射。
会社ではやっているのは実際のところiPhone用のページだろうけど
コピペで始めるプログラム。僕は本当に下品だなぁ!

説明

適当にsnoopyとhtmlsqlは設置しておいてください。
アニメをみれるページができます。
一次集約元のページはyoutubeアニ速を利用してます。
結構速いしいいサイトなんだwww
一次集約元がeuc-jpで作ってあるのかな?なのでソースもeuc-jpでつくってめんどくさい文字コードまわりのいざこざを回避してください。
もともと重いページが元なのでめちゃくちゃ遅いよ!

//index.php

<?php

    include_once("../snoopy.class.php");
    include_once("../htmlsql.class.php");
 
 
class PNScraper {
 
    private $_wsql;
 
    public function connect($src) {
        $this->_wsql = new htmlsql();
        $this->_wsql->set_referer('http://youtubeanisoku1.blog106.fc2.com/');
        $this->_wsql->set_user_agent('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
        if (!$this->_wsql->connect('url', $src)){
            throw new Exception('Error while connecting: ' . $this->_wsql->error);
        }
    }
 
    public function query($sql) {
        if ($this->_wsql->query($sql)) {
            return $this->_wsql;
        }
        throw new Exception('Query error: ' . $this->_wsql->error);
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Anisokuすく例</title>
<link rel="apple-touch-icon" href="./apple-touch-icon.png" />
<meta name="viewport" 
      content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<meta http-equiv="Content-Type" content="text/html; charset=euc_jp">

</head>

<body>
    <div class="toolbar">
        <h1 id="pageTitle"></h1>
        <a id="backButton" class="button" href="#"></a>
    </div>
<?php

$ps = new PNScraper();

try {
    $ps->connect('http://youtubeanisoku1.blog106.fc2.com/');

    print '<ul title="Home" selected="true">';
    print "\n";
    
    //更新情報URLの取得
    $kousin = $ps->query('SELECT * FROM a WHERE preg_match("/更新状況/",$text) and substr($href,0,50) =="http://youtubeanisoku1.blog106.fc2.com/blog-entry-"');

    $breakcount = 0;
    foreach($kousin->fetch_array() as $row)
    {
        //hrefをたどってそこの....
        print '<li class="group"><h2>'.$row['text'].'の更新リスト</h2></li>';
        $ps->connect($row['href']);
        $wawa = $ps->query('SELECT * FROM a WHERE preg_match("/話/",$text) and substr($href,0,50) =="http://youtubeanisoku1.blog106.fc2.com/blog-entry-"');
        foreach($wawa->fetch_array() as $row2)
        {
            $wtext2 ='<a href="'.'index2.php?url='.$row2['href'].'" >'.$row2['text'].'</a>';            
            print '<li>'.$wtext2.'</li>';
        }
        if($breakcount++ > 3){break;}//一応ここはずすといっぱいとれるが重くなります
    }
    
    print '</ul>';
    print "\n";
    
} catch (Exception $e) {
    echo $e->getMessage();
}
?>
</body>
</html>

//index2.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<title>Anisokuすく例</title>
<link rel="apple-touch-icon" href="./apple-touch-icon.png" />
</head>

<body>
    <div class="toolbar">
        <h1 id="pageTitle"></h1>
        <a id="backButton" class="button" href="index.php"></a>
    </div>
<?php
    include_once("../snoopy.class.php");
    include_once("../htmlsql.class.php");

class PNScraper {
 
    private $_wsql;
 
    public function connect($src) {
        $this->_wsql = new htmlsql();
        $this->_wsql->set_referer('http://youtubeanisoku1.blog106.fc2.com/');       
        $this->_wsql->set_user_agent('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
        if (!$this->_wsql->connect('url', $src)){
            throw new Exception('Error while connecting: ' . $this->_wsql->error);
        }
    }
 
    public function query($sql) {
        if ($this->_wsql->query($sql)) {
            return $this->_wsql;
        }
        throw new Exception('Query error: ' . $this->_wsql->error);
    }
}


/**
 * ページタイトルを取得する関数
 * http://creazy.net/2008/05/php_get_page_title_sample.htmlからパクッた。thx!
 * 404のWarningがうざいので消す@とEUC-JP化したお
 */
function getPageTitle( $url ) {
    $html = @file_get_contents($url); 
    $html = mb_convert_encoding($html, "EUC-JP", "auto" ); 
    if ( preg_match( "/<title>(.*?)<\/title>/i", $html, $matches) ) { 
        return $matches[1];
    } else {
        return false;
    }
}

$wcnt =0;
$ps = new PNScraper();
try {
    $url = $_GET['url'];
    $ps->connect($url);

    print '<ul title="veoh/SM一覧(逆順)" selected="true">';
    print "\n";
    print '<li class="group"><a href="index.php">戻る</a></li>';
        
    //veohとSMのURLを取得
    $vlist = $ps->query('SELECT * FROM a WHERE preg_match("/say-move.net\/comeplay\.php/", $href) or preg_match("/ww\.veoh\.com\/videos/", $href)');
    $getlimit = 0;
    // videoURLのリスト表示
    foreach(array_reverse($vlist->fetch_array()) as $row)
    {
	$te = getPageTitle( $row['href'] );
	if($te){
	        $wtext = '<a href="' . $row['href'] . '" target="blank">' . $te. '</a>';
	        print '<li>'.$wtext.'</li>';
	        print "\n";
        }
//        if($getlimit++ > 10){break;}
    }

    print '</ul>';
    print "\n";
        
}
catch (Exception $e)
{
    echo $e->getMessage();
}

?>
</body>
</html>

2009/06/08 19:49高速化しますた

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<style type="text/css" media="screen">@import "../iui/iui.css";</style>
<title>Anisokuすく例</title>
<link rel="apple-touch-icon" href="./apple-touch-icon.png" />
</head>

<body>
    <div class="toolbar">
        <h1 id="pageTitle"></h1>
        <a id="backButton" class="button" href="index.php"></a>
    </div>
<?php
    include_once("../snoopy.class.php");
    include_once("../htmlsql.class.php");

class PNScraper {
 
    private $_wsql;
 
    public function connect($src) {
        $this->_wsql = new htmlsql();
        $this->_wsql->set_referer('http://youtubeanisoku1.blog106.fc2.com/');       
        $this->_wsql->set_user_agent('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
        if (!$this->_wsql->connect('url', $src)){
            throw new Exception('Error while connecting: ' . $this->_wsql->error);
        }
    }
 
    public function query($sql) {
        if ($this->_wsql->query($sql)) {
            return $this->_wsql;
        }
        throw new Exception('Query error: ' . $this->_wsql->error);
    }
}

/**(ぱくった。http://techblog.ecstudio.jp/tech-tips/php-multi.html)
 * xamppでcurl使う方法 http://phpspot.org/blog/archives/2008/02/xamppcurlopenss.html 
 * 複数URLを同時に取得する
 *
 * @param array $url_list URLの配列
 * @param int $timeout タイムアウト秒数 0だと無制限
 * @return array 取得したソースコードの配列
 */
function fetch_multi_url($url_list,$timeout=0) {
    $mh = curl_multi_init();
 
    foreach ($url_list as $i => $url) {
        $conn[$i] = curl_init($url);
        curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
        curl_setopt($conn[$i],CURLOPT_FAILONERROR,1);
        curl_setopt($conn[$i],CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($conn[$i],CURLOPT_MAXREDIRS,3);
       
        //SSL証明書を無視
        curl_setopt($conn[$i],CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($conn[$i],CURLOPT_SSL_VERIFYHOST,false);
        
        //タイムアウト
        if ($timeout){
            curl_setopt($conn[$i],CURLOPT_TIMEOUT,$timeout);
        }

        curl_multi_add_handle($mh,$conn[$i]);
    }
   
    //URLを取得
    //すべて取得するまでループ
    $active = null;
    do {
        $mrc = curl_multi_exec($mh,$active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
   
    while ($active and $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh,$active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
   
    if ($mrc != CURLM_OK) {
//        echo '読み込みエラーが発生しました:'.$mrc;
    }
   
    //ソースコードを取得
    $res = array();
    foreach ($url_list as $i => $url) {
        if (($err = curl_error($conn[$i])) == '') {
            $res[$url] = curl_multi_getcontent($conn[$i]);
//ここちょっと変えた。連想配列にした。
        } else {
//            echo '取得に失敗しました:'.$url_list[$i].'<br />';
        }
        curl_multi_remove_handle($mh,$conn[$i]);
        curl_close($conn[$i]);
    }
    curl_multi_close($mh);
   
    return $res;
}

$time = time();

$ps = new PNScraper();
try {
    $url = $_GET['url'];
    $ps->connect($url);

//		echo '--<br />time:'.(time() - $time).' sec';

    print '<ul title="veoh/SM一覧(逆順)" selected="true">';
    print "\n";
//    print '<li class="group"><a href="index.php">戻る</a></li><br/><br/>';
    print '<a href="javascript:history.back();">戻る</a><br/><br/>';
//print '<form><input type="button" value="戻る" onclick="history.back()" /></form><br/><br/>';
        
    //veohとSMのURLを取得
    $vlist = $ps->query('SELECT * FROM a WHERE preg_match("/say-move.net\/comeplay\.php/", $href) or preg_match("/ww\.veoh\.com\/videos/", $href)');
    $getlimit = 0;
    // videoURLのリスト表示
		$hrefArray = array();
    foreach(array_reverse($vlist->fetch_array()) as $row)
    {
        array_push($hrefArray,$row['href']);
//      if($getlimit++ > 10){break;}
    }
    print '<li><font color="grey"> htmlsql time '.(time() - $time).'</font></li>';			
    //cURLになげとく
    $res = fetch_multi_url($hrefArray);
		foreach ($res as $k => $mre) 
		{ 
			$title = preg_match( "/<title>(.*?)<\/title>/i", $mre, $matches);
			$wtext = '<a href="' . $k . '" target="blank">' .mb_convert_encoding($matches[1],"EUC-JP","auto") . '</a>';
      print '<li>'.$wtext.'</li>';
      $myi++;
    }
    print '<li><font color="grey"> fetchover:'.(time() - $time).' sec</font></li>';
    print '</ul>';
    print "\n";
        
}
catch (Exception $e)
{
    echo $e->getMessage();
}

?>
</body>
</html>

一個ずつアクセスしていくとやっぱり遅いので
curlというのが平行アクセス可能になるらしい。
つかってみた。
1.xamppでcurlはデフォルトインストールだと有効になってないです。
http://phpspot.org/blog/archives/2008/02/xamppcurlopenss.html
2.curlはすごいらしいっす
ここをコピペした。
http://techblog.ecstudio.jp/tech-tips/php-multi.html
以上です。


表示までの時間が3分の1ぐらいになりました。
じょうずにやけましたー


タイトルをとってくる正規表現がうまくいかないことあるよ!とかありますけど、とりあえず気にしません。


さて、仕事しないと!