/** * This method takes the YouniversityPlaylist.id and a YoutubePlaylist object and inserts all of the associated images into the DB. * If an invalid (non-numeric or numeric less than one) YouniversityPlaylist.id is provided, returns false; otherwise, returns the number of successful inserts. * @param integer $youniversityPlaylistId * @param YoutubePlaylist $youtubePlaylistObject * @return false|integer * @see YoutubePlaylist::getAllThumbnails() * @see Model::query() */ public function saveImagesFromYoutubePlaylistObject($youniversityPlaylistId, YoutubePlaylist $youtubePlaylistObject) { $return = false; $youniversityPlaylistId = (int) $youniversityPlaylistId; if ($youniversityPlaylistId > 0) { // Validate "Youniversity Playlist ID" Parameter $return = 0; foreach ($youtubePlaylistObject->getAllThumbnails(true) as $currentThumbnailName => $currentThumbnailData) { // Loop through Thumbnails if (!empty($currentThumbnailData->url)) { // Verify Thumbnail Has URL $query = $this->query(sprintf('INSERT IGNORE INTO `youniversity_playlist_images` VALUES (NULL, %d, "%s", "%s", %d, %d) ON DUPLICATE KEY UPDATE `url` = VALUES(`url`), `width` = VALUES(`width`), `height` = VALUES(`height`)', $youniversityPlaylistId, strtoupper($currentThumbnailName), $currentThumbnailData->url, $currentThumbnailData->width, $currentThumbnailData->height)); if (is_array($query) || true === $query) { // Check Query Execution $return++; } // End of Check Query Execution } // End of Verify Thumbnail Has URL } // End of Loop through Thumbnails } // End of Validate "Youniversity Playlist ID" Parameter return $return; }
/** * This method retrieves the list of playlists associated with a channel from the YouTube Data API. * @param string * @return boolean|array * @see YouniversityShell::$guzzleClient * @see YouniversityShell::$youtubeApiPlaylistSuffix * @see YouniversityShell::$youtubeApiKey * @see Guzzle\Http\Client::get() * @see Guzzle\Http\Message\Request::send() * @see Guzzle\Http\Message\Response::getStatusCode() * @see Guzzle\Http\Message\Response::getBody() * @see YouniversityShell::$executionStatistics */ protected function getChannelPlaylistsFromYoutubeApi($channelId) { $return = false; $channelId = trim((string) $channelId); if (!empty($channelId) && 24 == strlen($channelId)) { $apiResponse = $this->guzzleClient->get(sprintf($this->youtubeApiPlaylistSuffix, $channelId, $this->youtubeApiKey))->send(); if (in_array($apiResponse->getStatusCode(), array(200))) { // Check YouTube Data API Response $apiResponseBody = json_decode($apiResponse->getBody(true)); if ($apiResponseBody instanceof \stdClass) { // Verify API Response Body Decoding if (!empty($apiResponseBody->items) && is_array($apiResponseBody->items) && count($apiResponseBody->items)) { // Check for Playlist Items $return = array(); foreach ($apiResponseBody->items as $currentPlaylist) { // Loop through Channel Playlists $this->executionStatistics['Playlists Processed']++; $currentPlaylistObject = YoutubePlaylist::getPlaylistFromStdClass($currentPlaylist); if ($currentPlaylistObject instanceof YoutubePlaylist) { // Verify Current Playlist $return[] = $currentPlaylistObject; } else { // Middle of Verify Current Playlist $this->executionStatistics['Playlists Invalid']++; } // End of Verify Current Playlist } // End of Loop through Channel Playlists } // End of Check for Playlist Items } else { // Middle of Verify API Response Body Decoding $this->out(sprintf('<error>Non-JSON YouTube API Response (Channel: %s): %s</error>', $channelId, print_r($apiResponse->getBody(true), true)), 1, Shell::VERBOSE); } // End of Verify API Response Body Decoding } else { // Middle of Check YouTube Data API Response $this->out(sprintf('<error>Unable to retrieve Channel (%s) contents from YouTube Data API. Response status code: %d</error>', $channelId, $apiResponse->getStatusCode()), 1, Shell::VERBOSE); } // End of Check YouTube Data API Response } else { $this->out(sprintf('<error>Invalid YouTube Channel ID: %s</error>', $channelId), 1, Shell::VERBOSE); } return $return; }
/** * This method takes an array of DB results (preferably, from a method on this model object) and converts them into YoutubePlaylist objects. * @param array $youniversityPlaylistDatabaseResults * @return array * @see YoutubePlaylist::getPlaylistFromArray() */ public function convertDatabaseResultsToPlaylistObjects(array $youniversityPlaylistDatabaseResults) { $return = array(); foreach ($youniversityPlaylistDatabaseResults as $currentDatabaseResult) { // Loop through Passed Database Results Array if (!empty($currentDatabaseResult['YouniversityPlaylist']) && is_array($currentDatabaseResult['YouniversityPlaylist']) && count($currentDatabaseResult['YouniversityPlaylist'])) { // Validate Current Row Data $currentPlaylistObject = YoutubePlaylist::getPlaylistFromArray($currentDatabaseResult); if ($currentPlaylistObject instanceof YoutubePlaylist) { // Verify Playlist Object Instantiation $return[] = $currentPlaylistObject; } // End of Verify Playlist Object Instantiation } // End of Validate Current Row Data } // End of Loop through Passed Database Results Array return $return; }