Skip to content

Commit

Permalink
Add Support Shorts Video ID Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
devsarfo committed Jan 21, 2024
1 parent 641c6fe commit 6ddf716
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"DevSarfo\\Youtube\\Tests\\YoutubeTest::testConstructorFail":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testConstructorFail2":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSetApiKey":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testInvalidApiKey":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetCategories":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetCommentThreadsByVideoId":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetVideoInfo":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetLocalizedVideoInfo":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetVideoInfoMultiple":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPopularVideos":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearch":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearchVideos":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearchChannelVideos":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testListChannelVideos":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearchAdvanced":5,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetRelatedVideos":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetChannelByName":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetChannelById":6,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistsByChannelId":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistById":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistByMultipleIds":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistItemsByPlaylistId":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLFull":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLShort":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromEmbedURL":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLException with data set #0":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLException with data set #1":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdException":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetActivitiesByChannelId":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetActivitiesByChannelIdException":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetChannelFromURL":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testNotFoundAPICall":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testNotFoundAPICall2":4,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetShortsVideoInfo":6},"times":{"DevSarfo\\Youtube\\Tests\\YoutubeTest::testConstructorFail":0.003,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testConstructorFail2":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSetApiKey":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testInvalidApiKey":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetCategories":0.242,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetCommentThreadsByVideoId":0.301,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetVideoInfo":0.218,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetLocalizedVideoInfo":0.23,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetVideoInfoMultiple":0.217,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPopularVideos":0.295,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearch":0.423,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearchVideos":0.424,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearchChannelVideos":0.405,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testListChannelVideos":0.389,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testSearchAdvanced":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetRelatedVideos":0.394,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetChannelByName":0.215,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetChannelById":0.212,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistsByChannelId":0.354,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistById":0.649,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistByMultipleIds":0.664,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetPlaylistItemsByPlaylistId":0.416,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLFull":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLShort":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromEmbedURL":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLException with data set #0":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdFromURLException with data set #1":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testParseVIdException":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetActivitiesByChannelId":0.412,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetActivitiesByChannelIdException":0,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetChannelFromURL":2.84,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testNotFoundAPICall":0.216,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testNotFoundAPICall2":0.318,"DevSarfo\\Youtube\\Tests\\YoutubeTest::testGetShortsVideoInfo":0.219}}
69 changes: 37 additions & 32 deletions src/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public function getCategories($regionCode = 'US', $part = ['snippet'])
* @return array
* @throws \Exception
*/
public function getCommentThreadsByVideoId($videoId = null, $maxResults = 20, $order = null, $part = ['id', 'replies', 'snippet'], $pageInfo = false) {
public function getCommentThreadsByVideoId($videoId = null, $maxResults = 20, $order = null, $part = ['id', 'replies', 'snippet'], $pageInfo = false)
{

return $this->getCommentThreads(null, null, $videoId, $maxResults, $order, $part, $pageInfo);
}
Expand Down Expand Up @@ -211,7 +212,8 @@ public function getVideoInfo($vId, $part = ['id', 'snippet', 'contentDetails', '
* @throws \Exception
*/

public function getLocalizedVideoInfo($vId, $language, $part = ['id', 'snippet', 'contentDetails', 'player', 'statistics', 'status']) {
public function getLocalizedVideoInfo($vId, $language, $part = ['id', 'snippet', 'contentDetails', 'player', 'statistics', 'status'])
{

$API_URL = $this->getApi('videos.list');
$params = [
Expand Down Expand Up @@ -418,29 +420,29 @@ public function getChannelByName($username, $optionalParams = [], $part = ['id',
return $this->decodeSingle($apiData);
}

/**
* @param $username
* @param $maxResults
* @param $part
* @return false|\StdClass
* @throws \Exception
*/
public function searchChannelByName($username, $maxResults = 1, $part = ['id', 'snippet'])
{
$params = [
'q' => $username,
'part' => implode(',', $part),
'type' => 'channel',
'maxResults' => $maxResults,
];
/**
* @param $username
* @param $maxResults
* @param $part
* @return false|\StdClass
* @throws \Exception
*/
public function searchChannelByName($username, $maxResults = 1, $part = ['id', 'snippet'])
{
$params = [
'q' => $username,
'part' => implode(',', $part),
'type' => 'channel',
'maxResults' => $maxResults,
];

$search = $this->searchAdvanced($params);
$search = $this->searchAdvanced($params);

if (!empty($search[0]->snippet->channelId)) {
$channelId = $search[0]->snippet->channelId;
return $this->getChannelById($channelId);
}
}
if (!empty($search[0]->snippet->channelId)) {
$channelId = $search[0]->snippet->channelId;
return $this->getChannelById($channelId);
}
}

/**
* @param $id
Expand Down Expand Up @@ -505,7 +507,7 @@ public function getPlaylistById($id, $part = ['id', 'snippet', 'status'])
{
$API_URL = $this->getApi('playlists.list');
$params = [
'id' => is_array($id)? implode(',', $id) : $id,
'id' => is_array($id) ? implode(',', $id) : $id,
'part' => implode(',', $part),
];
$apiData = $this->api_get($API_URL, $params);
Expand Down Expand Up @@ -613,6 +615,9 @@ public function getRelatedVideos($videoId, $maxResults = 5, $part = ['id', 'snip
*/
public static function parseVidFromURL($youtube_url)
{
preg_match('/(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/', $youtube_url, $matches);
if (isset($matches[3]) && !empty($matches[3])) return $matches[3];

if (strpos($youtube_url, 'youtube.com')) {
if (strpos($youtube_url, 'embed')) {
$path = static::_parse_url_path($youtube_url);
Expand Down Expand Up @@ -661,13 +666,13 @@ public function getChannelFromURL($youtube_url)
$channel = $this->searchChannelByName($username);
} else {
foreach ($this->youtube_reserved_urls as $r) {
if (preg_match('/'.$r.'/', $path)) {
if (preg_match('/' . $r . '/', $path)) {
throw new \Exception('The supplied URL does not look like a Youtube Channel URL');
}
}

$username = $segments[1];
$channel = $this->searchChannelByName($username);
$username = $segments[1];
$channel = $this->searchChannelByName($username);
}

return $channel;
Expand Down Expand Up @@ -705,15 +710,15 @@ public function decodeSingle(&$apiData)

throw new \Exception($msg);
} else {
if(isset($resObj->items)){
if (isset($resObj->items)) {
$itemsArray = $resObj->items;
if (!is_array($itemsArray) || count($itemsArray) == 0) {
return false;
} else {
return $itemsArray[0];
}
}
return false;
return false;
}
}

Expand All @@ -735,8 +740,8 @@ public function decodeMultiple(&$apiData)

throw new \Exception($msg);
} else {
if(isset($resObj->items)) {

if (isset($resObj->items)) {
$itemsArray = $resObj->items;
if (!is_array($itemsArray) || count($itemsArray) == 0) {
return false;
Expand Down Expand Up @@ -783,7 +788,7 @@ public function decodeList(&$apiData)
$this->page_info['nextPageToken'] = $resObj->nextPageToken;
}

if(isset($resObj->items)) {
if (isset($resObj->items)) {
$itemsArray = $resObj->items;
if (!is_array($itemsArray) || count($itemsArray) == 0) {
return false;
Expand Down

0 comments on commit 6ddf716

Please sign in to comment.