summaryrefslogtreecommitdiff
path: root/pick-random-song.php
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2026-08-09 20:04:39 +0100
committerDavid T. Sadler <davidtsadler@googlemail.com>2026-08-09 20:04:39 +0100
commitb9c4f1dcf1333c14e2d1b67561ec7a5f955baa6e (patch)
tree610536953be739d0778f19c90c477f080a24718a /pick-random-song.php
Initial commitHEADmain
Diffstat (limited to 'pick-random-song.php')
-rw-r--r--pick-random-song.php40
1 files changed, 40 insertions, 0 deletions
diff --git a/pick-random-song.php b/pick-random-song.php
new file mode 100644
index 0000000..7ba6a1b
--- /dev/null
+++ b/pick-random-song.php
@@ -0,0 +1,40 @@
+<?php
+
+declare(strict_types=1);
+
+require __DIR__ . '/vendor/autoload.php';
+$env = require __DIR__ . '/.env.php';
+
+use TCB\Credentials;
+use TCB\HistoryStorage;
+use TCB\HttpServer;
+use TCB\SpotifyApiClient;
+use TCB\TokenManager;
+use TCB\TokenStorage;
+
+$credentials = new Credentials(
+ $env['clientID'],
+ $env['clientSecret'],
+ $env['redirectURI'],
+ $env['state'],
+);
+$httpServer = new HttpServer(
+ $env['serverAddress'],
+ (int) $env['serverPort'],
+ (int) $env['serverTimeout'],
+);
+$tokenStorage = TokenStorage::load(__DIR__ . '/.token_storage.json');
+$historyStorage = HistoryStorage::load(__DIR__ . '/.history_storage.json');
+$tokenManager = new TokenManager($credentials, $httpServer, $tokenStorage);
+$client = new SpotifyApiClient($tokenManager, $historyStorage);
+
+$song = $client->pickRandomSongFromPlaylist($env['playlistName']);
+
+$name = $song['name'] ?? '';
+$artists = implode(',', array_map(function (array $artist): string {
+ return $artist['name'];
+}, $song['artists'] ?? []));
+
+echo "The song picked is $name by $artists\n";
+
+exit(0);