[IMP] Prevent album queue duplicates

refactor_total
Brett Spaulding 1 year ago
parent 4e35c6acae
commit 3148378334

@ -27,11 +27,11 @@ class ProcessArtistQueue extends Command
*/ */
public function handle() public function handle()
{ {
$records = ArtistQueue::where('state', 'pending')->get(); $artists = ArtistQueue::where('state', 'pending')->get();
$bar = new ProgressBar($this->output, count($records)); $bar = new ProgressBar($this->output, count($artists));
$bar->start(); $bar->start();
foreach ($records as $record) { foreach ($artists as $artist) {
$record->process_artist(); $artist->process_artist();
$bar->advance(); $bar->advance();
} }
} }

@ -20,12 +20,12 @@ class Album extends Model
$this->save(); $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(); return self::where('id', '=', $id)->get();
} }
@ -42,10 +42,10 @@ class Album extends Model
return $album; 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(); $album = self::findByArtistTitle($artist_id, $name);
if (!$album && $data) { if ($album->exists() && $data) {
$album = self::addAlbum($data['name'], $data['thumbnail'], $data['url_remote'], $data['image'], $data['artist_id']); $album = self::addAlbum($data['name'], $data['thumbnail'], $data['url_remote'], $data['image'], $data['artist_id']);
} }
return $album; return $album;

@ -9,11 +9,11 @@ class AlbumQueue extends Model
{ {
use HasFactory; use HasFactory;
public function enqueue(int $id): bool public function enqueue($album_id): bool
{ {
$result = false; $result = false;
$album_id = Album::findById($id)->first(); $album_queued = AlbumQueue::where('album_id', $album_id->id)->first();
if ($album_id->count() > 0 && $album_id->state === 'pending') { if (is_null($album_queued) && $album_id->state === 'pending') {
$this->album_id = $album_id->id; $this->album_id = $album_id->id;
$this->save(); $this->save();
$result = true; $result = true;

@ -31,7 +31,13 @@ class ArtistQueue extends Model
$driver = WebDriver::setUp(); $driver = WebDriver::setUp();
$artist_id = Artist::where('id', $this->artist_id)->get()->first(); $artist_id = Artist::where('id', $this->artist_id)->get()->first();
if ($artist_id->count() > 0) { 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 { } else {
throw new Exception('The Artist ID provided to the queue does not exist.'); throw new Exception('The Artist ID provided to the queue does not exist.');
} }

@ -136,10 +136,10 @@ class WebScraper
'url_remote' => $albumHref, 'url_remote' => $albumHref,
'image' => $imageFileUrl, 'image' => $imageFileUrl,
]; ];
$album_id = Album::findOrCreateByName($albumTitle, $data); $album_id = Album::findOrCreateByName($artist_id, $albumTitle, $data);
$album_queue = new AlbumQueue(); $album_queue = new AlbumQueue();
$album_queue_id = $album_queue->enqueue($album_id->id); $album_queue->enqueue($album_id);
} }
} }
} }

Loading…
Cancel
Save