Add tests.php
This commit is contained in:
1 parent
d3785ad0db
commit
70dbf4e88b
1 file changed
+331
@@ -0,0 +1,331 @@
|
||||
<?php
|
||||
// Load environmental variables
|
||||
if (file_exists('.env')){
|
||||
$env = parse_ini_file('.env');
|
||||
$genius = $env['GENIUS_API_BASE64'];
|
||||
$debug = $env['DEBUG'] ? true : $env['DEBUG'];
|
||||
$show_err = $env['SHOW_ERRORS'] ? true : $env['SHOW_ERRORS'];
|
||||
} else {
|
||||
die('You forgot to create your .env file!');
|
||||
}
|
||||
|
||||
// Testing file always shows errors
|
||||
$debug = true;
|
||||
$show_err = true;
|
||||
|
||||
// Show errors
|
||||
if ($show_err){
|
||||
ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
}
|
||||
|
||||
// Set header info
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Content-Type: text/event-stream');
|
||||
header('Cache-Control: private, max-age=90');
|
||||
header('Cache-Control: no-cache');
|
||||
|
||||
// Genius URLs
|
||||
$geniusAPI = "https://api.genius.com/search?q=";
|
||||
$geniusID = "https://api.genius.com/songs/";
|
||||
$geniusurl = "https://genius.com/";
|
||||
$bearer = base64_decode($genius);
|
||||
|
||||
// Default variables
|
||||
$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'];
|
||||
$vtitle = (empty($_POST['title'])) ? (empty($_GET['title'])) ? 'n' : $_GET['title'] : $_POST['title'];
|
||||
$vroute = (empty($_POST['route'])) ? (empty($_GET['route'])) ? 'n' : $_GET['route'] : $_POST['route'];
|
||||
if ($vartist === 'n' && $vtitle === 'n'){
|
||||
echo "offline";
|
||||
exit(0);
|
||||
}
|
||||
$json = rawurlencode($vartist . ' - ' . $vtitle);
|
||||
|
||||
// Curl function
|
||||
function curlThis($url, $auth, $header, $returnt, $timeoutc, $timeout, $bearer){
|
||||
$exe = curl_init();
|
||||
curl_setopt($exe, CURLOPT_URL, "$url");
|
||||
if ($auth === 1){
|
||||
curl_setopt($exe, CURLOPT_HTTPHEADER, ["Authorization: Bearer $bearer"]);
|
||||
}
|
||||
curl_setopt($exe, CURLOPT_HEADER, $header);
|
||||
curl_setopt($exe, CURLOPT_RETURNTRANSFER, $returnt);
|
||||
curl_setopt($exe, CURLOPT_CONNECTTIMEOUT, $timeoutc);
|
||||
curl_setopt($exe, CURLOPT_TIMEOUT, $timeout);
|
||||
$raw = curl_exec($exe);
|
||||
curl_close($exe);
|
||||
return $raw;
|
||||
}
|
||||
|
||||
// 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("<svg", "</svg>"),
|
||||
array("<button ", "</button>")
|
||||
);
|
||||
function cleanMe($lyrics, $start, $end){
|
||||
if (strpos($lyrics, $start) !== false && strpos($lyrics, $end) !== false){
|
||||
$start_len = strlen($start);
|
||||
$start_pos = strpos($lyrics, $start);
|
||||
$end_pos = strpos($lyrics, $end);
|
||||
$remove_lyrics = substr($lyrics, $start_pos, $end_pos + $start_len + 1 - $start_pos);
|
||||
return str_replace($remove_lyrics, '', $lyrics);
|
||||
} else {
|
||||
return $lyrics;
|
||||
}
|
||||
}
|
||||
|
||||
// Query Genius API using artist and song title to get URL of lyrics by querying first result
|
||||
$url = $geniusAPI . $json;
|
||||
$json_genius = json_decode(curlThis($url, 1, 0, 1, 5, 4, $bearer));
|
||||
$result = $json_genius->response->hits[0]->result;
|
||||
$scrapeme = $result->url;
|
||||
$returnURL = '<a href="' . $scrapeme . '" target="_blank">Link</a><br><br>';
|
||||
|
||||
//echo $url . "\n\n";
|
||||
//print_r($json_genius) . "\n\n";
|
||||
|
||||
// Scrape lyrics from Genius
|
||||
if ($vroute !== 'n'){
|
||||
$raw3 = curlThis($scrapeme, 0, 0, 1, 5, 4, $bearer);
|
||||
$divsearch = '<div data-lyrics-container="true" class="Lyrics';
|
||||
$divinstum = '<div class="LyricsPlaceholder-';
|
||||
$divend = '<div class="LyricsFooter-';
|
||||
// Clean raw data
|
||||
foreach ($cleanArray as $cleanThis){
|
||||
$clean_start = $cleanThis[0];
|
||||
$clean_end = $cleanThis[1];
|
||||
while (strpos($raw3, $clean_start) !== false){
|
||||
$raw3 = cleanMe($raw3, $clean_start, $clean_end);
|
||||
}
|
||||
}
|
||||
$lyricsraw = explode($divsearch, $raw3);
|
||||
$lyric_para = count($lyricsraw);
|
||||
|
||||
if ($debug){echo "$lyric_para\n\n";}
|
||||
|
||||
// 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 {
|
||||
$lyricsraw = explode($divinstum, $raw3);
|
||||
$lr1 = explode($d, $lyricsraw[3]);
|
||||
}
|
||||
|
||||
// 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 = '';
|
||||
}
|
||||
|
||||
// Replace all href's with full link to Genius
|
||||
$lyrics = str_replace('href="/','target="_blank" href="' . $geniusurl, $lyrics);
|
||||
} else {
|
||||
$lyrics = 'N/A';
|
||||
}
|
||||
|
||||
// If routine couldn't find anything, return "unknown"
|
||||
if (!$lyrics || $lyrics === $d){$lyrics = "unknown";}
|
||||
|
||||
// Return data separated with delimeter "|,="
|
||||
if ($debug){
|
||||
echo "$returnURL$d$lyrics";
|
||||
} else {
|
||||
echo "$lyrics";
|
||||
}
|
||||
Reference in new issue
Block a user