Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Niconico Comment Getter
<?php
define('NICONICO_MAILADDRESS', '<アカウントアドレス>');
define('NICONICO_PASSWORD' , '<アカウントパスワード>');
$id = @$argv[1];
if(!$id) exit('Usage (sm|lv)[\d]+'.PHP_EOL);
if(strpos($id, 'sm') === 0) getVideoComment($id);
if(strpos($id, 'lv') === 0) getLiveComment($id);
if(strpos($id, 'co') === 0) getLiveComment($id);
function getVideoComment($id) {
// niconico内でアカウントログイン状態でHTTPリクエストを送れるNicoHttpRequestを作成
$nhr = new NicoHttpRequest(NICONICO_MAILADDRESS, NICONICO_PASSWORD);
// getflvにアクセスしてコメントサーバの情報を取得
$getflv_url = sprintf(Define::NICOVIDEO_GETFLV_URLBASE, $id);
$getflv = $nhr->get($getflv_url);
parse_str($getflv, $getflv_array);
// コメントサーバに接続してコメントを取得
$msg_url = sprintf(Define::NICOVIDEO_MSG_URLBASE, $getflv_array['ms'], $getflv_array['thread_id']);
$msg = $nhr->get($msg_url, true);
// コメントをループして出力
foreach($msg->chat as $comment) {
echo $comment['no'] . ' - ' . $comment .PHP_EOL;
}
}
function getLiveComment($id) {
// niconico内でアカウントログイン状態でHTTPリクエストを送れるNicoHttpRequestを作成
$nhr = new NicoHttpRequest(NICONICO_MAILADDRESS, NICONICO_PASSWORD);
// getplayerstatusにアクセスしてコメントサーバの情報を取得
$getplayerstatus_url = sprintf(Define::NICOLIVE_GETPLAYERSTATUS_URLBASE, $id);
$getplayerstatus = $nhr->get($getplayerstatus_url, true);
// コメントサーバに接続してコメントを取得
$msg_socket = fsockopen((string)$getplayerstatus->ms->addr, (string)$getplayerstatus->ms->port);
stream_set_blocking($msg_socket, 0);
fputs($msg_socket, sprintf('<thread thread="%s" version="20061206" res_from="-100" />'."\0", (string)$getplayerstatus->ms->thread));
// コメントをループして出力
$buffer = "";
while(1) {
$buffer .= fread($msg_socket, 1024);
$commentbuffer = explode("\0", $buffer, 2);
if(isset($commentbuffer[1])) {
$buffer = $commentbuffer[1];
$comment = new SimpleXMLElement($commentbuffer[0]);
echo $comment['no'] . ' - ' . $comment .PHP_EOL;
}
}
}
class Define {
const NICOVIDEO_GETFLV_URLBASE = 'http://flapi.nicovideo.jp/api/getflv/%s';
const NICOVIDEO_MSG_URLBASE = '%sthread?version=20061206&thread=%s';
const NICOLIVE_GETPLAYERSTATUS_URLBASE = 'http://watch.live.nicovideo.jp/api/getplayerstatus?v=%s';
}
class NicoHttpRequest {
const NICONICO_LOGIN_URL = 'https://secure.nicovideo.jp/secure/login?site=niconico';
var $cookies;
function __construct($mail, $password) {
$data = array('mail'=> $mail, 'password'=> $password);
$data = http_build_query($data);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => implode("\r\n", array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($data)
)),
'content' => $data
)
));
file_get_contents(self::NICONICO_LOGIN_URL, false, $context);
$cookies = array();
foreach ($http_response_header as $r) {
if (strpos($r, 'Set-Cookie') === false) continue;
$c = explode(' ', $r);
$c = str_replace(';', '', $c[1]);
$cc = explode('=', $c);
if($cc[1] == 'deleted') continue;
$cookies[] = $c;
}
$this->cookies = $cookies;
}
function get($url, $return_xml = false) {
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => implode("\r\n", array(
'Cookie: ' . implode('; ', $this->cookies)
))
)
));
$content = file_get_contents($url, false, $context);
return $return_xml?simplexml_load_string($content):$content;
}
}

thank for share , this help me a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment