2 declare(strict_types
= 1);
7 const BASE_URL
= 'https://api.spotify.com/v1/';
8 const AUTH_URL
= 'https://accounts.spotify.com/';
9 const DATE_FILE
= 'prev_date.txt';
10 const DATE_FORM
= 'm/d/Y';
26 $code = $_GET['code'];
28 // Get month and year user is requesting
29 $state = explode(':', $_GET['state']);
38 #Handle Spotify Token Authorization
41 'grant_type' => 'authorization_code',
43 'redirect_uri' => REDIRECT_URI
45 $token_data = http_build_query($token_data);
50 /*'header' => "Content-type: application/x-www-form-urlencoded\r\n"
51 . "Content-Length: " . strlen($token_data) . "\r\n"
52 . "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET) . "\r\n",*/
53 'header' => "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET
) . " \r\n",
54 'content' => $token_data
58 $token_context = stream_context_create($token_opts);
60 $spot_req = file_get_contents(AUTH_URL
. 'api/token', false, $token_context);
63 $spot_json = json_decode($spot_req, true);
65 $spot_token = $spot_json['access_token'];
71 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
75 $me_context = stream_context_create($me_opts);
77 $me_resp = file_get_contents(BASE_URL
. 'me', false, $me_context);
78 $me_json = json_decode($me_resp, true);
79 $me_id = $me_json['id'];
81 # Check if this month's playlist exists
83 $playlistName = MONTHS
[$state[0]] . ' ' . $state[1] . ' Marketplace Tracks';
85 $checkPlaylistOpts = [
88 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
92 $checkPlaylistContext = stream_context_create($checkPlaylistOpts);
94 $checkPlaylistReq = file_get_contents(BASE_URL
. 'me/playlists', false, $checkPlaylistContext);
96 $checkPlaylistJson = json_decode($checkPlaylistReq, true);
98 foreach ($checkPlaylistJson['items'] as $playlist) {
99 #TODO should check if $user owns playlist
100 if (!strcmp($playlistName, $playlist['name'])) {
101 $playlistID = $playlist['id'];
105 #echo 'playlistID' . $playlistID;
107 # Create new playlist if one does not exist
108 # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST
113 'name' => $playlistName,
114 'description' => 'A playlist of Marketplace tracks by Marketplaylister.',
120 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
121 . 'Content-Type application/json \r\n',
122 'content' => json_encode($playlist_data)
126 $playlist_context = stream_context_create($playlist_opts);
127 $playlist_req = file_get_contents(BASE_URL
. 'users/' . $me_id . '/playlists', false, $playlist_context);
128 $playlist_json = json_decode($playlist_req, true);
129 $playlistID = $playlist_json['id'];
131 #echo '<br />' . $playlistID;
137 $pdo = new PDO("sqlite:mktplc.sqlite3");
138 $stmt = $pdo->prepare("SELECT uri FROM songs s WHERE (SELECT strftime('%m', s.date) == :month and strftime('%Y', s.date) == :year;");
139 $stmt->bindParam(':month', $state[0]);
140 $stmt->bindParam(':year', $state[1]);
141 if ($stmt->execute()) {
142 while ($row = $stmt->fetch()) {
143 $uris[] = $row['uri'];
151 echo '<br /> update_data <br />';
152 #print_r($update_data);
157 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
158 . 'Content-Type application/json \r\n',
159 'content' => json_encode($update_data)
163 $update_context = stream_context_create($update_opts);
164 $update_url = BASE_URL
. 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks';
165 echo '<br />' . $update_url;
167 echo '<br />' . count($uris);
169 print_r(json_encode($update_data));
170 $update_req = file_get_contents(BASE_URL
. 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks', false, $update_context);
171 print_r($update_req);
173 file_put_contents(DATE_FILE
, $recentEpDT->format(DATE_FORM
));