Skip to content
Snippets Groups Projects
Commit ebb1d1e1 authored by Johannes Kreiner's avatar Johannes Kreiner
Browse files

[TASK] Adjust api call and add maxResults field to plugin settings

parent 60d973ec
No related branches found
No related tags found
No related merge requests found
......@@ -46,6 +46,10 @@ class YoutubeController extends ActionController {
}
public function indexAction() {
$this->view->assign('feed', $this->youtubeService->getJsonAsArray($this->settings['id']));
$id = $this->settings['id'];
$maxResults = (string) $this->settings['maxResults'];
$jsonArray = $this->youtubeService->getJsonAsArray($id, $maxResults);
$this->view->assign('feed', $jsonArray);
}
}
......@@ -32,31 +32,36 @@ namespace SGalinski\SgYoutube\Service;
class YoutubeService {
const API_KEY = 'AIzaSyCDgiB2v8gW00WFo4z0H99XWJJiJRVdnlc';
const API_URL = 'https://www.googleapis.com/youtube/v3/';
const API_CHANNEL = 'channels';
const API_CHANNEL = 'search';
const API_PLAYLIST = 'playlistItems';
const API_PART = 'contentDetails';
const API_PART = 'snippet';
const API_ORDER_BY = 'date';
/**
* @param string $youtubeId
* @param string $maxResults
* @return string
*/
public function getApiUrl($youtubeId = '') {
public function getApiUrl($youtubeId = '', $maxResults = '10') {
$apiUrl = self::API_URL;
$parameters = [];
if (preg_match('#^UC#', $youtubeId)) {
$apiUrl .= self::API_CHANNEL;
$parameters['channelId'] = $youtubeId;
} else if (preg_match('#^PL#', $youtubeId)) {
$apiUrl .= self::API_PLAYLIST;
$parameters['playlistId'] = $youtubeId;
} else {
throw new \InvalidArgumentException(
'Given ID is neither a channel nor a playlist.', 403
);
}
$parameters = [];
$parameters['order'] = self::API_ORDER_BY;
$parameters['part'] = self::API_PART;
$parameters['id'] = $youtubeId;
$parameters['key'] = self::API_KEY;
$parameters['maxResults'] = $maxResults;
$query = http_build_query($parameters);
return $apiUrl . '?' . $query;
......@@ -64,10 +69,11 @@ class YoutubeService {
/**
* @param string $youtubeId
* @param string $maxResults
* @return array|mixed
*/
public function getJsonAsArray($youtubeId ='') {
$url = $this->getApiUrl($youtubeId);
public function getJsonAsArray($youtubeId = '', $maxResults = '10') {
$url = $this->getApiUrl($youtubeId, $maxResults);
$jsonString = '';
if (function_exists('curl_init')) {
$ch = curl_init();
......@@ -101,7 +107,11 @@ class YoutubeService {
throw new \InvalidArgumentException('Message: ' . $jsonArray['error'], 403);
}
return $jsonArray;
if (!isset($jsonArray['items'])) {
throw new \InvalidArgumentException('No items array.', 403);
}
return $jsonArray['items'];
}
}
......@@ -21,10 +21,27 @@
<config>
<type>input</type>
<size>30</size>
<eval>trim</eval>
<eval>trim,required</eval>
</config>
</TCEforms>
</settings.id>
<settings.maxResults>
<TCEforms>
<exclude>0</exclude>
<label>LLL:EXT:sg_youtube/Resources/Private/Language/locallang.xlf:maxResults
</label>
<config>
<type>input</type>
<size>30</size>
<default>10</default>
<eval>trim,int</eval>
<range>
<lower>1</lower>
<upper>50</upper>
</range>
</config>
</TCEforms>
</settings.maxResults>
</el>
</ROOT>
</sDEF>
......
......@@ -9,6 +9,10 @@
<source>ID of Channel (UC) or Playlist (PL)</source>
<target>ID eines Channels (UC) oder einer Playlist (PL)</target>
</trans-unit>
<trans-unit id="maxResults" approved="yes">
<source>Maximum Amount</source>
<target>Maximale Anzahl</target>
</trans-unit>
</body>
</file>
</xliff>
\ No newline at end of file
......@@ -8,6 +8,9 @@
<trans-unit id="id">
<source>ID of Channel (UC) or Playlist (PL)</source>
</trans-unit>
<trans-unit id="maxResults">
<source>Maximum Amount</source>
</trans-unit>
</body>
</file>
</xliff>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment