diff options
| author | sillylaird <sillyfanboy@gmail.com> | 2026-09-03 00:33:59 +0000 |
|---|---|---|
| committer | sillylaird <sillyfanboy@gmail.com> | 2026-09-03 00:33:59 +0000 |
| commit | 898b52edcb47bcb3e9d6106e74ca73e74ea01e70 (patch) | |
| tree | 85c6ee5ad58b860144551184d4cf86b560c62b91 /lastfm-proxy.php | |
| download | www-main.tar.gz www-main.zip | |
Diffstat (limited to 'lastfm-proxy.php')
| -rw-r--r-- | lastfm-proxy.php | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lastfm-proxy.php b/lastfm-proxy.php new file mode 100644 index 0000000..7a0a208 --- /dev/null +++ b/lastfm-proxy.php @@ -0,0 +1,35 @@ +<?php +require_once __DIR__ . '/partials/proxy_helpers.php'; + +header('Content-Type: application/json'); +header('Cache-Control: public, max-age=60'); + +if (!proxy_rate_limit('lastfm', 60, 60)) { + http_response_code(429); + echo json_encode(['error' => 'rate limited']); + exit; +} + +$api_key = lastfm_api_key(); +$user = 'SillyLaird'; +$url = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={$user}&api_key={$api_key}&format=json&limit=5"; + +$ctx = stream_context_create(['http' => [ + 'timeout' => 5, + 'header' => 'User-Agent: www.sillylaird.ca/1.0', +]]); + +$response = @file_get_contents($url, false, $ctx); + +if ($response === false) { + http_response_code(502); + echo json_encode(['error' => 'Failed to fetch from Last.fm']); + exit; +} + +// Cap response size we relay back (defensive — Last.fm responses are tiny). +if (strlen($response) > 256 * 1024) { + $response = substr($response, 0, 256 * 1024); +} + +echo $response; |
