ニコニコ生放送公式ランキングのスクレイピング

## 概要

[PHPSimpleHtmlDomParser](http://simplehtmldom.sourceforge.net/ "PHPSimpleHtmlDomParser")を用いた[ニコニコ生放送公式ランキング](http://live.nicovideo.jp/ranking "ニコニコ生放送公式ランキング")のスクレイピング

## コード

```php:OfficialRankingScraper.class.php
<?php

require_once("./simple_html_dom.php");

class OfficialRankingScraper
{
    const OFFICIAL_RANKING_URL = "http://live.nicovideo.jp/ranking";
    private $user,$official;
    public function __construct(){

    }

    public function getUser(){
        $this->makeUser();
        return $this->user;
    }

    public function getOfficial(){
        $this->makeOfficial();
        return $this->official;
    }

    private function makeUser(){
        $html = file_get_html(self::OFFICIAL_RANKING_URL);
        $stream_array = array();
        foreach($html->find('div[class=user clearfix spriteIcon ranking_video]') as $i => $element) {
            $rank           = ($i + 1);
            $community      = $element->find('span[class=comName]',0);
            $community_name = $community->plaintext;
            $community_url  = $community->find('a',0)->href;
            $tmp            = explode('/',$community_url);
            $community_id   = $tmp[4];
            $user           = $element->find('span[class=name]',0);
            $user_name      = $user->plaintext;
            $user_url       = $user->find('a',0)->href;
            $viewer         = $element->find('li[class="audience spriteIcon"]',0)->plaintext;
            $comment        = $element->find('li[class="coment spriteIcon"]',0)->plaintext;
            $title          = $element->find('span[class="video_title"]',0)->plaintext;
            $thumbnail      = $element->find('div[class="info"]',0)->find('img',0)->src;
            $stream_url     = $element->find('div[class="info"]',0)->find('a',0)->href;
            $stream_url     = "http://live.nicovideo.jp/".$stream_url;
            $active         = $element->find('span[class="score"]',0)->plaintext;

            $stream_array[] = array(
                'rank'           => $rank,
                'community_id'   => $community_id,
                'community_url'  => $community_url,
                'community_name' => $community_name,
                'user_name'      => $user_name,
                'user_url'       => $user_url,
                'viewer'         => $viewer,
                'comment'        => $comment,
                'title'          => $title,
                'thumbnail'      => $thumbnail,
                'stream_url'     => $stream_url,
                'active'         => $active
            );
        }
        $this->user = $stream_array;
    }

    private function makeOfficial(){
        $html = file_get_html(self::OFFICIAL_RANKING_URL);
        $stream_array = array();
        foreach($html->find('ul[class="active clearfix"] li') as $i => $element) {
            $rank           = ($i + 1);
            $viewer         = $element->find('div[class="audience spriteIcon"]',0)->plaintext;
            $comment        = $element->find('div[class="coment spriteIcon"]',0)->plaintext;
            $title          = $element->find('p a',0)->plaintext;
            $thumbnail      = $element->find('div[class="rankNumber"]',0)->find('img',0)->src;
            $stream_url     = $element->find('div[class="rankNumber"]',0)->find('a',0)->href;
            $stream_url     = "http://live.nicovideo.jp/".$stream_url;
            $stream_id      = $this->makeStreamId($stream_url);

            $stream_array[] = array(
                'rank'           => $rank,
                'viewer'         => $viewer,
                'comment'        => $comment,
                'title'          => $title,
                'thumbnail'      => $thumbnail,
                'stream_url'     => $stream_url,
                'stream_id'      => $stream_id
            );
        }
        $this->official = $stream_array;
    }

    private function makeStreamId($stream_url){
        $stream_id = "";
        $slash     = explode("/", $stream_url);
        $hatena    = explode("?",$slash[4]);
        $stream_id = $hatena[0];
        return $stream_id;
    }


}

?>
```

## サンプルコード

```php:sample.php

<?php
require_once("OfficialRankingScraper.class.php");

$scraper = new OfficialRankingScraper();
print_r($scraper->getOfficial());
print_r($scraper->getUser());


?>

```

## 結果

```xml:result

Array
(
    [0] => Array
        (
            [rank] => 1
            [viewer] =>  									77,592 								
            [comment] =>  									64,730 								
            [title] => 【衆議院 国会生中継】 ～平成25年3月18日 予算委員会～
            [thumbnail] => http://nl.simg.jp/img/a2/4539.198de4.jpg
            [stream_url] => http://live.nicovideo.jp/watch/lv130208276?ref=rnk_active&zroute=ranking&provider_type=official_and_channel
            [stream_id] => lv130208276
        )

    [1] => Array
        (
            [rank] => 2
            [viewer] =>  									41,713 								
            [comment] =>  									17,362 								
            [title] => 向谷実のニコニコ生レコーディング配信Vol.03（1日目）
            [thumbnail] => http://nl.simg.jp/img/a55/164883.9cec79.jpg
            [stream_url] => http://live.nicovideo.jp/watch/lv129102517?ref=rnk_active&zroute=ranking&provider_type=official_and_channel
            [stream_id] => lv129102517
        )

    [2] => Array
        (
            [rank] => 3
            [viewer] =>  									9,189 								
            [comment] =>  									1,684 								
            [title] => 《暴力問題と助成金不正使用問題で会見》全日本柔道連盟理事会後記者会見 
            [thumbnail] => http://nl.simg.jp/img/a56/165855.ba88d9.jpg
            [stream_url] => http://live.nicovideo.jp/watch/lv130578293?ref=rnk_active&zroute=ranking&provider_type=official_and_channel
            [stream_id] => lv130578293
        )
     ...

)

Array
(
    [0] => Array
        (
            [rank] => 1
            [community_id] => co1406335
            [community_url] => http://com.nicovideo.jp/community/co1406335
            [community_name] => モロちゃんねる(´・ω・｀) 
            [user_name] => モロ 
            [user_url] => http://www.nicovideo.jp/user/4770536
            [viewer] => 6,855
            [comment] => 21,927
            [title] => JKが全力で雑談∠(　　∵　　)／ 
            [thumbnail] => http://icon.nimg.jp/community/s/140/co1406335.jpg?1362519204
            [stream_url] => http://live.nicovideo.jp/watch/lv130537400?&zroute=ranking&provider_type=community
            [active] => 138 
        )

    [1] => Array
        (
            [rank] => 2
            [community_id] => co1893195
            [community_url] => http://com.nicovideo.jp/community/co1893195
            [community_name] => 金バエリローデッド 
            [user_name] => まこと 
            [user_url] => http://www.nicovideo.jp/user/31151811
            [viewer] => 3,988
            [comment] => 2,250
            [title] => 大雨だからロケは中止して雑談 
            [thumbnail] => http://icon.nimg.jp/community/s/189/co1893195.jpg?1363460057
            [stream_url] => http://live.nicovideo.jp/watch/lv130593299?&zroute=ranking&provider_type=community
            [active] => 61 
        )

    [2] => Array
        (
            [rank] => 3
            [community_id] => co1705340
            [community_url] => http://com.nicovideo.jp/community/co1705340
            [community_name] => 哲学とは哲学であるm9・｀ω・)ﾋﾞｼｯ!!【命の章】 
            [user_name] => ティロ・フィナーレ加川　放送用 
            [user_url] => http://www.nicovideo.jp/user/28450199
            [viewer] => 12,717
            [comment] => 2,521
            [title] => 初めてのFX 
            [thumbnail] => http://icon.nimg.jp/community/s/170/co1705340.jpg?1363370871
            [stream_url] => http://live.nicovideo.jp/watch/lv130583371?&zroute=ranking&provider_type=community
            [active] => 60 
        )
   ...
)
```