koel/app/Services/LastfmService.php

118 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
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;
use App\Models\User;
use App\Services\Contracts\MusicEncyclopedia;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
use Generator;
use Illuminate\Cache\Repository as Cache;
use Illuminate\Support\Collection;
2022-08-08 16:00:59 +00:00
class LastfmService implements MusicEncyclopedia
{
2024-04-18 14:36:28 +00:00
public function __construct(private readonly LastfmConnector $connector, private readonly Cache $cache)
2022-08-08 16:00:59 +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
{
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 {
return $this->cache->remember(
2024-03-22 15:33:18 +00:00
"lastfm.artist.$artist->id",
now()->addWeek(),
fn () => $this->connector->send(new GetArtistInfoRequest($artist))->dto()
);
2022-08-08 16:00:59 +00:00
});
}
2022-07-16 22:42:29 +00:00
public function getAlbumInformation(Album $album): ?AlbumInformation
{
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 {
return $this->cache->remember(
2024-03-22 15:33:18 +00:00
"lastfm.album.$album->id",
now()->addWeek(),
fn () => $this->connector->send(new GetAlbumInfoRequest($album))->dto()
);
2022-08-08 16:00:59 +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
{
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
{
attempt(fn () => $this->connector->send(new ToggleLoveTrackRequest($song, $user, $love)));
}
2015-12-21 13:49:00 +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
{
$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
}
};
2015-12-21 13:49:00 +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
{
attempt(fn () => $this->connector->send(new UpdateNowPlayingRequest($song, $user)));
}
2022-08-08 16:00:59 +00:00
public function getSessionKey(string $token): ?string
{
return object_get($this->connector->send(new GetSessionKeyRequest($token))->object(), 'session.key');
}
public function setUserSessionKey(User $user, ?string $sessionKey): void
{
$user->preferences->lastFmSessionKey = $sessionKey;
$user->save();
}
}