feat(*): refactor dynamic vs. static structure and distinctions; make additional elements dynamic
This commit is contained in:
parent
7a0b808f24
commit
13d17e2e51
128 changed files with 982 additions and 959 deletions
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace App\Classes;
|
||||
|
||||
require __DIR__ . "/BaseHandler.php";
|
||||
require __DIR__ . '/../../server/utils/init.php';
|
||||
|
||||
abstract class ApiHandler extends BaseHandler
|
||||
{
|
||||
protected function ensureCliAccess(): void
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace App\Classes;
|
||||
|
||||
require __DIR__ . "/../../vendor/autoload.php";
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
|
|
69
api/Classes/LatestListenHandler.php
Normal file
69
api/Classes/LatestListenHandler.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
class LatestListenHandler extends BaseHandler
|
||||
{
|
||||
protected int $cacheTTL = 60;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->initializeCache();
|
||||
}
|
||||
|
||||
public function handleRequest(): void
|
||||
{
|
||||
$data = $this->getLatestListen();
|
||||
|
||||
if (!$data) {
|
||||
$this->sendResponse(["message" => "No recent tracks found"], 404);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->sendResponse($data);
|
||||
}
|
||||
|
||||
public function getLatestListen(): ?array
|
||||
{
|
||||
try {
|
||||
$cachedData = $this->cache ? $this->cache->get("latest_listen") : null;
|
||||
|
||||
if ($cachedData) return json_decode($cachedData, true);
|
||||
|
||||
$data = $this->makeRequest("GET", "optimized_latest_listen?select=*");
|
||||
|
||||
if (!is_array($data) || empty($data[0])) return null;
|
||||
|
||||
$latestListen = $this->formatLatestListen($data[0]);
|
||||
|
||||
if ($this->cache) $this->cache->set("latest_listen", json_encode($latestListen), $this->cacheTTL);
|
||||
|
||||
return $latestListen;
|
||||
} catch (\Exception $e) {
|
||||
error_log("LatestListenHandler::getLatestListen error: " . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function formatLatestListen(array $latestListen): array
|
||||
{
|
||||
$emoji = $latestListen["artist_emoji"] ?? ($latestListen["genre_emoji"] ?? "🎧");
|
||||
$trackName = htmlspecialchars($latestListen["track_name"] ?? "Unknown Track", ENT_QUOTES, "UTF-8");
|
||||
$artistName = htmlspecialchars($latestListen["artist_name"] ?? "Unknown Artist", ENT_QUOTES, "UTF-8");
|
||||
$url = htmlspecialchars($latestListen["url"] ?? "/", ENT_QUOTES, "UTF-8");
|
||||
|
||||
return [
|
||||
"content" => sprintf(
|
||||
'%s %s by <a href="%s">%s</a>',
|
||||
$emoji,
|
||||
$trackName,
|
||||
$url,
|
||||
$artistName
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
26
api/Classes/MusicDataHandler.php
Normal file
26
api/Classes/MusicDataHandler.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class MusicDataHandler extends BaseHandler
|
||||
{
|
||||
protected int $cacheTTL = 300;
|
||||
|
||||
public function getThisWeekData(): array
|
||||
{
|
||||
$cacheKey = 'music_week_data';
|
||||
$cached = $this->cache ? $this->cache->get($cacheKey) : null;
|
||||
|
||||
if ($cached) return json_decode($cached, true);
|
||||
|
||||
$response = $this->makeRequest('GET', 'optimized_week_music?select=*');
|
||||
$music = $response[0]['week_music'] ?? [];
|
||||
$music['total_tracks'] = $music['week_summary']['total_tracks'] ?? 0;
|
||||
$music['total_artists'] = $music['week_summary']['total_artists'] ?? 0;
|
||||
$music['total_albums'] = $music['week_summary']['total_albums'] ?? 0;
|
||||
|
||||
if ($this->cache) $this->cache->set($cacheKey, json_encode($music), $this->cacheTTL);
|
||||
|
||||
return $music;
|
||||
}
|
||||
}
|
|
@ -11,21 +11,18 @@ abstract class PageFetcher extends BaseHandler
|
|||
|
||||
protected function cacheGet(string $key): mixed
|
||||
{
|
||||
return $this->cache && $this->cache->exists($key)
|
||||
? json_decode($this->cache->get($key), true)
|
||||
: null;
|
||||
return $this->cache && $this->cache->exists($key) ? json_decode($this->cache->get($key), true) : null;
|
||||
}
|
||||
|
||||
protected function cacheSet(string $key, mixed $value, int $ttl = 3600): void
|
||||
protected function cacheSet(string $key, mixed $value, int $ttl = 300): void
|
||||
{
|
||||
if ($this->cache) {
|
||||
$this->cache->setex($key, $ttl, json_encode($value));
|
||||
}
|
||||
if ($this->cache) $this->cache->setex($key, $ttl, json_encode($value));
|
||||
}
|
||||
|
||||
protected function fetchSingleFromApi(string $endpoint, string $url): ?array
|
||||
{
|
||||
$data = $this->fetchFromApi($endpoint, "url=eq./{$url}");
|
||||
|
||||
return $data[0] ?? null;
|
||||
}
|
||||
|
||||
|
@ -39,6 +36,7 @@ abstract class PageFetcher extends BaseHandler
|
|||
if ($this->globals !== null) return $this->globals;
|
||||
|
||||
$fetcher = new GlobalsFetcher();
|
||||
|
||||
$this->globals = $fetcher->fetch();
|
||||
|
||||
return $this->globals;
|
||||
|
|
36
api/Classes/RecentMediaHandler.php
Normal file
36
api/Classes/RecentMediaHandler.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class RecentMediaHandler extends BaseHandler
|
||||
{
|
||||
protected int $cacheTTL = 300;
|
||||
|
||||
public function getRecentMedia(): array
|
||||
{
|
||||
try {
|
||||
$cacheKey = 'recent_media';
|
||||
|
||||
if ($this->cache) {
|
||||
$cached = $this->cache->get($cacheKey);
|
||||
|
||||
if ($cached) return json_decode($cached, true);
|
||||
}
|
||||
|
||||
$response = $this->makeRequest("GET", "optimized_recent_media?select=*");
|
||||
$activity = $response[0]['recent_activity'] ?? [];
|
||||
$data = [
|
||||
'recentMusic' => $activity['recentMusic'] ?? [],
|
||||
'recentWatchedRead' => $activity['recentWatchedRead'] ?? [],
|
||||
];
|
||||
|
||||
if ($this->cache) $this->cache->set($cacheKey, json_encode($data), $this->cacheTTL);
|
||||
|
||||
return $data;
|
||||
} catch (\Exception $e) {
|
||||
error_log("RecentMediaHandler error: " . $e->getMessage());
|
||||
|
||||
return ['recentMusic' => [], 'recentWatchedRead' => []];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue