Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request all videos with automatic nextToken #12

Open
petersontubini opened this issue Jul 7, 2014 · 0 comments
Open

Request all videos with automatic nextToken #12

petersontubini opened this issue Jul 7, 2014 · 0 comments

Comments

@petersontubini
Copy link

Hi,
I've created a new function to retrieve all videos with automatic token!

Check the code below:

private $next = array();
private $playItemsArray = array();

public function getAllPlaylistItemsByPlaylistId($playlistId,$maxResults=50,$pageToken=""){
    $API_URL = $this->getApi('playlistItems.list');
    $params = array(
        'playlistId' => $playlistId,
        'part' => 'id, snippet, contentDetails, status',
        'maxResults' => $maxResults,
        'pageToken' => $pageToken
    );
    $apiData = $this->api_get($API_URL, $params);
    return $this->decodePlayList($apiData,$playlistId);
}


        public function decodePlayList(&$apiData,$id){
    $resObj = json_decode($apiData);
    if(isset($resObj->error)){
        $msg = "Error ".$resObj->error->code." ".$resObj->error->message;
        if(isset($resObj->error->errors[0])){
            $msg .= " : " . $resObj->error->errors[0]->reason;
        }
        throw new \Exception($msg);
    }else{
        $itemsArray = $resObj->items;
        if(!is_array($itemsArray) || count($itemsArray) == 0){
            return FALSE;
        }else{
        if (isset($resObj->nextPageToken)) {
        array_unshift ($this->next, $resObj->nextPageToken);
        $this->playItemsArray = array_merge($this->playItemsArray,$itemsArray);
        self::getAllPlaylistItemsByPlaylistId($id,50,$this->next[0]);

        } else {
        $this->playItemsArray = array_merge($this->playItemsArray,$itemsArray);
            }
        return $this->playItemsArray;
        }
    }
}

I've checked and this function just consume 60 Google Developer points (with 300 videos) and take around 35 seconds to run !

If you guys have another better way to do it tell or post it here!

This is very powerful when you want to get all videos 🎯

Best Regards,

Peterson Tubini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant