Loop through array instead of hard coding to 6 results
This commit is contained in:
1 parent
d7ef5fa719
commit
7e93393989
1 file changed
+63
-203
+63
-203
@@ -29,8 +29,10 @@ $geniusurl = "https://genius.com/";
|
||||
$bearer = base64_decode($genius);
|
||||
|
||||
// Default variables
|
||||
$lyrics = '';
|
||||
$scrapeme = '';
|
||||
$d = '<br/>';
|
||||
$array_lyrics = [];
|
||||
$lyrics = '';
|
||||
$scrapeme = '';
|
||||
|
||||
// Get POST and/or GET variables for artist and song title
|
||||
$vartist = (empty($_POST['artist'])) ? (empty($_GET['artist'])) ? 'n' : $_GET['artist'] : $_POST['artist'];
|
||||
@@ -59,13 +61,12 @@ function curlThis($url, $auth, $header, $returnt, $timeoutc, $timeout, $bearer){
|
||||
}
|
||||
|
||||
// Clean up raw page scrapping function
|
||||
$d = '<br/>';
|
||||
$cleanArray = array(
|
||||
array("<head>", "</head>"),
|
||||
array("<noscript>", "</noscript>"),
|
||||
array("<script ", "</script>"),
|
||||
array("<script>", "</script>"),
|
||||
array("<footer ", "</footer>"),
|
||||
//array("<noscript>", "</noscript>"),
|
||||
//array("<script ", "</script>"),
|
||||
//array("<script>", "</script>"),
|
||||
//array("<footer ", "</footer>"),
|
||||
array("<svg", "</svg>"),
|
||||
array("<button ", "</button>")
|
||||
);
|
||||
@@ -94,9 +95,9 @@ $returnURL = '<a href="' . $scrapeme . '" target="_blank">Link</a><br><br>';
|
||||
// Scrape lyrics from Genius
|
||||
if ($vroute !== 'n'){
|
||||
$raw3 = curlThis($scrapeme, 0, 0, 1, 5, 4, $bearer);
|
||||
$divsearch = '<div data-lyrics-container="true" class="Lyrics';
|
||||
$divsearch = '<div data-lyrics-container="true" class="Lyrics__Cont';
|
||||
$divinstum = '<div class="LyricsPlaceholder-';
|
||||
$divend = '<div class="LyricsFooter-';
|
||||
|
||||
// Clean raw data
|
||||
foreach ($cleanArray as $cleanThis){
|
||||
$clean_start = $cleanThis[0];
|
||||
@@ -105,207 +106,66 @@ if ($vroute !== 'n'){
|
||||
$raw3 = cleanMe($raw3, $clean_start, $clean_end);
|
||||
}
|
||||
}
|
||||
|
||||
$lyricsraw = explode($divsearch, $raw3);
|
||||
$lyric_para = count($lyricsraw);
|
||||
|
||||
// Find lyric containers scraped from webpage
|
||||
if ($lyric_para > 6){
|
||||
$lr6 = explode($d, $lyricsraw[6]);
|
||||
}
|
||||
if ($lyric_para > 5){
|
||||
$lr5 = explode($d, $lyricsraw[5]);
|
||||
}
|
||||
if ($lyric_para > 4){
|
||||
$lr4 = explode($d, $lyricsraw[4]);
|
||||
}
|
||||
if ($lyric_para > 3){
|
||||
$lr3 = explode($d, $lyricsraw[3]);
|
||||
}
|
||||
if ($lyric_para > 2){
|
||||
$lr2 = explode($d, $lyricsraw[2]);
|
||||
}
|
||||
if ($lyric_para > 1) {
|
||||
$lr1 = explode($d, $lyricsraw[1]);
|
||||
} else {
|
||||
if ($lyric_para === 1){
|
||||
// If there is only one result, the song probably doesn't have lyrics and needs special attention
|
||||
$lyricsraw = explode($divinstum, $raw3);
|
||||
$lr1 = explode($d, $lyricsraw[3]);
|
||||
}
|
||||
foreach (explode($d, $lyricsraw[3]) as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
array_push($array_lyrics, $c1);
|
||||
} else {
|
||||
array_push($array_lyrics, substr($c1, 0, strpos($c1, '</div')));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For everything else with a count > 1, it follows this style of parsing
|
||||
if ($debug){print_r($lyricsraw) . "\n\n\n\n";}
|
||||
// The first result [0] never has lyrics in it so let's start with the second result
|
||||
$lr = 1;
|
||||
while ($lr <= $lyric_para - 1){
|
||||
if ($lr === 1){
|
||||
// If this is the first time, there is preceeding garbage everywhere
|
||||
// so we must loop through each results looking for our special delimeter $d
|
||||
$last_exp = explode('</div>', $lyricsraw[$lr]);
|
||||
foreach ($last_exp as $c1){
|
||||
if (strpos($c1, $d) !== false){
|
||||
array_push($array_lyrics, $c1);
|
||||
}
|
||||
}
|
||||
} elseif ($lr === $lyric_para - 1){
|
||||
// If this is the last result, we look for the first occurance of '</div>' and
|
||||
// truncate the rest as it is garbage div stuff
|
||||
foreach (explode($d, $lyricsraw[$lr]) as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
array_push($array_lyrics, $c1);
|
||||
} else {
|
||||
array_push($array_lyrics, substr($c1, 0, strpos($c1, '</div')));
|
||||
//$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach (explode($d, $lyricsraw[$lr]) as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
array_push($array_lyrics, $c1);
|
||||
}
|
||||
}
|
||||
}
|
||||
$lr++;
|
||||
} // end while
|
||||
} // end if
|
||||
|
||||
// Join lyric containers scraped from webpage
|
||||
$lyrics = '';
|
||||
if (isset($lr6)){
|
||||
$last_exp = explode('</div>', $lr1[0]);
|
||||
$last = array_key_last($last_exp);
|
||||
if ($debug){print_r($last_exp) . "\n\n";}
|
||||
if ($last){
|
||||
$lyrics = $lyrics . $last_exp[$last] . $d;
|
||||
}
|
||||
foreach ($lr1 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr2 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr3 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr4 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr5 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr6 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
} else {
|
||||
$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (isset($lr5)){
|
||||
$last_exp = explode('</div>', $lr1[0]);
|
||||
$last = array_key_last($last_exp);
|
||||
if ($debug){print_r($last_exp) . "\n\n";}
|
||||
if ($last){
|
||||
$lyrics = $lyrics . $last_exp[$last] . $d;
|
||||
}
|
||||
foreach ($lr1 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr2 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr3 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr4 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr5 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
} else {
|
||||
$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (isset($lr4)){
|
||||
$last_exp = explode('</div>', $lr1[0]);
|
||||
$last = array_key_last($last_exp);
|
||||
if ($debug){print_r($last_exp) . "\n\n";}
|
||||
if ($last){
|
||||
$lyrics = $lyrics . $last_exp[$last] . $d;
|
||||
}
|
||||
foreach ($lr1 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr2 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr3 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr4 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
} else {
|
||||
$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (isset($lr3)){
|
||||
$last_exp = explode('</div>', $lr1[0]);
|
||||
$last = array_key_last($last_exp);
|
||||
if ($debug){print_r($last_exp) . "\n\n";}
|
||||
if ($last){
|
||||
$lyrics = $lyrics . $last_exp[$last] . $d;
|
||||
}
|
||||
foreach ($lr1 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr2 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr3 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
} else {
|
||||
$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (isset($lr2)){
|
||||
$last_exp = explode('</div>', $lr1[0]);
|
||||
$last = array_key_last($last_exp);
|
||||
if ($debug){print_r($last_exp) . "\n\n";}
|
||||
if ($last){
|
||||
$lyrics = $lyrics . $last_exp[$last] . $d;
|
||||
}
|
||||
foreach ($lr1 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
}
|
||||
}
|
||||
foreach ($lr2 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
} else {
|
||||
$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif (isset($lr1)){
|
||||
foreach ($lr1 as $c1){
|
||||
if (substr($c1, 0, 2) !== "__" && substr($c1, 0, 2) !== "</"){
|
||||
if (strpos($c1, '</div>') === false){
|
||||
$lyrics = $lyrics . $c1 . $d;
|
||||
} else {
|
||||
$lyrics = $lyrics . substr($c1, 0, strpos($c1, '</div')) . $d;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
$lyrics = '';
|
||||
foreach ($array_lyrics as $L){
|
||||
$lyrics = $lyrics . $L . $d;
|
||||
}
|
||||
|
||||
// Replace all href's with full link to Genius
|
||||
@@ -319,7 +179,7 @@ if (!$lyrics || $lyrics === $d){$lyrics = "unknown";}
|
||||
|
||||
// Return data separated with delimeter "|,="
|
||||
if ($debug){
|
||||
echo "$returnURL$d$lyrics";
|
||||
echo "$returnURL\n\n$d\n\n$lyrics";
|
||||
} else {
|
||||
echo "$lyrics";
|
||||
}
|
||||
Reference in new issue
Block a user