public function checkForApiResponseError(array $json) { $errorMessage = $this->_arrayReader->getAsString($json, 'error.message'); if ($errorMessage) { throw new RuntimeException(sprintf('Dailymotion responded with an error: %s', $errorMessage)); } }
private function _getInfo(tubepress_spi_http_oauth2_Oauth2ProviderInterface $provider, $dataPoint) { $clients = $this->_persistence->fetch(tubepress_api_options_Names::OAUTH2_CLIENT_DETAILS); $decodedClients = json_decode($clients, true); $providerName = $provider->getName(); $path = $providerName . '.' . $dataPoint; return $this->_arrayReader->getAsString($decodedClients, $path, null); }
private function _relativeQueryAsArray(array $json, $index, array $query, $default = array()) { $items = $this->_arrayReader->getAsArray($json, tubepress_youtube3_impl_ApiUtility::RESPONSE_ITEMS); if (!isset($items[$index])) { return $default; } $item = $items[$index]; return $this->_arrayReader->getAsArray($item, implode('.', $query), $default); }
private function _relativeQueryAsArray(array $json, $index, $query, $default = array()) { $items = $this->_arrayReader->getAsArray($json, 'list'); if (!isset($items[$index])) { return $default; } $item = $items[$index]; return $this->_arrayReader->getAsArray($item, $query, $default); }
private function _findMaxResults($minimumPage, $maximumPage) { $currentPage = floor(($maximumPage + $minimumPage) / 2); if ($this->_shouldLog) { $this->_logDebug(sprintf('Starting new iteration. Minimum page is <code>%d</code>, maximum page is <code>%d</code>, current page is <code>%d</code>', $minimumPage, $maximumPage, $currentPage)); } $url = $this->_urlFetched->getClone(); $query = $url->getQuery(); $query->set('fields', 'id'); $query->set('limit', 100); $query->set('page', $currentPage); $result = $this->_apiUtility->getDecodedApiResponse($url); $currentPageCount = count($this->_arrayReader->getAsArray($result, 'list')); $hasMore = $this->_arrayReader->getAsBoolean($result, 'has_more'); unset($url); unset($query); unset($result); if ($this->_shouldLog) { $this->_logDebug(sprintf('Page <code>%d</code> contains <code>%d</code> videos and <code>has_more</code> is <code>%s</code>', $currentPage, $currentPageCount, $hasMore ? 'true' : 'false')); } /* * There are 4 possible cases here: * * 1. We have over 10,000 results. * 2. Middle page is the last page in the result set. * 3. Middle page overshoots the last page. * 4. Middle page undershoots the last page. */ if ($hasMore) { //scenario 1 if (intval($currentPage) === 100) { if ($this->_shouldLog) { $this->_logDebug('There are over 10K videos in this result set.'); } // we've hit the max return 10000; } if ($this->_shouldLog) { $this->_logDebug('We have undershot the last page in the result set.'); } //scenario 4 return $this->_findMaxResults($currentPage + 1, $maximumPage); } // scenario 2 if ($currentPageCount > 0) { if ($this->_shouldLog) { $this->_logDebug('Looks like this was the last page in the result set.'); } return ($currentPage - 1) * 100 + $currentPageCount; } if ($this->_shouldLog) { $this->_logDebug('We have overshot the last page in the result set.'); } // scenario 3 return $this->_findMaxResults($minimumPage, $currentPage - 1); }
public function getValueMap() { if (!isset($this->_cache)) { $url = $this->_urlFactory->fromString($this->_urlAsString); $response = $this->_apiUtility->getDecodedApiResponse($url); $list = $this->_arrayReader->getAsArray($response, 'list'); $this->_cache = array(); foreach ($list as $entry) { if (!isset($entry[$this->_codeKey])) { continue; } $code = $entry[$this->_codeKey]; $displayName = $this->getDisplayNameFromCode($code, $entry); if ($displayName) { $this->_cache[$code] = "{$code} - {$displayName}"; } } ksort($this->_cache); $this->_cache = array_merge(array('none' => 'select ...'), $this->_cache); } return $this->_cache; }
private function _collectMetadata() { $idQueriesToTest = array(sprintf('%s.%s.%s', tubepress_youtube3_impl_ApiUtility::RESOURCE_PLITEM_SNIPPET, tubepress_youtube3_impl_ApiUtility::RESOURCE_PLITEM_SNIPPET_RESOURCE_ID, tubepress_youtube3_impl_ApiUtility::RESOURCE_PLITEM_SNIPPET_RESOURCE_ID_VIDEO_ID), sprintf('%s.%s', tubepress_youtube3_impl_ApiUtility::RESOURCE_SEARCH_ID, tubepress_youtube3_impl_ApiUtility::RESOURCE_SEARCH_ID_VIDEO_ID), tubepress_youtube3_impl_ApiUtility::RESOURCE_ID); $items = $this->_arrayReader->getAsArray($this->_feedAsArray, tubepress_youtube3_impl_ApiUtility::RESPONSE_ITEMS, array()); $ids = array(); $selectedQuery = null; for ($index = 0; $index < count($items); $index++) { if ($this->getReasonUnableToUseItemAtIndex($index) !== null) { if ($this->_logger->isEnabled()) { $reason = $this->getReasonUnableToUseItemAtIndex($index); $this->_logDebug(sprintf('Skipping video at index <code>%d</code>: <code>%s</code>', $index, $reason)); } $this->_skippedVideoCount++; unset($this->_feedAsArray[tubepress_youtube3_impl_ApiUtility::RESPONSE_ITEMS][$index]); continue; } $item = $items[$index]; if ($selectedQuery === null) { foreach ($idQueriesToTest as $query) { if ($this->_arrayReader->getAsString($item, $query) !== '') { $selectedQuery = $query; break; } } } if ($selectedQuery === null) { throw new RuntimeException('Unable to determine query to get video IDs'); } $id = $this->_arrayReader->getAsString($item, $selectedQuery); if ($id == '') { throw new RuntimeException('Unable to determine ID for a video in the result.'); } $ids[] = $id; } if ($this->_logger->isEnabled()) { $this->_logDebug(sprintf('Making API call to collect metadata for <code>%d</code> video(s): <code>[ %s ]</code>', count($ids), implode(', ', $ids))); } $url = $this->_urlFactory->fromString(tubepress_youtube3_impl_ApiUtility::YOUTUBE_API_URL); $url->addPath(tubepress_youtube3_impl_ApiUtility::PATH_VIDEOS); $partsToRequest = array(tubepress_youtube3_impl_ApiUtility::PART_ID, tubepress_youtube3_impl_ApiUtility::PART_SNIPPET, tubepress_youtube3_impl_ApiUtility::PART_VIDEO_STATISTICS, tubepress_youtube3_impl_ApiUtility::PART_VIDEO_CONTENT_DETAILS); $fieldsToRequest = array(tubepress_youtube3_impl_ApiUtility::RESOURCE_ID, tubepress_youtube3_impl_ApiUtility::RESOURCE_SNIPPET, tubepress_youtube3_impl_ApiUtility::RESOURCE_VIDEO_STATS, tubepress_youtube3_impl_ApiUtility::RESOURCE_VIDEO_CONTENT_DETAILS); $fields = sprintf('%s,%s(%s)', tubepress_youtube3_impl_ApiUtility::RESPONSE_ETAG, tubepress_youtube3_impl_ApiUtility::RESPONSE_ITEMS, implode(',', $fieldsToRequest)); /* * author //snippet.channelId and snippet.channelTitle * category //snippet.categoryId * comments count //statistics.commentCount * date uploaded //snippet.publishedAt * description //snippet.description * disklikes count //statistics.disklikeCount * favorites count //statistics.favoriteCount * id //id * length //contentDetails.duration * likes count //statistics.likeCount * tags //snippet.tags * title //snippet.title * url //https://youtu.be/<id> * view count //statistics.viewCount */ $url->getQuery()->set(tubepress_youtube3_impl_ApiUtility::QUERY_VIDEOS_ID, implode(',', $ids))->set(tubepress_youtube3_impl_ApiUtility::QUERY_PART, implode(',', $partsToRequest))->set(tubepress_youtube3_impl_ApiUtility::QUERY_FIELDS, $fields); return $this->_apiUtility->getDecodedApiResponse($url); }