mirror of
https://github.com/koel/koel
synced 2024-12-21 10:03:10 +00:00
36 lines
913 B
PHP
36 lines
913 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Http\Integrations\YouTube\Requests\SearchVideosRequest;
|
|
use App\Http\Integrations\YouTube\YouTubeConnector;
|
|
use App\Models\Song;
|
|
use Illuminate\Cache\Repository as Cache;
|
|
|
|
class YouTubeService
|
|
{
|
|
public function __construct(private YouTubeConnector $connector, private Cache $cache)
|
|
{
|
|
}
|
|
|
|
public static function enabled(): bool
|
|
{
|
|
return (bool) config('koel.youtube.key');
|
|
}
|
|
|
|
public function searchVideosRelatedToSong(Song $song, string $pageToken = ''): ?object
|
|
{
|
|
if (!self::enabled()) {
|
|
return null;
|
|
}
|
|
|
|
$request = new SearchVideosRequest($song, $pageToken);
|
|
$hash = md5(serialize($request->query()->all()));
|
|
|
|
return $this->cache->remember(
|
|
"youtube.$hash",
|
|
now()->addWeek(),
|
|
fn () => $this->connector->send($request)->object()
|
|
);
|
|
}
|
|
}
|