ページ

2010年6月3日木曜日

[PHP][cURL] ニコニコ動画のコメントを抽出する

このエントリーをはてなブックマークに追加

・デバックを取りながらやるとわかりやすいと思います。
・ソースはコピーペースとでメモ帳などに貼り付けてみたほうがみやすいです。
・proxy接続の場合はCURLOPT_PROXY等でGoogleに投げてみてください。
・ソースコードの悪い部分があったら指摘していただけると助かります。

1.ログインする
2.http://ext.nicovideo.jp/api/getflv/smxxxxに接続する(接続に成功した場合は$outputにthread_id= ~ というStringが格納される)
3.http://ext.nicovideo.jp/api/getflv/smxxxxからthread_idとmsのurlを抜き出す
4.message serverにpostするStringを作成する
5.message serverに4で作成しStringをpostする。

===========================================

<?php

define("res", -1 * 50); // 取り出すコメントの量(50)

$VideoId = "smxxxxxx"; // 動画ID
$login_url = "https://secure.nicovideo.jp/secure/login?site=niconico"; // ニコニコログインURL
$getflv_url = "http://ext.nicovideo.jp/api/getflv/".$VideoId; // GetFlvURL
$param = Array(
"mail"=>"your mail address",
"password"=>"your password"
); // mail, password
$cookiefile = "cookie.txt"; // save cookie

//=========================================================
// cURLで接続するクラス
//=========================================================
class Connect{

// cURLリソース
private $ch;
// parameters
private $param = Array();
// option
private $option = Array();
// ThreadId
private $ThreadId;
// MessageServer
public $ms;
// MessageXML;
private $MXML;

//===========================================
// コンストラクタ
// 引数
// $param : mail address と passwordの保持
//===========================================
public function __construct($param = Array()){
$this->param = $param;
}

//===========================================
// cURLリソースを開放する
//===========================================
public function Release(){
curl_close($this->ch);
}

//=====================================================
// Settings()の設定
// 引数
// 第一引数 : SSLを有効にするかどうか
// 第二引数 : CookieFileを保存するかどうか
// 第三引数 : CookieFileを読み込むかどうか
// 第四引数 : Postするかどうか(1=LOGIN, 2=Get Comment)
// 第五引数 : GetFlvに接続するかどうか
//=====================================================
private function SettingOption($SSL, $CookieFile, $CookieColl, $Post, $GetFlv){
$this->option['SSL'] = $SSL;
$this->option['CookieFile'] = $CookieFile;
$this->option['CookieColl'] = $CookieColl;
$this->option['Post'] = $Post;
$this->option['GetFlv'] = $GetFlv;
}


//===========================================
// URLに接続
// 引数
// 第一引数 : URLを指定してCURLのリソースを作成する
//===========================================
public function ConnectURL($url){
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
}


//===========================================
// 接続設定 HTTP / COOKIE等
//===========================================
public function Settings($SSL = TRUE, $CookieFile = NULL, $CookieColl = FALSE, $Post = FALSE, $GetFlv = FALSE){

$this->SettingOption($SSL, $CookieFile, $CookieColl, $Post, $GetFlv);

if(!$this->option['SSL']){
// SSL認証をしない。
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}

if($this->option['CookieColl']){
// COOKIEを呼び出す。
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->option['CookieFile']);
}

if($this->option['CookieFile']){
// COOKIEを保存する。
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->option['CookieFile']);
}

// USER_AGENTを要求する
curl_setopt($this->ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// 接続する時間(0で永遠に接続)
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 0);
// TRUEで出力を文字列とする
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);

if($this->option['Post']){
// TRUEでPOST経由で送る
curl_setopt($this->ch, CURLOPT_POST, TRUE);
// LOGIN
if($this->option['Post'] == 1){
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->param);
}
// Get Message
else{
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->MXML);
}
}
}

//===========================================
// 出力
// 引数
// 第一引数 : コメント数 -1~-1000
//===========================================
public function OutPut($res = 0){

$output = curl_exec($this->ch);

// Get thread_id and messageserver
if($this->option['GetFlv']){

// thread_idを抜き出す
preg_match("/[0-9]+/", $output, $ThreadId);
$this->ThreadId = $ThreadId[0];
// メッセージサーバーのURLを抜き出す
preg_match("/(ms=)([^&]*)/", $output, $ms);
$this->ms = $ms[2];
// encodeされたURLをdecodeする
$this->ms = urldecode($this->ms);
// 通常コメントを取得する時にPOSTするxml
$this->MXML = "<thread thread=\"{$this->ThreadId}\" version=\"20061206\" res_from=\"{$this->res}\" >";

}


// OutPut XML Value
if($this->option['Post'] == 2){
// XMLをArray()に変換する
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser,$output,$vals);
xml_parser_free($xml_parser);

// resで指定した分だけコメントを取り出す
for($i = 4 ; $i < (-1 * $res) + 4; $i++){ print_r($vals[$i]['value']."<br>\n"); } } } } $cURL = new Connect($param); $cURL->ConnectURL($login_url);
$cURL->Settings(FALSE, $cookiefile, FALSE, 1);
$cURL->OutPut();
$cURL->Release();

$cURL->ConnectURL($getflv_url);
$cURL->Settings(TRUE, $cookiefile, TRUE, FALSE, TRUE);
$cURL->OutPut(res);
$cURL->Release();

$cURL->ConnectURL($cURL->ms);
$cURL->Settings(TRUE, FALSE, FALSE, 2);
$cURL->OutPut(res);
$cURL->Release();

$cURL = NULL;

?>

===========================================
以上のソースコードはBSDライセンスです。
Copyright (C) 2010 raisana

・参考サイト

ニコニコ動画API: getflvとコメントの取得
Getflv - ニコニコ動画(API) - Cathode Music

0 件のコメント:

コメントを投稿