From a55121541833d392953dd7ab8cb95da5219bc579 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 17 Jun 2023 02:16:55 +0000 Subject: [PATCH] Use cron script for updating now playing shit. --- {public/bg => assets/bgs}/bg1.png | Bin {public/bg => assets/bgs}/bg2.png | Bin {public/bg => assets/bgs}/bg3.png | Bin {public/bg => assets/bgs}/bg4.png | Bin {public/bg => assets/bgs}/bg5.png | Bin {public/bg => assets/bgs}/bg6.png | Bin .../fonts}/Electrolize-Regular.ttf | Bin .../fonts}/NotoSansJP-Regular.otf | Bin {public/ex => assets/fonts}/fa-solid-900.ttf | Bin {public/ex => assets/img}/footer.png | Bin public/ex/no-cover.png | Bin 4306 -> 0 bytes public/index.php | 189 +++++++----------- sig.php | 37 ++++ tools/update-np | 99 +++++++++ 14 files changed, 204 insertions(+), 121 deletions(-) rename {public/bg => assets/bgs}/bg1.png (100%) rename {public/bg => assets/bgs}/bg2.png (100%) rename {public/bg => assets/bgs}/bg3.png (100%) rename {public/bg => assets/bgs}/bg4.png (100%) rename {public/bg => assets/bgs}/bg5.png (100%) rename {public/bg => assets/bgs}/bg6.png (100%) rename {public/ex => assets/fonts}/Electrolize-Regular.ttf (100%) rename {public/ex => assets/fonts}/NotoSansJP-Regular.otf (100%) rename {public/ex => assets/fonts}/fa-solid-900.ttf (100%) rename {public/ex => assets/img}/footer.png (100%) delete mode 100644 public/ex/no-cover.png create mode 100644 sig.php create mode 100755 tools/update-np diff --git a/public/bg/bg1.png b/assets/bgs/bg1.png similarity index 100% rename from public/bg/bg1.png rename to assets/bgs/bg1.png diff --git a/public/bg/bg2.png b/assets/bgs/bg2.png similarity index 100% rename from public/bg/bg2.png rename to assets/bgs/bg2.png diff --git a/public/bg/bg3.png b/assets/bgs/bg3.png similarity index 100% rename from public/bg/bg3.png rename to assets/bgs/bg3.png diff --git a/public/bg/bg4.png b/assets/bgs/bg4.png similarity index 100% rename from public/bg/bg4.png rename to assets/bgs/bg4.png diff --git a/public/bg/bg5.png b/assets/bgs/bg5.png similarity index 100% rename from public/bg/bg5.png rename to assets/bgs/bg5.png diff --git a/public/bg/bg6.png b/assets/bgs/bg6.png similarity index 100% rename from public/bg/bg6.png rename to assets/bgs/bg6.png diff --git a/public/ex/Electrolize-Regular.ttf b/assets/fonts/Electrolize-Regular.ttf similarity index 100% rename from public/ex/Electrolize-Regular.ttf rename to assets/fonts/Electrolize-Regular.ttf diff --git a/public/ex/NotoSansJP-Regular.otf b/assets/fonts/NotoSansJP-Regular.otf similarity index 100% rename from public/ex/NotoSansJP-Regular.otf rename to assets/fonts/NotoSansJP-Regular.otf diff --git a/public/ex/fa-solid-900.ttf b/assets/fonts/fa-solid-900.ttf similarity index 100% rename from public/ex/fa-solid-900.ttf rename to assets/fonts/fa-solid-900.ttf diff --git a/public/ex/footer.png b/assets/img/footer.png similarity index 100% rename from public/ex/footer.png rename to assets/img/footer.png diff --git a/public/ex/no-cover.png b/public/ex/no-cover.png deleted file mode 100644 index 801761a6ed5e8faee1977c51eea036f4da6d6307..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4306 zcmbtYdt8#~wzeFH(owr;SYAg@S7-Q)!oDpSiYVohNgYI*z2yP_suI3HP^*=IL%e!qSA!S}x3TF-jcde^(2 ze>k?s$5U5(i?)V_hOXB)q`ev%s}$&y5a7oO19&txLxT{?I>@l_ zil&7#_A+Rp39wm@vuL=u#mCU-kqp4%AR|12 ziMJfDlUQ0rgyJm&(cZS+F+|3ph;R68h9BQ&ADtgb$AwzDxmviyJ3$1|41i`4A05Ty zIK|^F-|;#@d$k#9Y4HvMMB*)1iVE`HV?kuG85U@Sy$#(Kg|fh45GV&U27~(A!p;_D zhqQ&h7#oy>6B^@WhqZYBv4pa*L&Kc*l04q$g5L0!hX5eP35n$KcnF?7g2fI;qHs8z zn#0b{213|y5|{uj-iFDs`oKV9aOmua7$Abhv`{nB4zjoa-V&;GUbdtl*0bL3$io~LGqZv%Vi-fm?Jc!VUP$vQo=Wg$4@9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*hC739m0@!fa6zabn8 zqb4TXoz<(OS!q(w#p)$c+OeDL2PXC|-_diHHBRH?mJoe6F~#A1F& zt`~w2pC0tM7Tk2UGBo&dUvOCTSnqE<&7I)FEf%xyK_J_yf_si_US7H6>LZ;KP+5Hj zox_fi*K#*1h<()<_A@5Y3WU8Vi`H{W^(o#6ex_aoA9wE!zh8b2{Jni8W`*Ei?eEAx k@PE?%r%C^?rn*`4+chl$y|rb=>VFx$$UY>Y`*&&o4eD9ae*gdg diff --git a/public/index.php b/public/index.php index b67276b..d527ed3 100644 --- a/public/index.php +++ b/public/index.php @@ -1,29 +1,10 @@ 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); diff --git a/sig.php b/sig.php new file mode 100644 index 0000000..391fded --- /dev/null +++ b/sig.php @@ -0,0 +1,37 @@ + 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!');