From e09351b964715f38a9005792ae7235ec64e7f0fe Mon Sep 17 00:00:00 2001 From: Brett Spaulding Date: Sat, 10 Aug 2024 18:50:45 -0400 Subject: [PATCH] [IMP] php: Return restults viaually to client --- .../app/Http/Controllers/SearchController.php | 43 ++++++++-------- php/src/app/Models/Artist.php | 9 +--- php/src/app/Models/WebDriver.php | 23 +++++++++ php/src/public/js/app.js | 50 +++++++++++++++---- 4 files changed, 87 insertions(+), 38 deletions(-) create mode 100644 php/src/app/Models/WebDriver.php diff --git a/php/src/app/Http/Controllers/SearchController.php b/php/src/app/Http/Controllers/SearchController.php index 296be4e..7181976 100644 --- a/php/src/app/Http/Controllers/SearchController.php +++ b/php/src/app/Http/Controllers/SearchController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Artist; +use App\Models\WebDriver; use Facebook\WebDriver\WebDriverExpectedCondition; use Illuminate\Http\Request; @@ -14,8 +15,18 @@ use Facebook\WebDriver\WebDriverBy; class SearchController extends Controller { + /** + * The default Artist data to be returned from this controller. + * + * @return array + */ public $defaultArtistData = ['id', 'name', 'thumbnail', 'url_remote']; + /** + * Fallback scrape option for the youtube music search page; in some cases there are no additional artists available + * + * @return RemoteWebDriver + */ protected function scrapeArtist($driver) { $response = []; @@ -31,9 +42,15 @@ class SearchController extends Controller 'url_remote' => $artistHref, ]; $artist_id = Artist::findOrCreateByName($artistName, $data); - return $artist_id->read($this->defaultArtistData); + return $artist_id->read(); } + /** + * The first scrape that is attempted; this will return the artists and similar artists per youtube so we can return + * the users search with additional suggestions, or a list of suggestions if their exact search isn't found. + * + * @return RemoteWebDriver + */ protected function scrapeArtists($driver) { $response = []; @@ -71,7 +88,7 @@ class SearchController extends Controller 'url_remote' => $artistHref, ]; $artist_id = Artist::findOrCreateByName($artistName, $data); - $response[] = $artist_id->read($this->defaultArtistData); + $response[] = $artist_id->read(); // Limit the results, there are alot of them if ($resultCap <= $resultIndex) { break; @@ -79,32 +96,17 @@ class SearchController extends Controller } } // There are 4 div#contents returned, one empty and 3 with duplicated info - if ($divCount === 1) { - break; - } + break; } } - return $response; - } - protected function setUp() - { - $host = 'http://selenium-hub:4444'; - $capabilities = DesiredCapabilities::chrome(); - $chromeOptions = new ChromeOptions(); - // TODO: Add '--headless' back in to arguments - $chromeOptions->addArguments(['--no-sandbox', '--disable-dev-shm-usage']); - $capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions); - $driver = RemoteWebDriver::create($host, $capabilities); - $driver->manage()->window()->maximize(); - return $driver; + return $response; } public function search_artist(string $artist) { - $response = []; $url = 'https://music.youtube.com/search?q=' . str_replace(' ', '+', $artist); - $driver = $this->setUp(); + $driver = WebDriver::setUp(); $driver->get($url); // Add handling for no artist button; Some artists searches don't have this option (Ex The Black Dahlia Murder) @@ -118,4 +120,5 @@ class SearchController extends Controller } return response()->json($response); } + } diff --git a/php/src/app/Models/Artist.php b/php/src/app/Models/Artist.php index b95e204..3af8d74 100644 --- a/php/src/app/Models/Artist.php +++ b/php/src/app/Models/Artist.php @@ -24,7 +24,6 @@ class Artist extends Model return $artist; } - // TODO: discuss why I can't return Artist or Collection, because it failes either way but says its returning Artist public static function findOrCreateByName(string $name, array $data = []) { $artist = self::findByName($name)->first(); @@ -36,12 +35,8 @@ class Artist extends Model public function read(array $fields = []): array { - $data = $this->toArray(); - // Filter - if ($fields) { - $data = array_intersect_key($data, array_flip(array_keys($fields))); - } - return $data; + // TODO: Add filter for fields if provided + return $this->toArray();; } } diff --git a/php/src/app/Models/WebDriver.php b/php/src/app/Models/WebDriver.php new file mode 100644 index 0000000..d1b7e79 --- /dev/null +++ b/php/src/app/Models/WebDriver.php @@ -0,0 +1,23 @@ +addArguments(['--no-sandbox', '--disable-dev-shm-usage']); + $capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions); + $driver = RemoteWebDriver::create($host, $capabilities); + $driver->manage()->window()->maximize(); + return $driver; + } +} diff --git a/php/src/public/js/app.js b/php/src/public/js/app.js index 4c79b94..fee44e6 100644 --- a/php/src/public/js/app.js +++ b/php/src/public/js/app.js @@ -2,9 +2,37 @@ console.log('Version 1:20:2'); const appModal = $('#modalDownloadQueue'); const loader = $("#loader-wrapper"); -function proc_notification(icon, title, text) { +function construct_artist_result_html(artist_list) { + let html = '

Found Artist!

'; + let index = 0; + artist_list.forEach((element) => { + index += 1; + html += ` +
+
+
+
+ +
+
+

${element.name}

+
+
+
+
+ ` + if (index === 1 && artist_list.length > 1) { + html += '
'; + html += '
Suggested Artists
' + html += '
'; + } + }) + return html +} + +function proc_notification(icon, html, text) { Swal.fire({ - title: title, + html: html, icon: icon, text: text }) @@ -12,15 +40,15 @@ function proc_notification(icon, title, text) { $('#settings_btn').on('click', () => { $('#modalSettings').modal('toggle'); -}) +}); $('#catalog_btn').on('click', () => { $('#modalCatalog').modal('toggle'); -}) +}); $('#queue_btn').on('click', () => { appModal.modal('toggle'); -}) +}); $('#download_btn').on('click', () => { loader.fadeIn(300); @@ -39,7 +67,7 @@ $('#download_btn').on('click', () => { console.log(response); console.log('==========='); icon = 'success'; - title = 'Shazam!'; + title = construct_artist_result_html(response); proc_notification(icon, title, 'Artist found'); $('#search_bar').val(''); loader.fadeOut(700); @@ -59,7 +87,7 @@ $('#download_btn').on('click', () => { } }, 100); -}) +}); document.addEventListener('alpine:init', () => { console.log('Alpine:init'); @@ -71,12 +99,12 @@ document.addEventListener('alpine:init', () => { this.ArtistResults = [] }, - Artists: [], // Rendered in the 'Artists' menu - ArtistResults: [], // Rendered in the - Queue: [], // Rendered in the 'Queue' menu + Artists: [], // Rendered in the 'Artists' modal + ArtistResults: [], // Rendered in the SWAL popup + Queue: [], // Rendered in the 'Queue' modal }); $("#loader-wrapper").fadeOut(900); -}) +});