Use cron script for updating now playing shit.

This commit is contained in:
flash 2023-06-17 02:16:55 +00:00
parent 1ff5051398
commit a551215418
14 changed files with 204 additions and 121 deletions

View file

Before

Width:  |  Height:  |  Size: 131 KiB

After

Width:  |  Height:  |  Size: 131 KiB

View file

Before

Width:  |  Height:  |  Size: 69 KiB

After

Width:  |  Height:  |  Size: 69 KiB

View file

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View file

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View file

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 100 KiB

View file

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 68 KiB

View file

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

View file

@ -1,29 +1,10 @@
<?php
define('SIG_DEBUG', is_file(__DIR__ . '/../.debug'));
define('SIG_WIDTH', 400);
define('SIG_HEIGHT', 100);
define('SIG_STATIC', '/tmp/sig-img-1HxTH4MQ.png');
define('SIG_AGE', 60);
define('SIG_FOOTER', __DIR__ . '/ex/footer.png');
define('FW_FONT_FAM', __DIR__ . '/ex/Electrolize-Regular.ttf');
define('FW_FONT_SIZE', 26);
define('FW_TEXT', 'flashwave');
define('FA_FONT_FAM', __DIR__ . '/ex/fa-solid-900.ttf');
define('NP_FONT_FAM', __DIR__ . '/ex/NotoSansJP-Regular.otf');
define('NP_FONT_SIZE', 13);
define('NP_FONT_SIZE_SMALL', 9.5);
define('NP_FONT_SIZE_ICON', 17);
define('NP_FILE', '/tmp/sig-np-NGowpEMJ.json');
define('NP_URL', 'https://now.flash.moe/get.php?u=flashwave_');
define('NP_COVER', '/tmp/sig-cover-h1coc2SW.png');
define('NP_COVER_URL', '/tmp/sig-cover-url-9vXPUgCi.txt');
define('NP_COVER_URL_DEFAULT', 'https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png');
define('NP_COVER_DEFAULT', __DIR__ . '/ex/no-cover.png');
define('NP_COVER_PAD', 5);
define('NP_COVER_FRAME', 1);
define('NP_COVER_RES', SIG_HEIGHT - (NP_COVER_PAD * 2));
define('BG_PATH', __DIR__ . '/bg');
define('BG_IMAGE', 'bg6.png');
require __DIR__ . '/../sig.php';
if(is_file(NP_INFO)) {
$nowPlaying = true;
$npInfo = unserialize(file_get_contents(NP_INFO));
} else $nowPlaying = false;
$reqMethod = $_SERVER['REQUEST_METHOD'];
$reqPath = '/' . trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
@ -36,12 +17,7 @@ if($reqMethod !== 'HEAD' && $reqMethod !== 'GET') {
if($reqPath === '/np' || $reqPath === '/redir.php') {
http_response_code(302);
$np = json_decode(file_get_contents(NP_FILE));
if(!empty($np[0]->nowplaying) && !empty($np[0]->url))
header('Location: ' . $np[0]->url);
else
header('Location: https://flash.moe');
header('Location: ' . ($nowPlaying ? $npInfo->url : 'https://flash.moe'));
return;
}
@ -56,111 +32,82 @@ if(in_array($reqPath, [
'/album.png', '/album1.png', '/album2.png',
'/body.png', '/body1.png', '/body2.png',
])) {
if(!SIG_DEBUG && is_file(SIG_STATIC) && time() - filemtime(SIG_STATIC) < SIG_AGE) {
header('X-Sig-State: cache');
$imagick = new Imagick(SIG_STATIC);
} else {
header('X-Sig-State: fresh');
$ip_000 = new ImagickPixel('#000');
$ip_444 = new ImagickPixel('#444');
$ip_fff = new ImagickPixel('#fff');
$ip_000 = new ImagickPixel('#000');
$ip_444 = new ImagickPixel('#444');
$ip_fff = new ImagickPixel('#fff');
$npRaw = file_get_contents(NP_URL);
$np = json_decode($npRaw);
$imagick = new Imagick;
$imagick->newImage(SIG_WIDTH, SIG_HEIGHT, $ip_000, 'png');
if(is_array($np)) {
file_put_contents(NP_FILE, $npRaw);
$background = new Imagick(BG_PATH . DIRECTORY_SEPARATOR . BG_IMAGE);
$imagick->compositeImage($background, Imagick::COMPOSITE_COPY, 0, 0);
$background->destroy();
if(empty($np[0]->images->large) || $np[0]->images->large === NP_COVER_URL_DEFAULT) {
unlink(NP_COVER);
file_put_contents(NP_COVER_URL, '');
} elseif(!empty($np[0]->images->large) && $np[0]->images->large !== file_get_contents(NP_COVER_URL)) {
unlink(NP_COVER);
file_put_contents(NP_COVER_URL, $np[0]->images->large);
file_put_contents(NP_COVER, file_get_contents($np[0]->images->large));
}
}
$footer = new Imagick(SIG_FOOTER);
$footerHeight = $footer->getImageHeight() - ($nowPlaying ? 0 : 10);
if(empty($np))
$np = json_decode(file_get_contents(NP_FILE));
$imagick->compositeImage($footer, Imagick::COMPOSITE_MATHEMATICS, 0, SIG_HEIGHT - $footerHeight);
$footer->destroy();
$nowPlaying = is_array($np) && !empty($np[0]->nowplaying);
$imagickDraw = new ImagickDraw;
$imagickDraw->setFillColor($ip_444);
$imagickDraw->setFont(FW_FONT_FAM);
$imagickDraw->setFontSize(FW_FONT_SIZE);
$imagick = new Imagick;
$imagick->newImage(SIG_WIDTH, SIG_HEIGHT, $ip_000, 'png');
$hasCover = $nowPlaying && is_file(NP_COVER_IMG);
$background = new Imagick(BG_PATH . DIRECTORY_SEPARATOR . BG_IMAGE);
$imagick->compositeImage($background, Imagick::COMPOSITE_COPY, 0, 0);
$background->destroy();
$footer = new Imagick(SIG_FOOTER);
$footerHeight = $footer->getImageHeight() - ($nowPlaying ? 0 : 10);
$imagick->compositeImage($footer, Imagick::COMPOSITE_MATHEMATICS, 0, SIG_HEIGHT - $footerHeight);
$footer->destroy();
$imagickDraw = new ImagickDraw;
$imagickDraw->setFillColor($ip_444);
$imagickDraw->setFont(FW_FONT_FAM);
$imagickDraw->setFontSize(FW_FONT_SIZE);
$hasCover = $nowPlaying && is_file(NP_COVER);
if($hasCover) {
$imagickDraw->rectangle(
SIG_WIDTH - (NP_COVER_RES + NP_COVER_PAD) - NP_COVER_FRAME,
NP_COVER_PAD - NP_COVER_FRAME,
SIG_WIDTH - (NP_COVER_RES + NP_COVER_PAD) + NP_COVER_RES,
NP_COVER_PAD + NP_COVER_RES,
);
$imagick->drawImage($imagickDraw);
$npCover = new Imagick(NP_COVER);
$npCover->resizeImage(NP_COVER_RES, NP_COVER_RES, Imagick::FILTER_CATROM, 0.9, true);
$imagick->compositeImage($npCover, Imagick::COMPOSITE_COPY, SIG_WIDTH - (NP_COVER_RES + NP_COVER_PAD), NP_COVER_PAD);
$npCover->destroy();
}
$imagickDraw->setFillColor(BG_IMAGE === 'bg4.png' ? $ip_000 : $ip_fff);
$imagickLogo = new Imagick();
$imagickLogo->newImage($imagick->getImageWidth(), $imagick->getImageHeight(), new ImagickPixel('none'), 'png');
$imagickLogo->annotateImage(
$imagickDraw,
SIG_WIDTH - (4.8 * FW_FONT_SIZE) - ($hasCover ? (NP_COVER_RES + NP_COVER_PAD + NP_COVER_FRAME) : 0),
SIG_HEIGHT - ($nowPlaying ? 37 : 27),
0,
FW_TEXT
if($hasCover) {
$imagickDraw->rectangle(
SIG_WIDTH - (NP_COVER_RES + NP_COVER_PAD) - NP_COVER_FRAME,
NP_COVER_PAD - NP_COVER_FRAME,
SIG_WIDTH - (NP_COVER_RES + NP_COVER_PAD) + NP_COVER_RES,
NP_COVER_PAD + NP_COVER_RES,
);
$imagick->drawImage($imagickDraw);
$imagickShadow = clone $imagickLogo;
$imagickShadow->shadowImage(.8, 5, 0, 0);
$imagickShadow->compositeImage($imagickLogo, Imagick::COMPOSITE_OVER, 4, 2);
$imagickLogo->destroy();
$npCover = new Imagick(NP_COVER_IMG);
$npCover->resizeImage(NP_COVER_RES, NP_COVER_RES, Imagick::FILTER_CATROM, 0.9, true);
$imagick->compositeImage($npCover, Imagick::COMPOSITE_COPY, SIG_WIDTH - (NP_COVER_RES + NP_COVER_PAD), NP_COVER_PAD);
$npCover->destroy();
}
$imagick->compositeImage($imagickShadow, Imagick::COMPOSITE_OVER, -4, -2);
$imagickShadow->destroy();
$imagickDraw->setFillColor(BG_IMAGE === 'bg4.png' ? $ip_000 : $ip_fff);
$imagickDraw->setFillColor($ip_fff);
$imagickLogo = new Imagick();
$imagickLogo->newImage($imagick->getImageWidth(), $imagick->getImageHeight(), new ImagickPixel('none'), 'png');
$imagickLogo->annotateImage(
$imagickDraw,
SIG_WIDTH - (4.8 * FW_FONT_SIZE) - ($hasCover ? (NP_COVER_RES + NP_COVER_PAD + NP_COVER_FRAME) : 0),
SIG_HEIGHT - ($nowPlaying ? 37 : 27),
0,
FW_TEXT
);
if($nowPlaying) {
$imagickDraw->setFont(FA_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE_ICON);
$imagick->annotateImage($imagickDraw, 8, SIG_HEIGHT - 9, 0, "\u{f001}");
$imagickShadow = clone $imagickLogo;
$imagickShadow->shadowImage(.8, 5, 0, 0);
$imagickShadow->compositeImage($imagickLogo, Imagick::COMPOSITE_OVER, 4, 2);
$imagickLogo->destroy();
$imagickDraw->setFont(NP_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE);
$imagick->annotateImage($imagickDraw, 32, SIG_HEIGHT - 5, 0, $np[0]->name ?? '');
$imagick->compositeImage($imagickShadow, Imagick::COMPOSITE_OVER, -4, -2);
$imagickShadow->destroy();
$imagickDraw->setFontSize(NP_FONT_SIZE_SMALL);
$imagick->annotateImage($imagickDraw, 32, SIG_HEIGHT - 20, 0, $np[0]->artist->name ?? '');
} else {
$imagickDraw->setFont(NP_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE);
$imagick->annotateImage($imagickDraw, 10, SIG_HEIGHT - 5, 0, "If it ain't broke, I'll break it");
}
$imagickDraw->setFillColor($ip_fff);
file_put_contents(SIG_STATIC, $imagick->getImageBlob());
if($nowPlaying) {
$imagickDraw->setFont(FA_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE_ICON);
$imagick->annotateImage($imagickDraw, 8, SIG_HEIGHT - 9, 0, "\u{f001}");
$imagickDraw->setFont(NP_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE);
$imagick->annotateImage($imagickDraw, 32, SIG_HEIGHT - 5, 0, $npInfo->title);
$imagickDraw->setFontSize(NP_FONT_SIZE_SMALL);
$imagick->annotateImage($imagickDraw, 32, SIG_HEIGHT - 20, 0, $npInfo->artist);
} else {
$imagickDraw->setFont(NP_FONT_FAM);
$imagickDraw->setFontSize(NP_FONT_SIZE);
$imagick->annotateImage($imagickDraw, 10, SIG_HEIGHT - 5, 0, "If it ain't broke, I'll break it");
}
$fileName = pathinfo($reqPath, PATHINFO_FILENAME);

37
sig.php Normal file
View file

@ -0,0 +1,37 @@
<?php
define('SIG_DEBUG', is_file(__DIR__ . '/.debug'));
define('SIG_ASSETS', __DIR__ . '/assets');
define('SIG_WIDTH', 400);
define('SIG_HEIGHT', 100);
define('SIG_AGE', 60);
define('SIG_FOOTER', SIG_ASSETS . '/img/footer.png');
define('FW_FONT_FAM', SIG_ASSETS . '/fonts/Electrolize-Regular.ttf');
define('FW_FONT_SIZE', 26);
define('FW_TEXT', 'flashwave');
define('FA_FONT_FAM', SIG_ASSETS . '/fonts/fa-solid-900.ttf');
define('BG_PATH', SIG_ASSETS . '/bgs');
define('BG_IMAGE', 'bg6.png');
define('NP_FONT_FAM', SIG_ASSETS . '/fonts/NotoSansJP-Regular.otf');
define('NP_FONT_SIZE', 13.8);
define('NP_FONT_SIZE_SMALL', 9.6);
define('NP_FONT_SIZE_ICON', 17);
define('NP_URL', 'https://now.flash.moe/get.php?u=flashwave_');
define('NP_INFO', '/tmp/sig-np-h5OPrgyk.json');
define('NP_COVER_IMG', '/tmp/sig-cover-EhhaGACQ.png');
define('NP_COVER_DEFAULT', '2a96cbd8b46e442fc41c2b86b821562f.png');
define('NP_COVER_PAD', 5);
define('NP_COVER_FRAME', 1);
define('NP_COVER_RES', SIG_HEIGHT - (NP_COVER_PAD * 2));
if(SIG_DEBUG) {
ini_set('display_errors', 'on');
error_reporting(-1);
} else {
ini_set('display_errors', 'off');
error_reporting(0);
}

99
tools/update-np Executable file
View file

@ -0,0 +1,99 @@
#!/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!');