2015-12-19 16:36:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-03-22 12:55:25 +00:00
|
|
|
use App\Http\Integrations\Lastfm\LastfmConnector;
|
|
|
|
use App\Http\Integrations\Lastfm\Requests\GetAlbumInfoRequest;
|
|
|
|
use App\Http\Integrations\Lastfm\Requests\GetArtistInfoRequest;
|
|
|
|
use App\Http\Integrations\Lastfm\Requests\GetSessionKeyRequest;
|
|
|
|
use App\Http\Integrations\Lastfm\Requests\ScrobbleRequest;
|
|
|
|
use App\Http\Integrations\Lastfm\Requests\ToggleLoveTrackRequest;
|
|
|
|
use App\Http\Integrations\Lastfm\Requests\UpdateNowPlayingRequest;
|
2022-07-16 22:42:29 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
2022-08-08 16:00:59 +00:00
|
|
|
use App\Models\Song;
|
2021-06-04 16:19:34 +00:00
|
|
|
use App\Models\User;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Services\Contracts\MusicEncyclopedia;
|
2022-07-08 14:53:04 +00:00
|
|
|
use App\Values\AlbumInformation;
|
|
|
|
use App\Values\ArtistInformation;
|
2024-03-22 12:55:25 +00:00
|
|
|
use Generator;
|
2024-03-22 15:05:39 +00:00
|
|
|
use Illuminate\Cache\Repository as Cache;
|
2021-06-04 15:19:33 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2015-12-19 16:36:44 +00:00
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
class LastfmService implements MusicEncyclopedia
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2024-03-22 15:05:39 +00:00
|
|
|
public function __construct(private LastfmConnector $connector, private Cache $cache)
|
2022-08-08 16:00:59 +00:00
|
|
|
{
|
|
|
|
}
|
2015-12-19 16:36:44 +00:00
|
|
|
|
2016-06-05 04:37:03 +00:00
|
|
|
/**
|
|
|
|
* Determine if our application is using Last.fm.
|
|
|
|
*/
|
2022-08-08 16:00:59 +00:00
|
|
|
public static function used(): bool
|
2016-06-05 04:37:03 +00:00
|
|
|
{
|
2022-08-08 16:00:59 +00:00
|
|
|
return (bool) config('koel.lastfm.key');
|
2016-06-05 04:37:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 12:17:35 +00:00
|
|
|
/**
|
|
|
|
* Determine if Last.fm integration is enabled.
|
|
|
|
*/
|
2022-08-08 16:00:59 +00:00
|
|
|
public static function enabled(): bool
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2022-08-08 16:00:59 +00:00
|
|
|
return config('koel.lastfm.key') && config('koel.lastfm.secret');
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 22:42:29 +00:00
|
|
|
public function getArtistInformation(Artist $artist): ?ArtistInformation
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2024-03-15 12:23:52 +00:00
|
|
|
if ($artist->is_unknown || $artist->is_various) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
return attempt_if(static::enabled(), function () use ($artist): ?ArtistInformation {
|
2024-03-22 15:05:39 +00:00
|
|
|
return $this->cache->remember(
|
2024-03-22 15:33:18 +00:00
|
|
|
"lastfm.artist.$artist->id",
|
2024-03-22 15:05:39 +00:00
|
|
|
now()->addWeek(),
|
|
|
|
fn () => $this->connector->send(new GetArtistInfoRequest($artist))->dto()
|
|
|
|
);
|
2022-08-08 16:00:59 +00:00
|
|
|
});
|
2015-12-19 16:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 22:42:29 +00:00
|
|
|
public function getAlbumInformation(Album $album): ?AlbumInformation
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2024-03-15 12:23:52 +00:00
|
|
|
if ($album->is_unknown || $album->artist->is_unknown) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
return attempt_if(static::enabled(), function () use ($album): ?AlbumInformation {
|
2024-03-22 15:05:39 +00:00
|
|
|
return $this->cache->remember(
|
2024-03-22 15:33:18 +00:00
|
|
|
"lastfm.album.$album->id",
|
2024-03-22 15:05:39 +00:00
|
|
|
now()->addWeek(),
|
|
|
|
fn () => $this->connector->send(new GetAlbumInfoRequest($album))->dto()
|
|
|
|
);
|
2022-08-08 16:00:59 +00:00
|
|
|
});
|
2015-12-19 16:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
public function scrobble(Song $song, User $user, int $timestamp): void
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
2024-03-22 12:55:25 +00:00
|
|
|
attempt(fn () => $this->connector->send(new ScrobbleRequest($song, $user, $timestamp)));
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
public function toggleLoveTrack(Song $song, User $user, bool $love): void
|
2021-06-04 15:19:33 +00:00
|
|
|
{
|
2024-03-22 12:55:25 +00:00
|
|
|
attempt(fn () => $this->connector->send(new ToggleLoveTrackRequest($song, $user, $love)));
|
2021-06-04 15:19:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 13:49:00 +00:00
|
|
|
/**
|
2024-04-18 11:27:07 +00:00
|
|
|
* @param Collection<array-key, Song> $songs
|
2015-12-21 13:49:00 +00:00
|
|
|
*/
|
2022-08-08 16:00:59 +00:00
|
|
|
public function batchToggleLoveTracks(Collection $songs, User $user, bool $love): void
|
2015-12-21 13:49:00 +00:00
|
|
|
{
|
2024-03-22 12:55:25 +00:00
|
|
|
$generatorCallback = static function () use ($songs, $user, $love): Generator {
|
|
|
|
foreach ($songs as $song) {
|
|
|
|
yield new ToggleLoveTrackRequest($song, $user, $love);
|
2022-08-08 16:00:59 +00:00
|
|
|
}
|
2024-03-22 12:55:25 +00:00
|
|
|
};
|
2015-12-21 13:49:00 +00:00
|
|
|
|
2024-03-22 12:55:25 +00:00
|
|
|
$this->connector
|
|
|
|
->pool($generatorCallback)
|
|
|
|
->send()
|
|
|
|
->wait();
|
2015-12-21 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
public function updateNowPlaying(Song $song, User $user): void
|
|
|
|
{
|
2024-03-22 12:55:25 +00:00
|
|
|
attempt(fn () => $this->connector->send(new UpdateNowPlayingRequest($song, $user)));
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
public function getSessionKey(string $token): ?string
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
2024-03-22 12:55:25 +00:00
|
|
|
return object_get($this->connector->send(new GetSessionKeyRequest($token))->object(), 'session.key');
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
2021-06-04 16:19:34 +00:00
|
|
|
|
|
|
|
public function setUserSessionKey(User $user, ?string $sessionKey): void
|
|
|
|
{
|
|
|
|
$user->preferences->lastFmSessionKey = $sessionKey;
|
|
|
|
$user->save();
|
|
|
|
}
|
2015-12-19 16:36:44 +00:00
|
|
|
}
|