diff --git a/src/DownloadOptions.php b/src/DownloadOptions.php index c38afde..ca077d2 100644 --- a/src/DownloadOptions.php +++ b/src/DownloadOptions.php @@ -55,6 +55,9 @@ public function getAudioFormats() }); } + /** + * @return StreamFormat[] + */ public function getCombinedFormats() { return Utils::arrayFilterReset($this->getAllFormats(), function ($format) { diff --git a/src/Responses/GetVideoInfo.php b/src/Responses/GetVideoInfo.php deleted file mode 100644 index 4a2f9ae..0000000 --- a/src/Responses/GetVideoInfo.php +++ /dev/null @@ -1,28 +0,0 @@ -getResponseBody()); - } - - public function isError() - { - return Utils::arrayGet($this->getJson(), 'errorcode') !== null; - } - - /** - * About same as `player_response` that appears on video pages. - * @return array - */ - public function getPlayerResponse() - { - $playerResponse = Utils::arrayGet($this->getJson(), 'player_response'); - return json_decode($playerResponse, true); - } -} \ No newline at end of file diff --git a/src/YouTubeDownloader.php b/src/YouTubeDownloader.php index 2304c41..0e3a6af 100644 --- a/src/YouTubeDownloader.php +++ b/src/YouTubeDownloader.php @@ -7,7 +7,6 @@ use YouTube\Exception\YouTubeException; use YouTube\Models\VideoDetails; use YouTube\Models\YouTubeConfigData; -use YouTube\Responses\GetVideoInfo; use YouTube\Responses\PlayerApiResponse; use YouTube\Responses\VideoPlayerJs; use YouTube\Responses\WatchVideoPage; @@ -15,27 +14,31 @@ class YouTubeDownloader { - protected $client; + protected Browser $client; function __construct() { $this->client = new Browser(); } - public function getBrowser() + public function getBrowser(): Browser { return $this->client; } /** - * @param $query + * @param string $query * @return array */ - public function getSearchSuggestions($query) + public function getSearchSuggestions(string $query): array { $query = rawurlencode($query); - $response = $this->client->get('http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q=' . $query); + $response = $this->client->get('http://suggestqueries.google.com/complete/search', [ + 'client' => 'firefox', + 'ds' => 'yt', + 'q' => $query + ]); $json = json_decode($response->body, true); if (is_array($json) && count($json) >= 2) { @@ -45,24 +48,7 @@ public function getSearchSuggestions($query) return []; } - // No longer working... - public function getVideoInfo($video_id) - { - $video_id = Utils::extractVideoId($video_id); - - $response = $this->client->get("https://www.youtube.com/get_video_info?" . http_build_query([ - 'html5' => 1, - 'video_id' => $video_id, - 'eurl' => 'https://youtube.googleapis.com/v/' . $video_id, - 'el' => 'embedded', // or detailpage. default: embedded, will fail if video is not embeddable - 'c' => 'TVHTML5', - 'cver' => '6.20180913' - ])); - - return new GetVideoInfo($response); - } - - public function getPage($url) + public function getPage(string $url): WatchVideoPage { $video_id = Utils::extractVideoId($url); @@ -78,24 +64,8 @@ public function getPage($url) return new WatchVideoPage($response); } - /** - * To parse the links for the video we need two things: - * contents of `player_response` JSON object that appears on video pages - * contents of player.js script file that's included inside video pages - * - * @param array $player_response - * @param VideoPlayerJs $player - * @return array - */ - public function parseLinksFromPlayerResponse($player_response, VideoPlayerJs $player) + protected function getPlayerApiResponse(string $video_id, YouTubeConfigData $configData): PlayerApiResponse { - return []; - } - - protected function getPlayerApiResponse($video_id, YouTubeConfigData $configData) - { - // $api_key = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'; - // exact params matter, because otherwise "slow" download links will be returned $response = $this->client->post("https://www.youtube.com/youtubei/v1/player?key=" . $configData->getApiKey(), json_encode([ "context" => [ @@ -124,13 +94,17 @@ protected function getPlayerApiResponse($video_id, YouTubeConfigData $configData } /** - * @param $video_id - * @param array $options + * + * TOOD: rename to DownloadResult?? + * + * @param string $video_id + * @param $options * @return DownloadOptions * @throws TooManyRequestsException + * @throws VideoNotFoundException * @throws YouTubeException */ - public function getDownloadLinks($video_id, $options = array()) + public function getDownloadLinks(string $video_id, $options = null): DownloadOptions { $page = $this->getPage($video_id); @@ -144,6 +118,7 @@ public function getDownloadLinks($video_id, $options = array()) throw new VideoNotFoundException(); } + // a giant JSON object holding useful data $youtube_config_data = $page->getYouTubeConfigData(); // the most reliable way of fetching all download links no matter what