Skip to content

Commit

Permalink
Merge pull request #27 from dreenot/dreenotChanges
Browse files Browse the repository at this point in the history
Subscriptions function is now getting the right amount of results
  • Loading branch information
alchemyguy authored Apr 14, 2021
2 parents 3bf8051 + fe16cf5 commit 844ca04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
39 changes: 16 additions & 23 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 21 additions & 15 deletions src/ChannelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ 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 {

$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);
Expand All @@ -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;
Expand Down Expand Up @@ -238,4 +244,4 @@ public function removeSubscription($token, $subscriptionId, $params = []) {

}

}
}

0 comments on commit 844ca04

Please sign in to comment.