diff --git a/php/src/app/Console/Commands/ProcessArtistQueue.php b/php/src/app/Console/Commands/ProcessArtistQueue.php index ad50f02..2f020d4 100644 --- a/php/src/app/Console/Commands/ProcessArtistQueue.php +++ b/php/src/app/Console/Commands/ProcessArtistQueue.php @@ -27,11 +27,11 @@ class ProcessArtistQueue extends Command */ public function handle() { - $records = ArtistQueue::where('state', 'pending')->get(); - $bar = new ProgressBar($this->output, count($records)); + $artists = ArtistQueue::where('state', 'pending')->get(); + $bar = new ProgressBar($this->output, count($artists)); $bar->start(); - foreach ($records as $record) { - $record->process_artist(); + foreach ($artists as $artist) { + $artist->process_artist(); $bar->advance(); } } diff --git a/php/src/app/Models/Album.php b/php/src/app/Models/Album.php index 0a00dd8..c5f205c 100644 --- a/php/src/app/Models/Album.php +++ b/php/src/app/Models/Album.php @@ -20,12 +20,12 @@ class Album extends Model $this->save(); } - public static function findByName($name) + public static function findByArtistTitle(Artist $artist, string $name) { - return self::where('name', '=', $name)->get(); + return self::where('name', '=', $name)->where('artist_id', '=', $artist->id)->first(); } - public static function findById($id) + public static function findById(int $id) { return self::where('id', '=', $id)->get(); } @@ -42,10 +42,10 @@ class Album extends Model return $album; } - public static function findOrCreateByName(string $name, array $data = []) + public static function findOrCreateByName($artist_id, string $name, array $data = []) { - $album = self::findByName($name)->first(); - if (!$album && $data) { + $album = self::findByArtistTitle($artist_id, $name); + if ($album->exists() && $data) { $album = self::addAlbum($data['name'], $data['thumbnail'], $data['url_remote'], $data['image'], $data['artist_id']); } return $album; diff --git a/php/src/app/Models/AlbumQueue.php b/php/src/app/Models/AlbumQueue.php index eb50660..0d69687 100644 --- a/php/src/app/Models/AlbumQueue.php +++ b/php/src/app/Models/AlbumQueue.php @@ -9,11 +9,11 @@ class AlbumQueue extends Model { use HasFactory; - public function enqueue(int $id): bool + public function enqueue($album_id): bool { $result = false; - $album_id = Album::findById($id)->first(); - if ($album_id->count() > 0 && $album_id->state === 'pending') { + $album_queued = AlbumQueue::where('album_id', $album_id->id)->first(); + if (is_null($album_queued) && $album_id->state === 'pending') { $this->album_id = $album_id->id; $this->save(); $result = true; diff --git a/php/src/app/Models/ArtistQueue.php b/php/src/app/Models/ArtistQueue.php index 61b2388..d18f690 100644 --- a/php/src/app/Models/ArtistQueue.php +++ b/php/src/app/Models/ArtistQueue.php @@ -31,7 +31,13 @@ class ArtistQueue extends Model $driver = WebDriver::setUp(); $artist_id = Artist::where('id', $this->artist_id)->get()->first(); if ($artist_id->count() > 0) { - $response = WebScraper::scrapeAlbums($driver, $artist_id); + try { + WebScraper::scrapeAlbums($driver, $artist_id); + } catch (Exception $e) { + \Log::warning('Failed to scrape albums: ' . $e->getMessage()); + } finally { + $driver->quit(); + } } else { throw new Exception('The Artist ID provided to the queue does not exist.'); } diff --git a/php/src/app/Models/WebScraper.php b/php/src/app/Models/WebScraper.php index 08037d6..1776476 100644 --- a/php/src/app/Models/WebScraper.php +++ b/php/src/app/Models/WebScraper.php @@ -136,10 +136,10 @@ class WebScraper 'url_remote' => $albumHref, 'image' => $imageFileUrl, ]; - $album_id = Album::findOrCreateByName($albumTitle, $data); + $album_id = Album::findOrCreateByName($artist_id, $albumTitle, $data); $album_queue = new AlbumQueue(); - $album_queue_id = $album_queue->enqueue($album_id->id); + $album_queue->enqueue($album_id); } } }