Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ニコニコ動画のFLVファイルをDL
<?php
$data = array('mail' => 'your mail address', 'password' => 'your 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('https://secure.nicovideo.jp/secure/login?site=niconico', 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;
}
$context= stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => implode("\r\n", array(
'Cookie: ' . implode('; ', $cookies)
))
)
));
$res = file_get_contents("http://flapi.nicovideo.jp/api/getflv/{$argv[1]}", false, $context);
$r = explode('&', $res);
$result = array();
foreach ($r as $r_) {
preg_match("/(.*?)=(.*)/", $r_, $matches);
$result[$matches[1]] = $matches[2];
}
file_get_contents("http://www.nicovideo.jp/watch/{$argv[1]}", false, $context);
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;
}
$context= stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => implode("\r\n", array(
'Cookie: ' . implode('; ', $cookies)
))
)
));
$file = file_get_contents(urldecode($result['url']), false, $context);
$fp = fopen("{$argv[1]}.flv", "w");
fwrite($fp, $file);
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.