diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 86246b3..a0c2ccc 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -2,42 +2,35 @@ ## Description -Describe your changes in detail. +The function **subscriptionByChannelId** didn't work properly. It was getting always the first 50 results and the user could not get more or less. The parameter 'maxResults' didn't do anything. -## Motivation and context +I reactivated the function **parseSubscriptions** and fixed it to get the desired amount of results instead of all of them. -Why is this change required? What problem does it solve? +To avoid confuision, the parameters is passed now as **totalResults** instead of **maxResults** since the last is the Google Api's parameter for each page, which can only be set up to 50 -If it fixes an open issue, please link to the issue here (if you write `fixes #num` -or `closes #num`, the issue will be automatically closed when the pull is accepted.) +The Read Me was updated -## How has this been tested? +## Motivation and context + +User was not able to get results off the first page -Please describe in detail how you tested your changes. +## How has this been tested? -Include details of your testing environment, and the tests you ran to -see how your change affects other areas of the code, etc. +I listed my own channel subscriptions ## Screenshots (if appropriate) ## Types of changes -What types of changes does your code introduce? Put an `x` in all the boxes that apply: - [ ] Bug fix (non-breaking change which fixes an issue) -- [ ] New feature (non-breaking change which adds functionality) +- [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist: -Go over all the following points, and put an `x` in all the boxes that apply. - -Please, please, please, don't send your pull request until all of the boxes are ticked. Once your pull request is created, it will trigger a build on our [continuous integration](http://www.phptherightway.com/#continuous-integration) server to make sure your [tests and code style pass](https://help.github.com/articles/about-required-status-checks/). - -- [ ] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document. -- [ ] My pull request addresses exactly one patch/feature. -- [ ] I have created a branch for this patch/feature. -- [ ] Each individual commit in the pull request is meaningful. -- [ ] I have added tests to cover my changes. -- [ ] If my change requires a change to the documentation, I have updated it accordingly. - -If you're unsure about any of these, don't hesitate to ask. We're here to help! +- [X] I have read the **[CONTRIBUTING](CONTRIBUTING.md)** document. +- [X] My pull request addresses exactly one patch/feature. +- [X] I have created a branch for this patch/feature. +- [X] Each individual commit in the pull request is meaningful. +- [X] I have added tests to cover my changes. +- [X] If my change requires a change to the documentation, I have updated it accordingly. \ No newline at end of file diff --git a/README.md b/README.md index 48c6660..89c8af7 100644 --- a/README.md +++ b/README.md @@ -227,8 +227,10 @@ $channelDetails = $channelServiceObject->getChannelDetails($authToken); ```php /* -* $params array('channel_id'=>'', -* 'max_results'= 10) +* $params array('channelId'=>'', 'totalResults'= 10) +* totalResults is different of maxResults from Google Api. +* totalResults = the amount of results you want +* maxResults = max of results PER PAGE. We don't need this parameter here since it will loop until it gets all the results you want. */ $channelServiceObject = new ChannelService; $channelDetails = $channelServiceObject->subscriptionByChannelId($params); diff --git a/src/ChannelService.php b/src/ChannelService.php index ffd21e9..54f4b6b 100644 --- a/src/ChannelService.php +++ b/src/ChannelService.php @@ -110,9 +110,9 @@ public function updateChannelBrandingSettings($googleToken, $properties, $part, } /** - * [parseSubscriptions modified for backUp] + * [parseSubscriptions working] * @param [type] $part - * @return [type] $params array('channelId'= '', "maxResults"='' ) + * @return [type] $params array('channelId'= '', 'totalResults'= '') */ public function subscriptionByChannelId($params, $part = 'snippet') { try { @@ -120,7 +120,7 @@ public function subscriptionByChannelId($params, $part = 'snippet') { $params = array_filter($params); $service = new \Google_Service_YouTube($this->client); - return $service->subscriptions->listSubscriptions($part, $params); + return $this->parseSubscriptions($params); } catch (\Google_Service_Exception $e) { throw new Exception($e->getMessage(), 1); @@ -134,31 +134,37 @@ public function subscriptionByChannelId($params, $part = 'snippet') { } /** - * [parseSubscriptions modified for backUp] + * [parseSubscriptions working] * @param [type] $channelId [description] * @return [type] [description] */ - public function parseSubscriptions($channelId, $token) { - + public function parseSubscriptions($params) { + $channelId = $params['channelId']; + $totalResults = $params['totalResults']; + $maxResultsPerPage = 50; + if($totalResults < 1){$totalResults = 0;} + $maxPages = ($totalResults - ($totalResults % $maxResultsPerPage))/$maxResultsPerPage + 1; + $i = 0; try { - if (!$this->setAccessToken($token)) { - return false; - } - $service = new \Google_Service_YouTube($this->client); - $part = "snippet"; - $params = array('channelId' => $channelId, "maxResults" => 20); + $part = 'snippet'; + $params = array('channelId' => $channelId, 'maxResults' => $maxResultsPerPage); $nextPageToken = 1; $subscriptions = []; - while ($nextPageToken) { + while ($nextPageToken and $i < $maxPages) { + if($i == $maxPages-1){ + $params['maxResults'] = $totalResults % $maxResultsPerPage + 2; + } + $response = $service->subscriptions->listSubscriptions($part, $params); $response = json_decode(json_encode($response), true); $sub = array_column($response['items'], 'snippet'); $sub2 = array_column($sub, 'resourceId'); $subscriptions = array_merge($subscriptions, $sub2); + $nextPageToken = isset($response['nextPageToken']) ? $response['nextPageToken'] : false; - $nextPageToken = isset($response["nextPageToken"]) ? $response['nextPageToken'] : false; $params['pageToken'] = $nextPageToken; + $i++; } return $subscriptions; @@ -238,4 +244,4 @@ public function removeSubscription($token, $subscriptionId, $params = []) { } -} +} \ No newline at end of file