|
|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Facebook\WebDriver\WebDriverExpectedCondition;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
|
|
|
|
@ -12,10 +13,12 @@ use Facebook\WebDriver\WebDriverBy;
|
|
|
|
|
class SearchController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
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);
|
|
|
|
|
@ -25,8 +28,6 @@ class SearchController extends Controller
|
|
|
|
|
|
|
|
|
|
public function search_artist(Request $request, string $artist)
|
|
|
|
|
{
|
|
|
|
|
\Log::info($artist);
|
|
|
|
|
\Log::info($request);
|
|
|
|
|
\Log::info('Getting Artist: ' . $artist);
|
|
|
|
|
// $url = 'https://example.com';
|
|
|
|
|
$url = 'https://music.youtube.com/search?q=' . str_replace(' ', '+', $artist);
|
|
|
|
|
@ -36,12 +37,58 @@ class SearchController extends Controller
|
|
|
|
|
// the URL to the local Selenium Server
|
|
|
|
|
$driver = $this->setUp();
|
|
|
|
|
$driver->get($url);
|
|
|
|
|
$html = $driver->getPageSource();
|
|
|
|
|
|
|
|
|
|
\Log::info($html);
|
|
|
|
|
\Log::info('=========================================');
|
|
|
|
|
// Click the artist button to force a "structure" of results
|
|
|
|
|
$artistBtnXpath = '//a[@title="Show artist results"]';
|
|
|
|
|
$driver->wait(10, 500)->until(
|
|
|
|
|
WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::xpath($artistBtnXpath))
|
|
|
|
|
);
|
|
|
|
|
$driver->findElement(WebDriverBy::xpath($artistBtnXpath))->click();
|
|
|
|
|
// Youtube has multiple elements with the same ID (Naughty!). We will give a reasonable analog time to render.
|
|
|
|
|
sleep(5);
|
|
|
|
|
|
|
|
|
|
$driver->quit();
|
|
|
|
|
$contentDivs = $driver->findElements(WebDriverBy::cssSelector('#contents'));
|
|
|
|
|
$divCount = 0;
|
|
|
|
|
foreach ($contentDivs as $content) {
|
|
|
|
|
$divCount += 1;
|
|
|
|
|
|
|
|
|
|
$artists = $content->findElements(WebDriverBy::xpath('//ytmusic-responsive-list-item-renderer'));
|
|
|
|
|
|
|
|
|
|
if ($artists) {
|
|
|
|
|
$resultCap = 6;
|
|
|
|
|
$resultIndex = 0;
|
|
|
|
|
foreach ($artists as $artist) {
|
|
|
|
|
// There are a bunch of elements with no text in them; just a quick and dirty filter
|
|
|
|
|
$hasText = $artist->getText();
|
|
|
|
|
if ($hasText) {
|
|
|
|
|
$resultIndex += 1;
|
|
|
|
|
\Log::info('===================================================================================================================================');
|
|
|
|
|
\Log::info('===================================================================================================================================');
|
|
|
|
|
// \Log::info($artist->getDomProperty('innerHTML'));
|
|
|
|
|
|
|
|
|
|
// Artist Data Targeting
|
|
|
|
|
$artistThumbnail = $artist->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src');
|
|
|
|
|
$artistLink = $artist->findElements(WebDriverBy::cssSelector('a'));
|
|
|
|
|
$artistHref = $artistLink[0]->getAttribute('href');
|
|
|
|
|
$artistName = $artistLink[0]->getAttribute('aria-label');
|
|
|
|
|
|
|
|
|
|
\Log::info($artistName . ': ' . $artistHref);
|
|
|
|
|
\Log::info($artistThumbnail);
|
|
|
|
|
|
|
|
|
|
if($resultCap <= $resultIndex) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($divCount === 1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$driver->quit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|