Advertisement
Guest User

Untitled

a guest
May 17th, 2010
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. 0.0.6
  6. - fixed scraping after changes on youtube website
  7.  
  8. 0.0.5
  9. - read YOUTUBE_QUALITY setting from /conf/config
  10.  
  11. 0.0.4
  12. - added fmt_map (quality map) handling to support 1080p, 720p, 480p, 360p, 270p based on user setting
  13.  
  14. 0.0.3
  15. - user private videos supported (270p quality only)
  16.  
  17. 0.0.2
  18. - support for videos which require age confirmation (270p quality only)
  19.  
  20. 0.0.1
  21. - 720p quality videos / restricted videos playback supported
  22.  
  23. todo:
  24. - direct web autentification to support all videos in >270p quality (if available) and shared private videos of other users
  25.  
  26. */
  27.  
  28. session_id('youtube');
  29. session_start();
  30.  
  31. if (count($_SESSION) > 1024) {
  32. $_SESSION = array();
  33. }
  34.  
  35. if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
  36. postreq();
  37. } else {
  38. if ($_GET['id']) {
  39. getvideo();
  40. }
  41. else {
  42. videolist();
  43. }
  44. }
  45.  
  46. function videolist() {
  47.  
  48.  
  49. $head = "Host: gdata.youtube.com" . "\r\n" . "X-GData-Device:" . $_SERVER[HTTP_X_GDATA_DEVICE] . "\r\n";
  50.  
  51. $headers = apache_request_headers();
  52.  
  53. if ($headers['Authorization']) $head = $head . "Authorization:" . $headers['Authorization'] . "\r\n";
  54. if ($_SERVER['HTTP_GDATA_VERSION']) $head = $head . "GData-Version:" . $_SERVER['HTTP_GDATA_VERSION'] . "\r\n";
  55.  
  56. $http = array(
  57. 'method'=>"GET",
  58. 'header'=> $head
  59. );
  60.  
  61. $opts = array('http'=> $http);
  62. $url = "http://74.125.43.118" . str_replace("&amp;", '&', $_SERVER[REQUEST_URI]);
  63. $context = stream_context_create($opts);
  64. $xmlfeed = file_get_contents($url, false, $context);
  65. $reader = new XMLReader();
  66. $reader->XML($xmlfeed);
  67. while ($reader->read()) {
  68. if (($reader->nodeType == XMLReader::ELEMENT) && ($reader->localName == 'entry')) {
  69. do {
  70. $reader->read();
  71. if (($reader->localName == "content") && ($reader->getAttribute('yt:format') == 3)) {
  72. $link = $reader->getAttribute('url');
  73. }
  74. if ($reader->localName == "videoid") {
  75. $reader->read();
  76. if ($reader->nodeType == XMLReader::TEXT) {
  77. $id = $reader->value;
  78. }
  79. }
  80. } while (!(($reader->nodeType == XMLReader::END_ELEMENT) && ($reader->localName == 'entry')));
  81. $_SESSION['id_' . str_replace("-","_",$id)] = $link;
  82. }
  83. }
  84. $xmlfeed = str_replace("yt:format='5'", "yt:format='3'", $xmlfeed);
  85. echo $xmlfeed;
  86. }
  87.  
  88. function getvideo() {
  89.  
  90. $url = "http://www.youtube.com/watch?v=" . $_GET['id'];
  91. $html = file_get_contents($url);
  92.  
  93. if(preg_match('/&video_id=(.*?)&/', $html, $id) && preg_match('/&t=(.*?)&/', $html, $t) && preg_match('/&fmt_map=(.*?)&/', $html, $fmt_map)) {
  94. $var_id = $id[1];
  95. $var_t = $t[1];
  96. $var_fmt_map = $fmt_map[1];
  97.  
  98. $quality = "270P";
  99.  
  100. if (file_exists('/conf/config')) {
  101. $config = file_get_contents('/conf/config');
  102. if(preg_match('/YOUTUBE_QUALITY=\'(.+)\'/', $config, $config_quality)) {
  103. $quality = $config_quality[1];
  104. }
  105. }
  106.  
  107. $quality_map = array('1080P' => 37, '720P' => 22, '480P' => 35, '360P' => 34, '270P' => 18);
  108. $quality_map_values = array_values($quality_map);
  109.  
  110. foreach (split(",",urldecode($var_fmt_map)) as $key) {
  111. list($current_list[],$rest) = split("/",$key,2);
  112. }
  113.  
  114. $index = array_search($quality_map[$quality], $quality_map_values);
  115. while (!in_array($quality_map_values[$index], $current_list) && ($index < (count($quality_map_values) - 1))) {
  116. $index++;
  117. }
  118.  
  119. $quality = $quality_map_values[$index];
  120.  
  121. $url = "http://www.youtube.com/get_video?video_id=".$var_id."&t=".$var_t."&fmt=".$quality;
  122.  
  123. $opts = array(
  124. 'http' => array(
  125. 'method' => "GET",
  126. 'max_redirects' => '0'
  127. )
  128. );
  129.  
  130. $context = stream_context_create($opts);
  131. $html = @file_get_contents($url, false, $context);
  132. foreach ($http_response_header as $header) {
  133. if(preg_match('/Location: (.+)/',$header,$link)) {
  134. echo "<?xml?><entry><media:group><media:content url='" . str_replace('&', "&amp;", $link[1]) . "' yt:format='3'/></media:group></entry>";
  135. }
  136. }
  137. } else {
  138. echo "<?xml?><entry><media:group><media:content url='" . str_replace('&', "&amp;", ($_SESSION['id_' . str_replace("-","_",$_GET['id'])])) . "' yt:format='3'/></media:group></entry>";
  139. }
  140. }
  141.  
  142. function postreq() {
  143.  
  144. $postdata = trim(file_get_contents('php://input'));
  145.  
  146. $headers = apache_request_headers();
  147.  
  148. $opts = array(
  149. 'http' => array(
  150. 'method' => "POST",
  151. '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",
  152. 'content' => $postdata
  153. )
  154. );
  155.  
  156. $fp = fsockopen("74.125.43.118", 80, $errno, $errstr, 30);
  157. if ($fp) {
  158. $out = "POST " . $_SERVER[REQUEST_URI] . " HTTP/1.0\r\n";
  159. $out .= "Host: gdata.youtube.com\r\n";
  160. $out .= "Content-Length: " . strlen($opts["http"]["content"]) ."\r\n";
  161. $out .= "Connection: Close\r\n";
  162. $out .= $opts["http"]["header"];
  163. $out .= "\r\n";
  164. $out .= $opts["http"]["content"];
  165. fwrite($fp, $out);
  166. $buff = "";
  167. while (!feof($fp)) {
  168. $buff .= fgets($fp, 4096);
  169. }
  170. fclose($fp);
  171. list($header, $body) = split("\r\n\r\n", $buff);
  172. header("HTTP/1.0 201 Created");
  173. echo $body;
  174. }
  175. }
  176.  
  177. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement