Files
radio/title.php
T
2025-04-11 14:24:43 -05:00

129 lines
5.0 KiB
PHP

<?php
// Load environmental variables
if (file_exists('.env')){
$env = parse_ini_file('.env');
$genius = $env['GENIUS_API_BASE64'];
$debug = $env['DEBUG'] ? 1 : $env['DEBUG'];
$show_err = $env['SHOW_ERRORS'] ? 1 : $env['SHOW_ERRORS'];
} else {
die('You forgot to create your .env file!');
}
// 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
$random_number = rand(0, 100);
$art = '';
$title = '';
$ftitle = '';
$lyrics = '';
$scrapeme = '';
$album = '';
// 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|,=offline|,=offline|,=offline|,=offline|,=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;
}
// 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;
$songid = $result->id;
$scrapeme = $result->url;
$artart = $result->primary_artist->image_url;
// Scrape lyrics from Genius
$lyrics = 'N/A';
// Get album information from Genius API using songid
$url = $geniusID . $songid;
$get2 = json_decode(curlThis($url, 1, 0, 1, 5, 4, $bearer));
$songInfo = $get2->response->song;
$title = $songInfo->title;
$ftitle = $songInfo->full_title;
$album = $songInfo->album->name;
$art = $songInfo->header_image_url;
// Double check if we have data and try something else if no data yet
if (!$album){$album = $ftitle;}
if (!$title){$title = $result->title;}
if (!$ftitle){$ftitle = $result->full_title;}
if (!$art){$art = $result->header_image_url;}
// Replace default Genius images with cool GIFs
function badArt($arturl){
$badart = ['https://radio.dou.bet/na.jpg','https://images.genius.com/9d53cf21e85632e8a774b470fc1af6d9.960x960x1.jpg','https://images.genius.com/5bb0d83dd9b4f5f97b7cea2e16091892.1000x1000x1.png','https://images.genius.com/4defb2d0b45e544711e5dbabe266a370.1000x1000x1.png',
'https://images.genius.com/e3f28a8c15ae3b11ac9cc7aaef0396c1.512x512x1.jpg','https://images.genius.com/2aa2941e1d8ed0034c2ddc9dd5012af9.1000x1000x1.png','https://assets.genius.com/images/default_cover_image.png',
'https://assets.genius.com/images/default_cover_image.jpg','https://images.genius.com/cc43e9f65bda58676823af326760364a.999x999x1.png'];
$childish = ['https://images.genius.com/2114312d382ad914c2c7b6146fbfb6c2.300x300x1.jpg','https://images.genius.com/90d733d87dcdbe5bb5f89062e6fb381f.500x500x1.jpg','https://images.genius.com/90d733d87dcdbe5bb5f89062e6fb381f.500x500x1.jpg'];
if (!$arturl || in_array($arturl, $badart)){
if ($random_number <= 40){
$art = "https://78.media.tumblr.com/a38fa7ca0f62769471df65b4ce8d177d/tumblr_pogjorskHk1w3y4ilo2_540.gif";
} elseif ($random_number <= 70){
$art = "https://i.imgur.com/PWw7VmH.gif";
} else {
$art = "https://i.pinimg.com/originals/d7/18/9b/d7189b9a574f7c279473847357a91f69.gif";
}
} else {
$art = $arturl;
}
if (in_array($arturl, $childish)){
$art = "https://miro.medium.com/v2/resize:fit:632/1*eswZSNBeUgzlczhy7qA9Nw.gif";
}
return $art;
}
$art = badArt($art);
if ($vartist === 'BAW' && $vtitle === 'CHILDHOOD MEMORIES'){$art = "https://i1.sndcdn.com/artworks-zNyvrtxKfGAu60Yr-e7fGPA-t500x500.jpg";}
// If routine couldn't find anything, return "unknown"
if (!$title){$title = $vtitle;}
if (!$ftitle){$ftitle = "unknown";}
if (!$lyrics){$lyrics = "unknown";}
if (!$scrapeme){$scrapeme = "unknown";}
if(!$album){$album = "unknown";}
// Return data separated with delimeter "|,="
echo "$art|,=$title|,=$ftitle|,=$lyrics|,=$scrapeme|,=$album";