koel/app/Services/YouTubeService.php

42 lines
1,003 B
PHP
Raw Normal View History

2016-07-14 08:47:50 +00:00
<?php
namespace App\Services;
use App\Http\Integrations\YouTube\Requests\SearchVideosRequest;
use App\Http\Integrations\YouTube\YouTubeConnector;
2016-07-30 15:32:17 +00:00
use App\Models\Song;
2024-06-04 13:35:00 +00:00
use Illuminate\Support\Facades\Cache;
2024-04-04 22:20:42 +00:00
use Throwable;
2016-07-14 08:47:50 +00:00
2022-08-08 16:00:59 +00:00
class YouTubeService
2016-07-14 08:47:50 +00:00
{
2024-06-04 13:35:00 +00:00
public function __construct(private readonly YouTubeConnector $connector)
2022-08-08 16:00:59 +00:00
{
}
public static function enabled(): bool
2016-07-14 08:47:50 +00:00
{
2022-08-08 16:00:59 +00:00
return (bool) config('koel.youtube.key');
2016-07-14 08:47:50 +00:00
}
public function searchVideosRelatedToSong(Song $song, string $pageToken = ''): ?object
2016-07-30 15:32:17 +00:00
{
if (!self::enabled()) {
return null;
2016-07-30 15:32:17 +00:00
}
$request = new SearchVideosRequest($song, $pageToken);
$hash = md5(serialize($request->query()->all()));
2022-08-08 16:00:59 +00:00
2024-04-04 22:20:42 +00:00
try {
2024-06-04 13:35:00 +00:00
return Cache::remember(
2024-04-04 22:20:42 +00:00
"youtube.$hash",
now()->addWeek(),
fn () => $this->connector->send($request)->object()
);
} catch (Throwable) {
return null;
}
}
2016-07-14 08:47:50 +00:00
}