Skip to content

Commit

Permalink
feat: integrate with Spotify
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Jul 16, 2022
1 parent 1e38150 commit 8788156
Show file tree
Hide file tree
Showing 29 changed files with 262 additions and 258 deletions.
16 changes: 0 additions & 16 deletions app/Events/AlbumInformationFetched.php

This file was deleted.

16 changes: 0 additions & 16 deletions app/Events/ArtistInformationFetched.php

This file was deleted.

4 changes: 3 additions & 1 deletion app/Http/Controllers/V6/API/DataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Services\ApplicationInformationService;
use App\Services\ITunesService;
use App\Services\LastfmService;
use App\Services\SpotifyService;
use App\Services\YouTubeService;
use Illuminate\Contracts\Auth\Authenticatable;

Expand All @@ -36,7 +37,8 @@ public function index()
'playlists' => $this->playlistRepository->getAllByCurrentUser(),
'current_user' => UserResource::make($this->user),
'use_last_fm' => $this->lastfmService->used(),
'use_you_tube' => $this->youTubeService->enabled(),
'use_spotify' => SpotifyService::enabled(),
'use_you_tube' => $this->youTubeService->enabled(), // @todo clean this mess up
'use_i_tunes' => $this->iTunesService->used(),
'allow_download' => config('koel.download.allow'),
'supports_transcoding' => config('koel.streaming.ffmpeg_path')
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Requests/SpotifyCallbackRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Http\Requests;

/**
* @property-read string $state
* @property-read string $code
*/
class SpotifyCallbackRequest extends AbstractRequest
{
}
25 changes: 0 additions & 25 deletions app/Listeners/DownloadAlbumCover.php

This file was deleted.

25 changes: 0 additions & 25 deletions app/Listeners/DownloadArtistImage.php

This file was deleted.

12 changes: 0 additions & 12 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Providers;

use App\Events\AlbumInformationFetched;
use App\Events\ArtistInformationFetched;
use App\Events\LibraryChanged;
use App\Events\MediaSyncCompleted;
use App\Events\SongLikeToggled;
Expand All @@ -12,8 +10,6 @@
use App\Events\SongStartedPlaying;
use App\Listeners\ClearMediaCache;
use App\Listeners\DeleteNonExistingRecordsPostSync;
use App\Listeners\DownloadAlbumCover;
use App\Listeners\DownloadArtistImage;
use App\Listeners\LoveMultipleTracksOnLastfm;
use App\Listeners\LoveTrackOnLastfm;
use App\Listeners\PruneLibrary;
Expand Down Expand Up @@ -49,14 +45,6 @@ class EventServiceProvider extends ServiceProvider
ClearMediaCache::class,
],

AlbumInformationFetched::class => [
DownloadAlbumCover::class,
],

ArtistInformationFetched::class => [
DownloadArtistImage::class,
],

MediaSyncCompleted::class => [
DeleteNonExistingRecordsPostSync::class,
],
Expand Down
2 changes: 1 addition & 1 deletion app/Services/AbstractApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Webmozart\Assert\Assert;

/**
* @method object get(string $uri, array $data = [], bool $appendKey = true)
* @method object|null get(string $uri, array $data = [], bool $appendKey = true)
* @method object post($uri, array $data = [], bool $appendKey = true)
* @method object put($uri, array $data = [], bool $appendKey = true)
* @method object patch($uri, array $data = [], bool $appendKey = true)
Expand Down
2 changes: 1 addition & 1 deletion app/Services/FileSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function tryGenerateAlbumCover(Album $album, ?array $coverData): void

if ($cover) {
$extension = pathinfo($cover, PATHINFO_EXTENSION);
$this->mediaMetadataService->writeAlbumCover($album, file_get_contents($cover), $extension);
$this->mediaMetadataService->writeAlbumCover($album, $cover, $extension);
}
} catch (Throwable) {
}
Expand Down
4 changes: 2 additions & 2 deletions app/Services/ImageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public function __construct(ImageManager $imageManager)
$this->imageManager = $imageManager;
}

public function writeFromBinaryData(string $destination, string $data, array $config = []): void
public function write(string $destination, object|string $source, array $config = []): void
{
$img = $this->imageManager
->make($data)
->make($source)
->resize(
$config['max_width'] ?? self::DEFAULT_MAX_WIDTH,
null,
Expand Down
12 changes: 7 additions & 5 deletions app/Services/LastfmService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Services;

use App\Models\Album;
use App\Models\Artist;
use App\Models\User;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
Expand Down Expand Up @@ -34,13 +36,13 @@ public function enabled(): bool
return $this->getKey() && $this->getSecret();
}

public function getArtistInformation(string $name): ?ArtistInformation
public function getArtistInformation(Artist $artist): ?ArtistInformation
{
if (!$this->enabled()) {
return null;
}

$name = urlencode($name);
$name = urlencode($artist->name);

try {
return $this->cache->remember(
Expand All @@ -59,14 +61,14 @@ function () use ($name): ?ArtistInformation {
}
}

public function getAlbumInformation(string $albumName, string $artistName): ?AlbumInformation
public function getAlbumInformation(Album $album): ?AlbumInformation
{
if (!$this->enabled()) {
return null;
}

$albumName = urlencode($albumName);
$artistName = urlencode($artistName);
$albumName = urlencode($album->name);
$artistName = urlencode($album->artist->name);

try {
$cacheKey = md5("lastfm_album_{$albumName}_{$artistName}");
Expand Down
41 changes: 25 additions & 16 deletions app/Services/MediaInformationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

namespace App\Services;

use App\Events\AlbumInformationFetched;
use App\Events\ArtistInformationFetched;
use App\Models\Album;
use App\Models\Artist;
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
use Throwable;

class MediaInformationService
{
public function __construct(
private LastfmService $lastfmService,
private AlbumRepository $albumRepository,
private ArtistRepository $artistRepository
private SpotifyService $spotifyService,
private MediaMetadataService $mediaMetadataService
) {
}

Expand All @@ -26,12 +23,18 @@ public function getAlbumInformation(Album $album): ?AlbumInformation
return null;
}

$info = $this->lastfmService->getAlbumInformation($album->name, $album->artist->name);
$info = $this->lastfmService->getAlbumInformation($album) ?: new AlbumInformation();

if ($info) {
event(new AlbumInformationFetched($album, $info));
// The album cover may have been updated.
$info->cover = $this->albumRepository->getOneById($album->id)->cover;
if (!$album->has_cover) {
try {
$cover = $this->spotifyService->tryGetAlbumCover($album);

if ($cover) {
$this->mediaMetadataService->downloadAlbumCover($album, $cover);
$info->cover = $album->refresh()->cover;
}
} catch (Throwable) {
}
}

return $info;
Expand All @@ -43,12 +46,18 @@ public function getArtistInformation(Artist $artist): ?ArtistInformation
return null;
}

$info = $this->lastfmService->getArtistInformation($artist->name);
$info = $this->lastfmService->getArtistInformation($artist) ?: new ArtistInformation();

if (!$artist->has_image) {
try {
$image = $this->spotifyService->tryGetArtistImage($artist);

if ($info) {
event(new ArtistInformationFetched($artist, $info));
// The artist image may have been updated.
$info->image = $this->artistRepository->getOneById($artist->id)->image;
if ($image) {
$this->mediaMetadataService->downloadArtistImage($artist, $image);
$info->image = $artist->refresh()->image;
}
} catch (Throwable) {
}
}

return $info;
Expand Down
Loading

0 comments on commit 8788156

Please sign in to comment.