Skip to content

Commit

Permalink
Merge pull request #53 from tylergets/bugfix/channel_listings
Browse files Browse the repository at this point in the history
Add ability to list videos in a users channel
  • Loading branch information
alaouy authored Feb 10, 2017
2 parents 46dbb39 + e7670b5 commit b158049
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ $videoList = Youtube::searchVideos('Android');
// Search only videos in a given channel, return an array of PHP objects
$videoList = Youtube::searchChannelVideos('keyword', 'UCk1SpWNzOs4MYmr0uICEntg', 40);

// List videos in a given channel, return an array of PHP objects
$videoList = Youtube::listChannelVideos('UCk1SpWNzOs4MYmr0uICEntg', 40);

$results = Youtube::searchAdvanced(array( /* params */ ));

// Get channel data by channel name, return an STD PHP object
Expand Down
29 changes: 27 additions & 2 deletions src/Alaouy/Youtube/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ public function searchChannelVideos($q, $channelId, $maxResults = 10, $order = n
return $this->searchAdvanced($params, $pageInfo);
}

/**
* List videos in the channel
*
* @param string $channelId
* @param integer $maxResults
* @param string $order
* @param array $part
* @param $pageInfo
* @return array
*/
public function listChannelVideos($channelId, $maxResults = 10, $order = null, $part = ['id', 'snippet'], $pageInfo = false)
{
$params = array(
'type' => 'video',
'channelId' => $channelId,
'part' => implode(', ', $part),
'maxResults' => $maxResults,
);
if (!empty($order)) {
$params['order'] = $order;
}

return $this->searchAdvanced($params, $pageInfo);
}

/**
* Generic Search interface, use any parameters specified in
* the API reference
Expand All @@ -174,8 +199,8 @@ public function searchAdvanced($params, $pageInfo = false)
{
$API_URL = $this->getApi('search.list');

if (empty($params) || !isset($params['q'])) {
throw new \InvalidArgumentException('at least the Search query must be supplied');
if (empty($params) || (!isset($params['q']) && !isset($params['channelId']))) {
throw new \InvalidArgumentException('at least the Search query or Channel ID must be supplied');
}

$apiData = $this->api_get($API_URL, $params);
Expand Down
9 changes: 9 additions & 0 deletions tests/YoutubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ public function testSearchChannelVideos()
$this->assertEquals('youtube#video', $response[0]->id->kind);
}

public function testListChannelVideos()
{
$limit = rand(3, 10);
$response = $this->youtube->listChannelVideos('UCVHFbqXqoYvEWM1Ddxl0QDg', $limit);
$this->assertEquals($limit, count($response));
$this->assertEquals('youtube#searchResult', $response[0]->kind);
$this->assertEquals('youtube#video', $response[0]->id->kind);
}

public function testSearchAdvanced()
{
//TODO
Expand Down

0 comments on commit b158049

Please sign in to comment.