[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()
{
$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();
}
}

@ -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;

@ -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;

@ -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.');
}

@ -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);
}
}
}

Loading…
Cancel
Save