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';
25 $code = $_GET['code'];
27 // Get month and year user is requesting
28 $state = explode(':', $_GET['state']);
37 #Handle Spotify Token Authorization
40 'grant_type' => 'authorization_code',
42 'redirect_uri' => REDIRECT_URI
44 $token_data = http_build_query($token_data);
49 /*'header' => "Content-type: application/x-www-form-urlencoded\r\n"
50 . "Content-Length: " . strlen($token_data) . "\r\n"
51 . "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET) . "\r\n",*/
52 'header' => "Authorization: Basic " . base64_encode('93a6f9c0375c45d4b348157691aa24e8:' . CLIENT_SECRET
) . " \r\n",
53 'content' => $token_data
57 $token_context = stream_context_create($token_opts);
59 $spot_req = file_get_contents(AUTH_URL
. 'api/token', false, $token_context);
62 $spot_json = json_decode($spot_req, true);
64 $spot_token = $spot_json['access_token'];
70 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
74 $me_context = stream_context_create($me_opts);
76 $me_resp = file_get_contents(BASE_URL
. 'me', false, $me_context);
77 $me_json = json_decode($me_resp, true);
78 $me_id = $me_json['id'];
80 # Check if this month's playlist exists
82 $playlistName = MONTHS
[$state[0]] . ' ' . $state[1] . ' Marketplace Tracks';
84 $checkPlaylistOpts = [
87 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
91 $checkPlaylistContext = stream_context_create($checkPlaylistOpts);
93 $checkPlaylistReq = file_get_contents(BASE_URL
. 'me/playlists', false, $checkPlaylistContext);
95 $checkPlaylistJson = json_decode($checkPlaylistReq, true);
97 foreach ($checkPlaylistJson['items'] as $playlist) {
98 #TODO should check if $user owns playlist
99 if (!strcmp($playlistName, $playlist['name'])) {
100 $playlistID = $playlist['id'];
104 #echo 'playlistID' . $playlistID;
106 # Create new playlist if one does not exist
107 # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST
112 'name' => $playlistName,
113 'description' => 'A playlist of Marketplace tracks by Marketplaylister.',
119 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
120 . 'Content-Type application/json \r\n',
121 'content' => json_encode($playlist_data)
125 $playlist_context = stream_context_create($playlist_opts);
126 $playlist_req = file_get_contents(BASE_URL
. 'users/' . $me_id . '/playlists', false, $playlist_context);
127 $playlist_json = json_decode($playlist_req, true);
128 $playlistID = $playlist_json['id'];
130 #echo '<br />' . $playlistID;
136 $pdo = new PDO("sqlite:mktplc.sqlite3");
137 $stmt = $pdo->prepare("SELECT uri FROM songs s WHERE uri IS NOT NULL AND strftime('%m', s.date) == :month AND strftime('%Y', s.date) == :year");
138 $stmt->bindParam(':month', $state[0]);
139 $stmt->bindParam(':year', $state[1]);
140 if ($stmt->execute()) {
141 while ($row = $stmt->fetch()) {
142 $uris[] = $row['uri'];
150 echo '<br /> update_data <br />';
151 #print_r($update_data);
156 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
157 . 'Content-Type application/json \r\n',
158 'content' => json_encode($update_data)
162 $update_context = stream_context_create($update_opts);
163 $update_url = BASE_URL
. 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks';
164 echo '<br />' . $update_url;
166 echo '<br />' . count($uris);
168 print_r(json_encode($update_data));
169 $update_req = file_get_contents(BASE_URL
. 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks', false, $update_context);
170 print_r($update_req);