[IMP] php: Command update for album covers

refactor_total
Brett Spaulding 1 year ago
parent 45abbc823f
commit 6af3f45af5

@ -0,0 +1,46 @@
<?php
namespace App\Console\Commands;
use App\Models\Album;
use App\Utils\ImageUrl;
use Illuminate\Console\Command;
use Symfony\Component\Console\Helper\ProgressBar;
class UpdateAlbumImages extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:update-album-images';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$albums = Album::all();
$bar = new ProgressBar($this->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();
}
}
}

@ -33,7 +33,7 @@ class UpdateArtistImages extends Command
$bar->start(); $bar->start();
foreach ($artists as $artist) { foreach ($artists as $artist) {
$image_url = ImageUrl::modifyGoogleImageUrl($artist->thumbnail); $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->image = $image_file;
$artist->save(); $artist->save();
$bar->advance(); $bar->advance();

@ -45,7 +45,7 @@ class Album extends Model
public static function findOrCreateByName($artist_id, string $name, array $data = []) public static function findOrCreateByName($artist_id, string $name, array $data = [])
{ {
$album = self::findByArtistTitle($artist_id, $name); $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']); $album = self::addAlbum($data['name'], $data['thumbnail'], $data['url_remote'], $data['image'], $data['artist_id']);
} }
return $album; return $album;

@ -106,45 +106,51 @@ class WebScraper
* *
* @return RemoteWebDriver * @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; $url = 'https://music.youtube.com/' . $artist_id->url_remote;
$driver->get($url); $driver->get($url);
$response = 0; $response = 0;
$albumBtn = $driver->findElement(WebDriverBy::xpath('//a[text()="Albums"]')); try {
if ($albumBtn) { $albumBtn = $driver->findElement(WebDriverBy::xpath('//a[text()="Albums"]'));
$albumBtn->click(); if ($albumBtn) {
sleep(3); $albumBtn->click();
$itemsContainer = $driver->findElements(WebDriverBy::cssSelector('#items')); sleep(3);
foreach ($itemsContainer as $item) { $itemsContainer = $driver->findElements(WebDriverBy::cssSelector('#items'));
$albumContainers = $item->findElements(WebDriverBy::cssSelector('.ytmusic-grid-renderer')); foreach ($itemsContainer as $item) {
if ($albumContainers) { $albumContainers = $item->findElements(WebDriverBy::cssSelector('.ytmusic-grid-renderer'));
foreach ($albumContainers as $albumContainer) { if ($albumContainers) {
$response += 1; foreach ($albumContainers as $albumContainer) {
$albumLink = $albumContainer->findElement(WebDriverBy::cssSelector('a')); $response += 1;
$albumHref = $albumLink->getAttribute('href'); $albumLink = $albumContainer->findElement(WebDriverBy::cssSelector('a'));
$albumTitle = $albumLink->getAttribute('title'); $albumHref = $albumLink->getAttribute('href');
$albumThumbnail = $albumLink->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src'); $albumTitle = $albumLink->getAttribute('title');
$albumThumbnail = $albumLink->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src');
// Resize image and save to file, provide path to data // Resize image and save to file, provide path to data
$imageUrl = ImageUrl::modifyGoogleImageUrl($albumThumbnail); $imageUrl = ImageUrl::modifyGoogleImageUrl($albumThumbnail);
$imageFileUrl = ImageUrl::save_img_url($imageUrl, 'album'); $imageFileUrl = ImageUrl::save_img_url($imageUrl, 'album');
$data = [ $data = [
'name' => $albumTitle, 'name' => $albumTitle,
'artist_id' => $artist_id->id, 'artist_id' => $artist_id->id,
'thumbnail' => $albumThumbnail, 'thumbnail' => $albumThumbnail,
'url_remote' => $albumHref, 'url_remote' => $albumHref,
'image' => $imageFileUrl, 'image' => $imageFileUrl,
]; ];
$album_id = Album::findOrCreateByName($artist_id, $albumTitle, $data); $album_id = Album::findOrCreateByName($artist_id, $albumTitle, $data);
$album_queue = new AlbumQueue(); $album_queue = new AlbumQueue();
$album_queue->enqueue($album_id); $album_queue->enqueue($album_id);
}
} }
} }
} }
} catch (\Exception $e) {
\Log::warning('Failed to scrape albums: ---------');
\Log::warning($e->getMessage());
} }
return $response; return $response;
} }
} }

@ -45,7 +45,7 @@ class ImageUrl
// Check if the file already exists // Check if the file already exists
$imagePath = $imagesDir . '/' . $filename . '.jpg'; $imagePath = $imagesDir . '/' . $filename . '.jpg';
if (file_exists($imagePath)) { 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 // Download the image from the URL using curl

@ -1,6 +1,7 @@
console.log('Version 1:20:2'); console.log('Version 1:20:2');
const appModal = $('#modalDownloadQueue'); const appModal = $('#modalDownloadQueue');
const loader = $("#loader-wrapper"); const loader = $("#loader-wrapper");
let ArtistTable = {}; // Initialized for ajax reload
function template_artist_result(element) { function template_artist_result(element) {
return ` return `
@ -48,14 +49,13 @@ function proc_notification(icon, title, html) {
function artist_queue_toggle(element) { function artist_queue_toggle(element) {
let self = $(element); let self = $(element);
console.log(self);
console.log(self.data('artist_id'));
let artist_name = self.data('artist_name'); let artist_name = self.data('artist_name');
self.prop('disabled', true) self.prop('disabled', true)
$.ajax({ $.ajax({
url: `/api/queue/artist/${self.data('artist_id')}`, url: `/api/queue/artist/${self.data('artist_id')}`,
success: () => { success: () => {
proc_notification('success', 'Queued Download', `Artist ${artist_name} Queued for Download!`); proc_notification('success', 'Queued Download', `Artist ${artist_name} Queued for Download!`);
ArtistTable.ajax.reload();
}, },
error: (response) => { error: (response) => {
console.log(response); console.log(response);
@ -95,6 +95,7 @@ function bind_action_buttons() {
icon = 'success'; icon = 'success';
let html = construct_artist_result_html(response); let html = construct_artist_result_html(response);
proc_notification(icon, 'Shazam!', html); proc_notification(icon, 'Shazam!', html);
ArtistTable.ajax.reload();
$('#search_bar').val(''); $('#search_bar').val('');
loader.fadeOut(700); loader.fadeOut(700);
}, },
@ -133,11 +134,9 @@ document.addEventListener('alpine:init', () => {
}); });
$(document).ready(function () { $(document).ready(function () {
bind_action_buttons(); bind_action_buttons();
//Datatable for 'Catalog' menu //Datatable for 'Catalog' menu
let ArtistTable = $('#artistsCatalogDatatable').DataTable({ ArtistTable = $('#artistsCatalogDatatable').DataTable({
ajax: '/api/artists', ajax: '/api/artists',
type: 'get', type: 'get',
dataType: 'json', dataType: 'json',
@ -164,9 +163,4 @@ $(document).ready(function () {
} }
], ],
}); });
// Polling for table update
const getArtistTableInterval = setInterval(function () {
ArtistTable.ajax.reload();
}, 5000);
}); });

Loading…
Cancel
Save