-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnow.php
More file actions
32 lines (23 loc) · 1.22 KB
/
Copy pathnow.php
File metadata and controls
32 lines (23 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json");
$username = $_GET['username'] ?? null; // todo if no username, send back Rick Astley
$limit = $_GET['limit'] ?? 1;
$emoji = $_GET['emoji'] ?? '🎧';
$nomoji = isset($_GET['nomoji']) && $_GET['nomoji'] === 'true';
$emojiString = $nomoji ? '' : "$emoji ";
$apiKey = $_ENV['LASTFMKEY'];
if (!$username)
{
$content = "<p class='recent-played-track'>" . $emoji ." <a href='https://www.last.fm/music/Rick+Astley/_/Never+Gonna+Give+You+Up'>Never Gunna Give You Up by Rick Astley</a></p>";
echo json_encode(['content' => $content]);
return;
}
$res = json_decode(file_get_contents("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=$username&api_key=$apiKey&format=json&limit=$limit"));
$formattedTracks = implode('', array_map(function($track) use ($emojiString) {
return sprintf('<p class="recent-played-track">%s<a href="%s">%s by %s</a></p>', $emojiString, $track->url, $track->name, $track->artist->{'#text'});
}, array_slice($res->recenttracks->track, 0, $limit)));
echo json_encode(['content' => $formattedTracks]);