Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- 0.0.6
- - fixed scraping after changes on youtube website
- 0.0.5
- - read YOUTUBE_QUALITY setting from /conf/config
- 0.0.4
- - added fmt_map (quality map) handling to support 1080p, 720p, 480p, 360p, 270p based on user setting
- 0.0.3
- - user private videos supported (270p quality only)
- 0.0.2
- - support for videos which require age confirmation (270p quality only)
- 0.0.1
- - 720p quality videos / restricted videos playback supported
- todo:
- - direct web autentification to support all videos in >270p quality (if available) and shared private videos of other users
- */
- session_id('youtube');
- session_start();
- if (count($_SESSION) > 1024) {
- $_SESSION = array();
- }
- if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
- postreq();
- } else {
- if ($_GET['id']) {
- getvideo();
- }
- else {
- videolist();
- }
- }
- function videolist() {
- $head = "Host: gdata.youtube.com" . "\r\n" . "X-GData-Device:" . $_SERVER[HTTP_X_GDATA_DEVICE] . "\r\n";
- $headers = apache_request_headers();
- if ($headers['Authorization']) $head = $head . "Authorization:" . $headers['Authorization'] . "\r\n";
- if ($_SERVER['HTTP_GDATA_VERSION']) $head = $head . "GData-Version:" . $_SERVER['HTTP_GDATA_VERSION'] . "\r\n";
- $http = array(
- 'method'=>"GET",
- 'header'=> $head
- );
- $opts = array('http'=> $http);
- $url = "http://74.125.43.118" . str_replace("&", '&', $_SERVER[REQUEST_URI]);
- $context = stream_context_create($opts);
- $xmlfeed = file_get_contents($url, false, $context);
- $reader = new XMLReader();
- $reader->XML($xmlfeed);
- while ($reader->read()) {
- if (($reader->nodeType == XMLReader::ELEMENT) && ($reader->localName == 'entry')) {
- do {
- $reader->read();
- if (($reader->localName == "content") && ($reader->getAttribute('yt:format') == 3)) {
- $link = $reader->getAttribute('url');
- }
- if ($reader->localName == "videoid") {
- $reader->read();
- if ($reader->nodeType == XMLReader::TEXT) {
- $id = $reader->value;
- }
- }
- } while (!(($reader->nodeType == XMLReader::END_ELEMENT) && ($reader->localName == 'entry')));
- $_SESSION['id_' . str_replace("-","_",$id)] = $link;
- }
- }
- $xmlfeed = str_replace("yt:format='5'", "yt:format='3'", $xmlfeed);
- echo $xmlfeed;
- }
- function getvideo() {
- $url = "http://www.youtube.com/watch?v=" . $_GET['id'];
- $html = file_get_contents($url);
- if(preg_match('/&video_id=(.*?)&/', $html, $id) && preg_match('/&t=(.*?)&/', $html, $t) && preg_match('/&fmt_map=(.*?)&/', $html, $fmt_map)) {
- $var_id = $id[1];
- $var_t = $t[1];
- $var_fmt_map = $fmt_map[1];
- $quality = "270P";
- if (file_exists('/conf/config')) {
- $config = file_get_contents('/conf/config');
- if(preg_match('/YOUTUBE_QUALITY=\'(.+)\'/', $config, $config_quality)) {
- $quality = $config_quality[1];
- }
- }
- $quality_map = array('1080P' => 37, '720P' => 22, '480P' => 35, '360P' => 34, '270P' => 18);
- $quality_map_values = array_values($quality_map);
- foreach (split(",",urldecode($var_fmt_map)) as $key) {
- list($current_list[],$rest) = split("/",$key,2);
- }
- $index = array_search($quality_map[$quality], $quality_map_values);
- while (!in_array($quality_map_values[$index], $current_list) && ($index < (count($quality_map_values) - 1))) {
- $index++;
- }
- $quality = $quality_map_values[$index];
- $url = "http://www.youtube.com/get_video?video_id=".$var_id."&t=".$var_t."&fmt=".$quality;
- $opts = array(
- 'http' => array(
- 'method' => "GET",
- 'max_redirects' => '0'
- )
- );
- $context = stream_context_create($opts);
- $html = @file_get_contents($url, false, $context);
- foreach ($http_response_header as $header) {
- if(preg_match('/Location: (.+)/',$header,$link)) {
- echo "<?xml?><entry><media:group><media:content url='" . str_replace('&', "&", $link[1]) . "' yt:format='3'/></media:group></entry>";
- }
- }
- } else {
- echo "<?xml?><entry><media:group><media:content url='" . str_replace('&', "&", ($_SESSION['id_' . str_replace("-","_",$_GET['id'])])) . "' yt:format='3'/></media:group></entry>";
- }
- }
- function postreq() {
- $postdata = trim(file_get_contents('php://input'));
- $headers = apache_request_headers();
- $opts = array(
- 'http' => array(
- 'method' => "POST",
- 'header' => "X-GData-Key: " . $_SERVER[HTTP_X_GDATA_KEY] . "\r\n" . "X-Gdata-Client: " . $_SERVER[HTTP_X_GDATA_CLIENT] . "\r\n" . "Authorization: " . $headers[Authorization] . "\r\n" . "GData-Version: 2" . "\r\n" . "Content-Type: application/atom+xml" . "\r\n",
- 'content' => $postdata
- )
- );
- $fp = fsockopen("74.125.43.118", 80, $errno, $errstr, 30);
- if ($fp) {
- $out = "POST " . $_SERVER[REQUEST_URI] . " HTTP/1.0\r\n";
- $out .= "Host: gdata.youtube.com\r\n";
- $out .= "Content-Length: " . strlen($opts["http"]["content"]) ."\r\n";
- $out .= "Connection: Close\r\n";
- $out .= $opts["http"]["header"];
- $out .= "\r\n";
- $out .= $opts["http"]["content"];
- fwrite($fp, $out);
- $buff = "";
- while (!feof($fp)) {
- $buff .= fgets($fp, 4096);
- }
- fclose($fp);
- list($header, $body) = split("\r\n\r\n", $buff);
- header("HTTP/1.0 201 Created");
- echo $body;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement