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 # Create new playlist if one does not exist
105 # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST
110 'name' => $playlistName,
111 'description' => 'A playlist of Marketplace tracks by Marketplaylister.',
117 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
118 . 'Content-Type: application/json \r\n',
119 'content' => json_encode($playlist_data)
123 $playlist_context = stream_context_create($playlist_opts);
124 $playlist_req = file_get_contents(BASE_URL
. 'users/' . $me_id . '/playlists', false, $playlist_context);
125 $playlist_json = json_decode($playlist_req, true);
126 $playlistID = $playlist_json['id'];
133 $pdo = new PDO("sqlite:mktplc.sqlite3");
134 $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");
135 $stmt->bindParam(':month', $state[0]);
136 $stmt->bindParam(':year', $state[1]);
137 if ($stmt->execute()) {
138 while ($row = $stmt->fetch()) {
139 $uris[] = $row['uri'];
147 echo '<br /> update_data <br />';
152 'ignore_errors' => true,
153 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
154 . 'Content-Type: application/json \r\n'
155 . 'Accept: application/json \r\n',
156 'content' => json_encode($update_data)
160 $update_context = stream_context_create($update_opts);
161 $update_url = BASE_URL
. 'playlists/' . $playlistID . '/tracks';
162 echo '<br />' . $update_url;
164 echo '<br />' . count($uris);
166 print_r(json_encode($update_data));
167 $update_req = file_get_contents($update_url, false, $update_context);
168 var_dump($update_req);