Skip to content

Instantly share code, notes, and snippets.

@tsuwatch
Created November 25, 2012 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsuwatch/4144354 to your computer and use it in GitHub Desktop.
Save tsuwatch/4144354 to your computer and use it in GitHub Desktop.
ニコニコ動画の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