2016-07-14 08:47:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2016-07-30 15:32:17 +00:00
|
|
|
use App\Models\Song;
|
2020-01-25 13:04:05 +00:00
|
|
|
use Exception;
|
2016-07-14 08:47:50 +00:00
|
|
|
|
2019-04-07 21:09:25 +00:00
|
|
|
class YouTubeService extends AbstractApiClient implements ApiConsumerInterface
|
2016-07-14 08:47:50 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if our application is using YouTube.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function enabled(): bool
|
2016-07-14 08:47:50 +00:00
|
|
|
{
|
2018-08-19 11:08:16 +00:00
|
|
|
return (bool) $this->getKey();
|
2016-07-14 08:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-30 15:32:17 +00:00
|
|
|
* Search for YouTube videos related to a song.
|
|
|
|
*
|
2018-08-24 15:27:19 +00:00
|
|
|
* @return mixed|null
|
2016-07-30 15:32:17 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function searchVideosRelatedToSong(Song $song, string $pageToken = '')
|
2016-07-30 15:32:17 +00:00
|
|
|
{
|
|
|
|
$q = $song->title;
|
|
|
|
|
|
|
|
// If the artist is worth noticing, include them into the search.
|
2017-06-03 23:21:50 +00:00
|
|
|
if (!$song->artist->is_unknown && !$song->artist->is_various) {
|
|
|
|
$q .= " {$song->artist->name}";
|
2016-07-30 15:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->search($q, $pageToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for YouTube videos by a query string.
|
2016-07-14 08:47:50 +00:00
|
|
|
*
|
|
|
|
* @param string $q The query string
|
|
|
|
* @param string $pageToken YouTube page token (e.g. for next/previous page)
|
|
|
|
* @param int $perPage Number of results per page
|
|
|
|
*
|
2018-08-24 15:27:19 +00:00
|
|
|
* @return mixed|null
|
2016-07-14 08:47:50 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function search(string $q, string $pageToken = '', int $perPage = 10)
|
2016-07-14 08:47:50 +00:00
|
|
|
{
|
|
|
|
if (!$this->enabled()) {
|
2020-01-25 13:04:05 +00:00
|
|
|
return null;
|
2016-07-14 08:47:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 10:16:55 +00:00
|
|
|
$uri = sprintf(
|
|
|
|
'search?part=snippet&type=video&maxResults=%s&pageToken=%s&q=%s',
|
2016-07-14 08:47:50 +00:00
|
|
|
$perPage,
|
|
|
|
urlencode($pageToken),
|
|
|
|
urlencode($q)
|
|
|
|
);
|
|
|
|
|
2020-01-25 13:04:05 +00:00
|
|
|
try {
|
|
|
|
return $this->cache->remember(md5("youtube_$uri"), 60 * 24 * 7, function () use ($uri) {
|
|
|
|
return $this->get($uri);
|
|
|
|
});
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logger->error($e);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-14 08:47:50 +00:00
|
|
|
}
|
2018-08-19 11:08:16 +00:00
|
|
|
|
2018-08-29 10:36:05 +00:00
|
|
|
public function getEndpoint(): ?string
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
|
|
|
return config('koel.youtube.endpoint');
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:36:05 +00:00
|
|
|
public function getKey(): ?string
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
|
|
|
return config('koel.youtube.key');
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function getSecret(): ?string
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
2018-08-24 15:27:19 +00:00
|
|
|
return null;
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
2016-07-14 08:47:50 +00:00
|
|
|
}
|