[IMP] php: Artist data being saved to DB

refactor_total
Brett Spaulding 1 year ago
parent 2dfe542904
commit 64379756cc

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Artist;
use Facebook\WebDriver\WebDriverExpectedCondition; use Facebook\WebDriver\WebDriverExpectedCondition;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -13,6 +14,14 @@ use Facebook\WebDriver\WebDriverBy;
class SearchController extends Controller class SearchController extends Controller
{ {
protected static function buildResponse($artist_id) {
return [
'name' => $artist_id->name,
'url_remote' => $artist_id->url_remote,
'thumbnail' => $artist_id->thumbnail,
];
}
protected function setUp() protected function setUp()
{ {
$host = 'http://selenium-hub:4444'; $host = 'http://selenium-hub:4444';
@ -28,13 +37,8 @@ class SearchController extends Controller
public function search_artist(Request $request, string $artist) public function search_artist(Request $request, string $artist)
{ {
\Log::info('Getting Artist: ' . $artist); $response = [];
// $url = 'https://example.com';
$url = 'https://music.youtube.com/search?q=' . str_replace(' ', '+', $artist); $url = 'https://music.youtube.com/search?q=' . str_replace(' ', '+', $artist);
\Log::info('Search URL: ' . $url);
\Log::info('=======================================');
// the URL to the local Selenium Server
$driver = $this->setUp(); $driver = $this->setUp();
$driver->get($url); $driver->get($url);
@ -53,7 +57,6 @@ class SearchController extends Controller
$divCount += 1; $divCount += 1;
$artists = $content->findElements(WebDriverBy::xpath('//ytmusic-responsive-list-item-renderer')); $artists = $content->findElements(WebDriverBy::xpath('//ytmusic-responsive-list-item-renderer'));
if ($artists) { if ($artists) {
$resultCap = 6; $resultCap = 6;
$resultIndex = 0; $resultIndex = 0;
@ -62,33 +65,41 @@ class SearchController extends Controller
$hasText = $artist->getText(); $hasText = $artist->getText();
if ($hasText) { if ($hasText) {
$resultIndex += 1; $resultIndex += 1;
\Log::info('===================================================================================================================================');
\Log::info('===================================================================================================================================');
// \Log::info($artist->getDomProperty('innerHTML'));
// Artist Data Targeting
$artistThumbnail = $artist->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src'); $artistThumbnail = $artist->findElement(WebDriverBy::cssSelector('img'))->getAttribute('src');
$artistLink = $artist->findElements(WebDriverBy::cssSelector('a')); $artistLink = $artist->findElements(WebDriverBy::cssSelector('a'));
$artistHref = $artistLink[0]->getAttribute('href'); $artistHref = $artistLink[0]->getAttribute('href');
$artistName = $artistLink[0]->getAttribute('aria-label'); $artistName = $artistLink[0]->getAttribute('aria-label');
\Log::info($artistName . ': ' . $artistHref); $existingArtist = Artist::findByName($artistName)->first();
\Log::info($artistThumbnail); if (!$existingArtist) {
$artist_id = new Artist();
$artist_id->name = $artistName;
$artist_id->thumbnail = $artistThumbnail;
$artist_id->url_remote = $artistHref;
$artist_id->save();
$response += [$this->buildResponse($artist_id)];
} elseif (!$existingArtist->selected) {
// Send the unselected artists back to client as suggestions
$response += [$this->buildResponse($existingArtist)];
}
// 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
if ($divCount === 1) { if ($divCount === 1) {
break; break;
} }
} }
} }
$driver->quit(); $driver->quit();
return response()->json($response);
} }
} }

@ -8,4 +8,10 @@ use Illuminate\Database\Eloquent\Model;
class Artist extends Model class Artist extends Model
{ {
use HasFactory; use HasFactory;
public static function findByName($name)
{
return self::where('name', '=', $name)->get();
}
} }

Loading…
Cancel
Save