From 6af3f45af5afb3eb19622227e0b2a99f3debd59d Mon Sep 17 00:00:00 2001 From: Brett Spaulding Date: Sun, 11 Aug 2024 20:17:20 -0400 Subject: [PATCH] [IMP] php: Command update for album covers --- .../Console/Commands/UpdateAlbumImages.php | 46 ++++++++++++++ .../Console/Commands/UpdateArtistImages.php | 2 +- php/src/app/Models/Album.php | 2 +- php/src/app/Models/WebScraper.php | 62 ++++++++++--------- php/src/app/Utils/ImageUrl.php | 2 +- php/src/public/js/app.js | 14 ++--- 6 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 php/src/app/Console/Commands/UpdateAlbumImages.php diff --git a/php/src/app/Console/Commands/UpdateAlbumImages.php b/php/src/app/Console/Commands/UpdateAlbumImages.php new file mode 100644 index 0000000..e143906 --- /dev/null +++ b/php/src/app/Console/Commands/UpdateAlbumImages.php @@ -0,0 +1,46 @@ +output, count($albums)); + $bar->start(); + foreach ($albums as $album) { + $image_url = ImageUrl::modifyGoogleImageUrl($album->thumbnail); + $image_file = ImageUrl::save_img_url($image_url, 'album'); + echo '-------------------'; + echo $image_url . '\n'; + echo $image_file . '\n'; + echo '-------------------'; + $album->image = $image_file; + $album->save(); + $bar->advance(); + } + } +} diff --git a/php/src/app/Console/Commands/UpdateArtistImages.php b/php/src/app/Console/Commands/UpdateArtistImages.php index 3e247e6..b5c38e0 100644 --- a/php/src/app/Console/Commands/UpdateArtistImages.php +++ b/php/src/app/Console/Commands/UpdateArtistImages.php @@ -33,7 +33,7 @@ class UpdateArtistImages extends Command $bar->start(); foreach ($artists as $artist) { $image_url = ImageUrl::modifyGoogleImageUrl($artist->thumbnail); - $image_file = ImageUrl::save_img_url($image_url); + $image_file = ImageUrl::save_img_url($image_url, 'artist'); $artist->image = $image_file; $artist->save(); $bar->advance(); diff --git a/php/src/app/Models/Album.php b/php/src/app/Models/Album.php index c5f205c..d661afe 100644 --- a/php/src/app/Models/Album.php +++ b/php/src/app/Models/Album.php @@ -45,7 +45,7 @@ class Album extends Model public static function findOrCreateByName($artist_id, string $name, array $data = []) { $album = self::findByArtistTitle($artist_id, $name); - if ($album->exists() && $data) { + if (is_null($album) && $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/WebScraper.php b/php/src/app/Models/WebScraper.php index 66aaab2..6433c9a 100644 --- a/php/src/app/Models/WebScraper.php +++ b/php/src/app/Models/WebScraper.php @@ -106,45 +106,51 @@ class WebScraper * * @return RemoteWebDriver */ - public static function scrapeAlbums($driver, $artist_id): array + public static function scrapeAlbums($driver, $artist_id) { $url = 'https://music.youtube.com/' . $artist_id->url_remote; $driver->get($url); $response = 0; - $albumBtn = $driver->findElement(WebDriverBy::xpath('//a[text()="Albums"]')); - if ($albumBtn) { - $albumBtn->click(); - sleep(3); - $itemsContainer = $driver->findElements(WebDriverBy::cssSelector('#items')); - foreach ($itemsContainer as $item) { - $albumContainers = $item->findElements(WebDriverBy::cssSelector('.ytmusic-grid-renderer')); - if ($albumContainers) { - foreach ($albumContainers as $albumContainer) { - $response += 1; - $albumLink = $albumContainer->findElement(WebDriverBy::cssSelector('a')); - $albumHref = $albumLink->getAttribute('href'); - $albumTitle = $albumLink->getAttribute('title'); - $albumThumbnail = $albumLink->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src'); + try { + $albumBtn = $driver->findElement(WebDriverBy::xpath('//a[text()="Albums"]')); + if ($albumBtn) { + $albumBtn->click(); + sleep(3); + $itemsContainer = $driver->findElements(WebDriverBy::cssSelector('#items')); + foreach ($itemsContainer as $item) { + $albumContainers = $item->findElements(WebDriverBy::cssSelector('.ytmusic-grid-renderer')); + if ($albumContainers) { + foreach ($albumContainers as $albumContainer) { + $response += 1; + $albumLink = $albumContainer->findElement(WebDriverBy::cssSelector('a')); + $albumHref = $albumLink->getAttribute('href'); + $albumTitle = $albumLink->getAttribute('title'); + $albumThumbnail = $albumLink->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src'); - // Resize image and save to file, provide path to data - $imageUrl = ImageUrl::modifyGoogleImageUrl($albumThumbnail); - $imageFileUrl = ImageUrl::save_img_url($imageUrl, 'album'); + // Resize image and save to file, provide path to data + $imageUrl = ImageUrl::modifyGoogleImageUrl($albumThumbnail); + $imageFileUrl = ImageUrl::save_img_url($imageUrl, 'album'); - $data = [ - 'name' => $albumTitle, - 'artist_id' => $artist_id->id, - 'thumbnail' => $albumThumbnail, - 'url_remote' => $albumHref, - 'image' => $imageFileUrl, - ]; - $album_id = Album::findOrCreateByName($artist_id, $albumTitle, $data); + $data = [ + 'name' => $albumTitle, + 'artist_id' => $artist_id->id, + 'thumbnail' => $albumThumbnail, + 'url_remote' => $albumHref, + 'image' => $imageFileUrl, + ]; + $album_id = Album::findOrCreateByName($artist_id, $albumTitle, $data); - $album_queue = new AlbumQueue(); - $album_queue->enqueue($album_id); + $album_queue = new AlbumQueue(); + $album_queue->enqueue($album_id); + } } } } + } catch (\Exception $e) { + \Log::warning('Failed to scrape albums: ---------'); + \Log::warning($e->getMessage()); } + return $response; } } diff --git a/php/src/app/Utils/ImageUrl.php b/php/src/app/Utils/ImageUrl.php index 66bd771..e5394f8 100644 --- a/php/src/app/Utils/ImageUrl.php +++ b/php/src/app/Utils/ImageUrl.php @@ -45,7 +45,7 @@ class ImageUrl // Check if the file already exists $imagePath = $imagesDir . '/' . $filename . '.jpg'; if (file_exists($imagePath)) { - return ''; // File already exists, don't save again + return $imagePath; // File already exists, don't save again } // Download the image from the URL using curl diff --git a/php/src/public/js/app.js b/php/src/public/js/app.js index 150399c..200ede9 100644 --- a/php/src/public/js/app.js +++ b/php/src/public/js/app.js @@ -1,6 +1,7 @@ console.log('Version 1:20:2'); const appModal = $('#modalDownloadQueue'); const loader = $("#loader-wrapper"); +let ArtistTable = {}; // Initialized for ajax reload function template_artist_result(element) { return ` @@ -48,14 +49,13 @@ function proc_notification(icon, title, html) { function artist_queue_toggle(element) { let self = $(element); - console.log(self); - console.log(self.data('artist_id')); let artist_name = self.data('artist_name'); self.prop('disabled', true) $.ajax({ url: `/api/queue/artist/${self.data('artist_id')}`, success: () => { proc_notification('success', 'Queued Download', `Artist ${artist_name} Queued for Download!`); + ArtistTable.ajax.reload(); }, error: (response) => { console.log(response); @@ -95,6 +95,7 @@ function bind_action_buttons() { icon = 'success'; let html = construct_artist_result_html(response); proc_notification(icon, 'Shazam!', html); + ArtistTable.ajax.reload(); $('#search_bar').val(''); loader.fadeOut(700); }, @@ -133,11 +134,9 @@ document.addEventListener('alpine:init', () => { }); $(document).ready(function () { - bind_action_buttons(); - //Datatable for 'Catalog' menu - let ArtistTable = $('#artistsCatalogDatatable').DataTable({ + ArtistTable = $('#artistsCatalogDatatable').DataTable({ ajax: '/api/artists', type: 'get', dataType: 'json', @@ -164,9 +163,4 @@ $(document).ready(function () { } ], }); - // Polling for table update - const getArtistTableInterval = setInterval(function () { - ArtistTable.ajax.reload(); - }, 5000); }); -