100 lines
2.7 KiB
Text
100 lines
2.7 KiB
Text
|
#!/usr/bin/env php
|
||
|
<?php
|
||
|
require __DIR__ . '/../sig.php';
|
||
|
|
||
|
define('SIG_REFRESH_START', microtime(true));
|
||
|
|
||
|
function sig_log(string $str) {
|
||
|
$time = round((microtime(true) - SIG_REFRESH_START) * 1000);
|
||
|
printf('[%d] %s%s', $time, $str, PHP_EOL);
|
||
|
}
|
||
|
|
||
|
function sig_web_get($url) {
|
||
|
$res = '';
|
||
|
$req = curl_init($url);
|
||
|
try {
|
||
|
curl_setopt_array($req, [
|
||
|
CURLOPT_AUTOREFERER => true,
|
||
|
CURLOPT_FAILONERROR => true,
|
||
|
CURLOPT_FOLLOWLOCATION => true,
|
||
|
CURLOPT_HEADER => false,
|
||
|
CURLOPT_RETURNTRANSFER => true,
|
||
|
CURLOPT_TCP_FASTOPEN => true,
|
||
|
CURLOPT_CONNECTTIMEOUT => 5,
|
||
|
CURLOPT_MAXREDIRS => 5,
|
||
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,
|
||
|
CURLOPT_TIMEOUT => 10,
|
||
|
CURLOPT_USERAGENT => 'flash.moe signature',
|
||
|
]);
|
||
|
$res = curl_exec($req);
|
||
|
} finally {
|
||
|
curl_close($req);
|
||
|
}
|
||
|
|
||
|
return $res;
|
||
|
}
|
||
|
|
||
|
sig_log('Starting Now Playing refresh...');
|
||
|
|
||
|
$npRaw = json_decode(sig_web_get(NP_URL));
|
||
|
|
||
|
sig_log('Downloaded NP info.');
|
||
|
|
||
|
if(empty($npRaw[0]->nowplaying)) {
|
||
|
sig_log('Not currently listening to anything.');
|
||
|
|
||
|
if(is_file(NP_INFO)) {
|
||
|
sig_log('Deleting NP_INFO file...');
|
||
|
unlink(NP_INFO);
|
||
|
}
|
||
|
|
||
|
if(is_file(NP_COVER_IMG)) {
|
||
|
sig_log('Deleting NP_COVER_IMG file...');
|
||
|
unlink(NP_COVER_IMG);
|
||
|
}
|
||
|
} else {
|
||
|
sig_log('Currently listening to something!');
|
||
|
|
||
|
if(is_file(NP_INFO)) {
|
||
|
sig_log('Fetching previous URL from current NP_INFO file...');
|
||
|
$npInfo = unserialize(file_get_contents(NP_INFO));
|
||
|
$npLastUrl = $npInfo->url;
|
||
|
} else $npLastUrl = '';
|
||
|
|
||
|
$npInfo = new stdClass;
|
||
|
$npInfo->v = 1;
|
||
|
$npInfo->title = $npRaw[0]->name;
|
||
|
$npInfo->artist = $npRaw[0]->artist->name;
|
||
|
$npInfo->url = $npRaw[0]->url;
|
||
|
|
||
|
$sameAsLast = $npLastUrl === $npInfo->url;
|
||
|
if(!$sameAsLast) {
|
||
|
sig_log('Writing new NP_INFO file...');
|
||
|
file_put_contents(NP_INFO, serialize($npInfo));
|
||
|
}
|
||
|
|
||
|
$hasCover = basename($npRaw[0]->images->large) !== NP_COVER_DEFAULT;
|
||
|
|
||
|
if($hasCover) {
|
||
|
sig_log('Current song has a cover image.');
|
||
|
|
||
|
if($sameAsLast && is_file(NP_COVER_IMG)) {
|
||
|
sig_log('Song URL is identical to previous run, leaving existing cover image.');
|
||
|
} else {
|
||
|
sig_log('Downloading cover image...');
|
||
|
$npCover = sig_web_get($npRaw[0]->images->large);
|
||
|
sig_log('Writing cover image...');
|
||
|
file_put_contents(NP_COVER_IMG, $npCover);
|
||
|
}
|
||
|
} else {
|
||
|
sig_log('Current song does not have a cover image.');
|
||
|
|
||
|
if(is_file(NP_COVER_IMG)) {
|
||
|
sig_log('Deleting NP_COVER_IMG file...');
|
||
|
unlink(NP_COVER_IMG);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sig_log('Done!');
|