|
|
|
@ -13,40 +13,41 @@ use Facebook\WebDriver\WebDriverBy;
|
|
|
|
|
|
|
|
|
|
|
|
class SearchController extends Controller
|
|
|
|
class SearchController extends Controller
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
protected function getArtist($driver)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$response = [];
|
|
|
|
|
|
|
|
$artistContainer = $driver->findElement(WebDriverBy::cssSelector('.main-card-content-container'));
|
|
|
|
|
|
|
|
$artistThumbnail = $artistContainer->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src');
|
|
|
|
|
|
|
|
$artistLink = $artistContainer->findElements(WebDriverBy::cssSelector('a'));
|
|
|
|
|
|
|
|
$artistHref = $artistLink[0]->getAttribute('href');
|
|
|
|
|
|
|
|
$artistName = $artistLink[0]->getAttribute('title');
|
|
|
|
|
|
|
|
|
|
|
|
protected static function buildResponse($artist_id) {
|
|
|
|
if ($artistHref && $artistThumbnail && $artistName) {
|
|
|
|
return [
|
|
|
|
$existingArtist = Artist::findByName($artistName)->first();
|
|
|
|
'name' => $artist_id->name,
|
|
|
|
|
|
|
|
'url_remote' => $artist_id->url_remote,
|
|
|
|
|
|
|
|
'thumbnail' => $artist_id->thumbnail,
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
if (!$existingArtist) {
|
|
|
|
{
|
|
|
|
$artist_id = new Artist();
|
|
|
|
$host = 'http://selenium-hub:4444';
|
|
|
|
$artist_id->name = $artistName;
|
|
|
|
$capabilities = DesiredCapabilities::chrome();
|
|
|
|
$artist_id->thumbnail = $artistThumbnail;
|
|
|
|
$chromeOptions = new ChromeOptions();
|
|
|
|
$artist_id->url_remote = $artistHref;
|
|
|
|
// TODO: Add '--headless' back in to arguments
|
|
|
|
$artist_id->save();
|
|
|
|
$chromeOptions->addArguments(['--no-sandbox', '--disable-dev-shm-usage']);
|
|
|
|
$response[] = $this->buildResponse($artist_id);
|
|
|
|
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);
|
|
|
|
} elseif (!$existingArtist->selected) {
|
|
|
|
$driver = RemoteWebDriver::create($host, $capabilities);
|
|
|
|
$this->buildResponse($existingArtist);
|
|
|
|
$driver->manage()->window()->maximize();
|
|
|
|
}
|
|
|
|
return $driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function search_artist(Request $request, string $artist)
|
|
|
|
protected function getArtists($driver)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$response = [];
|
|
|
|
$response = [];
|
|
|
|
$url = 'https://music.youtube.com/search?q=' . str_replace(' ', '+', $artist);
|
|
|
|
|
|
|
|
$driver = $this->setUp();
|
|
|
|
|
|
|
|
$driver->get($url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Click the artist button to force a "structure" of results
|
|
|
|
// Click the artist button to force a "structure" of results
|
|
|
|
$artistBtnXpath = '//a[@title="Show artist results"]';
|
|
|
|
$artistBtnXpath = '//a[@title="Show artist results"]';
|
|
|
|
$driver->wait(10, 500)->until(
|
|
|
|
$driver->wait(10, 500)->until(
|
|
|
|
WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::xpath($artistBtnXpath))
|
|
|
|
WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::xpath($artistBtnXpath))
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$driver->findElement(WebDriverBy::xpath($artistBtnXpath))->click();
|
|
|
|
$driver->findElement(WebDriverBy::xpath($artistBtnXpath))->click();
|
|
|
|
// Youtube has multiple elements with the same ID (Naughty!). We will give a reasonable analog time to render.
|
|
|
|
// Youtube has multiple elements with the same ID (Naughty!). We will give a reasonable analog time to render.
|
|
|
|
sleep(5);
|
|
|
|
sleep(5);
|
|
|
|
@ -78,20 +79,20 @@ class SearchController extends Controller
|
|
|
|
$artist_id->thumbnail = $artistThumbnail;
|
|
|
|
$artist_id->thumbnail = $artistThumbnail;
|
|
|
|
$artist_id->url_remote = $artistHref;
|
|
|
|
$artist_id->url_remote = $artistHref;
|
|
|
|
$artist_id->save();
|
|
|
|
$artist_id->save();
|
|
|
|
|
|
|
|
\Log::info('New Artist added: ' . $artist_id->name);
|
|
|
|
$response += [$this->buildResponse($artist_id)];
|
|
|
|
$response[] = $this->buildResponse($artist_id);
|
|
|
|
} elseif (!$existingArtist->selected) {
|
|
|
|
} elseif ($existingArtist->exists() && !$existingArtist->selected) {
|
|
|
|
// Send the unselected artists back to client as suggestions
|
|
|
|
// Send the unselected artists back to client as suggestions
|
|
|
|
$response += [$this->buildResponse($existingArtist)];
|
|
|
|
\Log::info('Artist already found' . $existingArtist->name);
|
|
|
|
|
|
|
|
$response[] = $this->buildResponse($existingArtist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Limit the results, there are alot of them
|
|
|
|
// Limit the results, there are alot of them
|
|
|
|
if($resultCap <= $resultIndex) {
|
|
|
|
if ($resultCap <= $resultIndex) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// There are 4 div#contents returned, one empty and 3 with duplicated info
|
|
|
|
// There are 4 div#contents returned, one empty and 3 with duplicated info
|
|
|
|
if ($divCount === 1) {
|
|
|
|
if ($divCount === 1) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
@ -99,7 +100,48 @@ class SearchController extends Controller
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static function buildResponse($artist_id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
'name' => $artist_id->name,
|
|
|
|
|
|
|
|
'url_remote' => $artist_id->url_remote,
|
|
|
|
|
|
|
|
'thumbnail' => $artist_id->thumbnail,
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function search_artist(Request $request, string $artist)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$response = [];
|
|
|
|
|
|
|
|
$url = 'https://music.youtube.com/search?q=' . str_replace(' ', '+', $artist);
|
|
|
|
|
|
|
|
$driver = $this->setUp();
|
|
|
|
|
|
|
|
$driver->get($url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add handling for no artist button; Some artists searches don't have this option (Ex The Black Dahlia Murder)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$response = $this->getArtists($driver);
|
|
|
|
|
|
|
|
} catch (\Exception) {
|
|
|
|
|
|
|
|
\Log::warning('Could not get list of artists, attempting to get single artist card..');
|
|
|
|
|
|
|
|
$response = $this->getArtist($driver);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
$driver->quit();
|
|
|
|
$driver->quit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response()->json($response);
|
|
|
|
return response()->json($response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|