Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions app/Console/Commands/UpdateArtistImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Console\Commands;

use App\Enums\ConnectionType;
use App\Enums\MediaType;
use App\Livewire\Actions\Api\Spotify\RefreshToken;
use App\Models\Media;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class UpdateArtistImages extends Command
{
protected $signature = 'artists:update-images';

protected $description = 'Update Artist Profile Images';

public function handle()
{
$user = User::first();

(new RefreshToken)->handle($user);

/* Loop through every artist in chunks of 50 and attempt to update their image. */
Media::query()
->where('type_id', MediaType::ARTIST->value)
->get()
->chunk(50)
->each(function ($chunk) use ($user) {
$ids = $chunk->pluck('media_id')->implode(',');

$response = (new Client)->request(
'GET',
"https://api.spotify.com/v1/artists?ids={$ids}", [
'headers' => [
'Authorization' => 'Bearer '. $user->connections->firstWhere('type_id', ConnectionType::SPOTIFY->value)->token,
],
]
);

$artists = collect(collect(json_decode($response->getBody()))->get('artists'))
->filter(fn ($artist) => $artist !== null);

if (count($artists)) {
$artists = collect($artists->map(fn ($artist) => [
'media_id' => $artist->id,
'cover' => data_get($artist, 'images.0.url'),
]));

foreach ($artists as $artist) {
Media::query()
->where('media_id', $artist['media_id'])
->first()
?->update(['cover' => $artist['cover']]);
}
}
});

return Command::SUCCESS;
}
}
23 changes: 23 additions & 0 deletions app/Enums/MediaType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Enums;

enum MediaType: int
{
case MOVIE = 1;
case TV = 2;
case ARTIST = 3;
case TRACK = 4;
case VIDEO_GAME = 5;

public function label(): string
{
return match($this) {
self::MOVIE => 'Movie',
self::TV => 'Tv',
self::ARTIST => 'Artist',
self::TRACK => 'Track',
self::VIDEO_GAME => 'Video Game',
};
}
}
8 changes: 4 additions & 4 deletions app/Livewire/Actions/Api/SearchMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace App\Livewire\Actions\Api;

use App\Enums\MediaType;
use App\Models\Media;
use App\Models\MediaType;
use App\Models\User;
use Illuminate\Support\Facades\Http;

final class SearchMedia
{
public function search(User $user, string $phrase, int $mediaType)
public function search(User $user, string $phrase, MediaType $mediaType)
{
$type = $mediaType == MediaType::MOVIE ? 'movie' : 'tv';
$type = $mediaType->value == MediaType::MOVIE->value ? 'movie' : 'tv';

$response = Http::withHeaders([
'Authorization' => 'Bearer '.config('services.movie_db.access_token'),
Expand All @@ -34,7 +34,7 @@ public function search(User $user, string $phrase, int $mediaType)
return [
'type_id' => $mediaType,
'media_id' => $media['id'],
'name' => $mediaType == MediaType::MOVIE ? $media['original_title'] : $media['original_name'],
'name' => $mediaType->value == MediaType::MOVIE->value ? $media['original_title'] : $media['original_name'],
'cover' => 'https://image.tmdb.org/t/p/original'.$media['poster_path'],
];
})->filter();
Expand Down
106 changes: 0 additions & 106 deletions app/Livewire/Actions/Api/SearchSpotify.php

This file was deleted.

38 changes: 38 additions & 0 deletions app/Livewire/Actions/Api/Spotify/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Livewire\Actions\Api\Spotify;

use App\Enums\ConnectionType;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

final class RefreshToken
{
public function handle(User $user): bool
{
try {
$spotifyConnection = $user->connections->firstWhere('type_id', ConnectionType::SPOTIFY->value);

$response = Http::asForm()->post('https://accounts.spotify.com/api/token', [
'grant_type' => 'refresh_token',
'refresh_token' => $spotifyConnection->refresh_token,
'client_id' => config('services.spotify.client_id'),
'client_secret' => config('services.spotify.client_secret'),
]);

if (! $response->successful()) {
return false;
}

$spotifyConnection->update(['token' => $response->json()['access_token']]);

return true;
} catch (Exception $e) {
Log::error("Could not refresh user: {$user->name} token: {$e->getMessage()}");

return false;
}
}
}
52 changes: 52 additions & 0 deletions app/Livewire/Actions/Api/Spotify/SearchArtist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Livewire\Actions\Api\Spotify;

use App\Enums\ConnectionType;
use App\Enums\MediaType;
use App\Models\Media;
use App\Models\User;
use Illuminate\Support\Facades\Http;

final class SearchArtist
{
public function handle(User $user, string $phrase)
{
(new RefreshToken)->handle($user);

$response = Http::withHeaders([
'Authorization' => 'Bearer '.$user->connections->firstWhere('type_id', ConnectionType::SPOTIFY->value)->token,
'Content-Type' => 'application/json',
'Client-Id' => config('services.spotify.client_id'),
])->get('https://api.spotify.com/v1/search', [
'q' => $phrase,
'type' => 'artist',
'limit' => 10,
]);

if ($response->successful()) {
$artists = Media::query()
->where('type_id', MediaType::ARTIST->value)
->pluck('media_id')
->toArray();

return collect($response->json('artists.items'))->map(function ($artist) use ($artists) {
$cover = data_get($artist, 'images.0.url');

if (in_array($artist['id'], $artists) || is_null($cover)) {
return;
}

return [
'type_id' => MediaType::ARTIST->value,
'media_id' => $artist['id'],
'name' => $artist['name'],
'cover' => $cover,
'data' => null,
];
})->filter();
}

return collect();
}
}
56 changes: 56 additions & 0 deletions app/Livewire/Actions/Api/Spotify/SearchTrack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Livewire\Actions\Api\Spotify;

use App\Enums\ConnectionType;
use App\Enums\MediaType;
use App\Models\Media;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;

final class SearchTrack
{
public function handle(User $user, string $phrase): Collection
{
(new RefreshToken)->handle($user);

$response = Http::withHeaders([
'Authorization' => 'Bearer '.$user->connections->firstWhere('type_id', ConnectionType::SPOTIFY->value)->token,
'Content-Type' => 'application/json',
'Client-Id' => config('services.spotify.client_id'),
])->get('https://api.spotify.com/v1/search', [
'q' => $phrase,
'type' => 'track',
'limit' => 10,
]);

if ($response->successful()) {
$tracks = Media::query()
->where('type_id', MediaType::TRACK->value)
->pluck('media_id')
->toArray();

return collect($response->json('tracks.items'))->map(function ($track) use ($tracks) {
$cover = data_get($track, 'album.images.0.url');

if (in_array($track['id'], $tracks) || is_null($cover)) {
return;
}

return [
'type_id' => MediaType::TRACK->value,
'media_id' => $track['id'],
'name' => $track['name'],
'cover' => $cover,
'data' => [
'artist_name' => data_get($track, 'artists.0.name'),
'album_name' => data_get($track, 'album.name'),
],
];
})->filter();
}

return collect();
}
}
Loading