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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php
declare(strict_types=1);
namespace TCB;
use TCB\TokenManager;
use TCB\HistoryStorage;
final class SpotifyApiClient
{
public function __construct(
private readonly TokenManager $tokenManager,
private readonly HistoryStorage $historyStorage,
) {}
public function pickRandomSongFromPlaylist(string $playlistName): array
{
echo "Picking a random song from the playlist $playlistName.\n";
if (
!array_key_exists($playlistName, $this->historyStorage->playlists)
|| $this->historyStorage->playlists[$playlistName] === ''
) {
$this->getPlaylistID($playlistName);
}
$items = $this->getPlaylistItems($this->historyStorage->playlists[$playlistName]);
if (!count($items)) {
echo "Playlist appears empty.\n";
exit(1);
}
$items = array_values(array_filter($items, function (array $item): bool {
return !array_key_exists($item['id'], $this->historyStorage->history);
}));
if (!count($items)) {
echo "No songs left to pick.\n";
exit(1);
}
$key = array_rand($items);
$song = $items[$key];
$this->historyStorage->saveSong($song);
return $song;
}
private function getPlaylistID(string $playlistName): void
{
echo "Looking up the Spotify playlist ID.\n";
$url = 'https://api.spotify.com/v1/me/playlists?limit=50';
$ch = curl_init();
while ($url !== null) {
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_HTTPGET => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$this->tokenManager->getAccessToken()}",
],
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($statusCode !== 200) {
echo "Non-200 response from Spotify: $statusCode $response\n";
exit(1);
}
$data = json_decode($response, true) ?? [];
foreach ($data['items'] ?? [] as $item) {
if ($item['name'] === $playlistName) {
$this->historyStorage->playlists[$playlistName] = $item['id'];
$this->historyStorage->save();
return;
}
}
$url = $data['next'] ?? null;
}
echo "Unable to find a playlist named $playlistName.\n";
exit(1);
}
private function getPlaylistItems(string $playlistID): array
{
$items = [];
$query = http_build_query([
'fields' => 'next,items(item(id,name,artists(name)))',
]);
$url = "https://api.spotify.com/v1/playlists/$playlistID/items?limit=50&$query";
$ch = curl_init();
while ($url !== null) {
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_HTTPGET => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$this->tokenManager->getAccessToken()}",
],
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($statusCode !== 200) {
echo "Non-200 response from Spotify: $statusCode $response\n";
exit(1);
}
$data = json_decode($response, true) ?? [];
foreach ($data['items'] ?? [] as $item) {
if (isset($item['item'])) {
$items[] = $item['item'];
}
}
$url = $data['next'] ?? null;
}
return $items;
}
}
|