Skip to content

Commit

Permalink
better handling for non existing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed Jul 17, 2020
1 parent b7a5c7d commit 68d5947
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
}
},
"scripts": {
"test": "phpunit"
"test": "phpunit tests"
}
}
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 6
paths:
- src
43 changes: 25 additions & 18 deletions src/YouTubeDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class YouTubeDownloader
{
protected $client;

/** @var string */
/** @var string|null */
protected $error;

function __construct()
Expand All @@ -26,9 +26,14 @@ public function getLastError()
return $this->error;
}

// accepts either raw HTML or url
// <script src="//s.ytimg.com/yts/jsbin/player-fr_FR-vflHVjlC5/base.js" name="player/base"></script>
public function getPlayerUrl($video_html)
/**
* Look for a player script URL. E.g:
* <script src="//s.ytimg.com/yts/jsbin/player-fr_FR-vflHVjlC5/base.js" name="player/base"></script>
*
* @param $video_html
* @return null|string
*/
public function getPlayerScriptUrl($video_html)
{
$player_url = null;

Expand Down Expand Up @@ -116,16 +121,18 @@ public function parsePlayerResponse($player_response, $js_code)
$parser = new Parser();

try {
$formats = [];
if (isset($player_response['streamingData']))
{
if (isset($player_response['streamingData']['formats']))
{
$formats = $player_response['streamingData']['formats'];
}

$formats = array();

if (isset($player_response['streamingData']) && isset($player_response['streamingData']['formats'])) {
$formats = $player_response['streamingData']['formats'];
}

$adaptiveFormats = array();

if (isset($player_response['streamingData']) && isset($player_response['streamingData']['adaptiveFormats'])) {
$adaptiveFormats = $player_response['streamingData']['adaptiveFormats'];
}

$adaptiveFormats = $player_response['streamingData']['adaptiveFormats'];

if (!is_array($formats)) {
$formats = array();
Expand All @@ -141,10 +148,10 @@ public function parsePlayerResponse($player_response, $js_code)
$return = array();

foreach ($formats_combined as $item) {
$cipher = '';
if(isset($item['cipher']) || isset($item['signatureCipher'])) {
$cipher = isset($item['cipher']) ? $item['cipher'] : $item['signatureCipher'];
}
$cipher = '';
if (isset($item['cipher']) || isset($item['signatureCipher'])) {
$cipher = isset($item['cipher']) ? $item['cipher'] : $item['signatureCipher'];
}
$itag = $item['itag'];

// some videos do not need to be decrypted!
Expand Down Expand Up @@ -204,7 +211,7 @@ public function getDownloadLinks($video_id, $selector = false)
$json = $this->getPlayerResponse($page_html);

// get player.js location that holds signature function
$url = $this->getPlayerUrl($page_html);
$url = $this->getPlayerScriptUrl($page_html);
$js = $this->getPlayerCode($url);

$result = $this->parsePlayerResponse($json, $js);
Expand Down

0 comments on commit 68d5947

Please sign in to comment.