-<?php\r
- declare(strict_types = 1);\r
- \r
- require 'secrets.php';\r
- require 'mpfuncs.php';\r
- \r
- const BASE_URL = 'https://api.spotify.com/v1/';\r
- const AUTH_URL = 'https://accounts.spotify.com/';\r
- const DATE_FILE = 'prev_date.txt';\r
- const MONTHS = [\r
- '01' => 'January',\r
- '02' => 'February',\r
- '03' => 'March',\r
- '04' => 'April',\r
- '05' => 'May',\r
- '06' => 'June',\r
- '07' => 'July',\r
- '08' => 'August',\r
- '09' => 'September',\r
- '10' => 'October',\r
- '11' => 'November',\r
- '12' => 'December',\r
- ];\r
- \r
- $code = $_GET['code'];\r
- \r
- // Get month and year user is requesting\r
- $state = explode(':', $_GET['state']);\r
- \r
- \r
- if (!$code) {\r
- exit(1);\r
- }\r
- \r
- #print_r($today); \r
- \r
- #Handle Spotify Token Authorization\r
- \r
- $token_data = [\r
- 'grant_type' => 'authorization_code',\r
- 'code' => $code,\r
- 'redirect_uri' => REDIRECT_URI\r
- ];\r
- $token_data = http_build_query($token_data);\r
- \r
- $token_opts = [\r
- 'http' => [\r
- 'method' => 'POST',\r
- /*'header' => "Content-type: application/x-www-form-urlencoded\r\n"\r
- . "Content-Length: " . strlen($token_data) . "\r\n"\r
- . "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET) . "\r\n",*/\r
- 'header' => "Authorization: Basic " . base64_encode('93a6f9c0375c45d4b348157691aa24e8:' . CLIENT_SECRET) . " \r\n",\r
- 'content' => $token_data\r
- ]\r
- ];\r
- \r
- $token_context = stream_context_create($token_opts);\r
- \r
- $spot_req = file_get_contents(AUTH_URL . 'api/token', false, $token_context);\r
- \r
- echo $spot_req;\r
- $spot_json = json_decode($spot_req, true);\r
-\r
- $spot_token = $spot_json['access_token'];\r
- \r
- \r
- $me_opts = [\r
- 'http' => [\r
- 'method' => 'GET',\r
- 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"\r
- ]\r
- ];\r
- \r
- $me_context = stream_context_create($me_opts);\r
- \r
- $me_resp = file_get_contents(BASE_URL . 'me', false, $me_context);\r
- $me_json = json_decode($me_resp, true);\r
- $me_id = $me_json['id'];\r
- \r
- # Check if this month's playlist exists\r
- \r
- $playlistName = MONTHS[$state[0]] . ' ' . $state[1] . ' Marketplace Tracks';\r
- \r
- $checkPlaylistOpts = [\r
- 'http' => [\r
- 'method' => 'GET',\r
- 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"\r
- ]\r
- ];\r
- \r
- $checkPlaylistContext = stream_context_create($checkPlaylistOpts);\r
- \r
- $checkPlaylistReq = file_get_contents(BASE_URL . 'me/playlists', false, $checkPlaylistContext);\r
- \r
- $checkPlaylistJson = json_decode($checkPlaylistReq, true);\r
- \r
- foreach ($checkPlaylistJson['items'] as $playlist) {\r
- #TODO should check if $user owns playlist\r
- if (!strcmp($playlistName, $playlist['name'])) {\r
- $playlistID = $playlist['id'];\r
- }\r
- }\r
- \r
- #echo 'playlistID' . $playlistID;\r
- \r
- # Create new playlist if one does not exist\r
- # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST\r
- #if (!$playlistID) {\r
- if (true) {\r
-\r
- $playlist_data = [\r
- 'name' => $playlistName,\r
- 'description' => 'A playlist of Marketplace tracks by Marketplaylister.',\r
- ];\r
- \r
- $playlist_opts = [\r
- 'http' => [\r
- 'method' => 'POST',\r
- 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"\r
- . 'Content-Type application/json \r\n',\r
- 'content' => json_encode($playlist_data)\r
- ]\r
- ];\r
-\r
- $playlist_context = stream_context_create($playlist_opts);\r
- $playlist_req = file_get_contents(BASE_URL . 'users/' . $me_id . '/playlists', false, $playlist_context);\r
- $playlist_json = json_decode($playlist_req, true);\r
- $playlistID = $playlist_json['id'];\r
- \r
- #echo '<br />' . $playlistID;\r
- \r
- }\r
- \r
- $uris = [];\r
- \r
- $pdo = new PDO("sqlite:mktplc.sqlite3");\r
- $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");\r
- $stmt->bindParam(':month', $state[0]);\r
- $stmt->bindParam(':year', $state[1]);\r
- if ($stmt->execute()) {\r
- while ($row = $stmt->fetch()) {\r
- $uris[] = $row['uri'];\r
- }\r
- }\r
- \r
- $update_data = [\r
- 'uris' => $uris,\r
- ];\r
- \r
- echo '<br /> update_data <br />';\r
- #print_r($update_data);\r
- \r
- $update_opts = [\r
- 'http' => [\r
- 'method' => 'POST',\r
- 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"\r
- . 'Content-Type application/json \r\n',\r
- 'content' => json_encode($update_data)\r
- ]\r
- ];\r
- \r
- $update_context = stream_context_create($update_opts);\r
- $update_url = BASE_URL . 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks';\r
- echo '<br />' . $update_url;\r
- echo '<br />';\r
- echo '<br />' . count($uris);\r
- echo '<br />';\r
- print_r(json_encode($update_data));\r
- $update_req = file_get_contents(BASE_URL . 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks', false, $update_context);\r
- print_r($update_req);
\ No newline at end of file
+<?php
+declare(strict_types = 1);
+
+require 'secrets.php';
+require 'mpfuncs.php';
+
+const BASE_URL = 'https://api.spotify.com/v1/';
+const AUTH_URL = 'https://accounts.spotify.com/';
+const DATE_FILE = 'prev_date.txt';
+const MONTHS = [
+ '01' => 'January',
+ '02' => 'February',
+ '03' => 'March',
+ '04' => 'April',
+ '05' => 'May',
+ '06' => 'June',
+ '07' => 'July',
+ '08' => 'August',
+ '09' => 'September',
+ '10' => 'October',
+ '11' => 'November',
+ '12' => 'December',
+];
+
+$code = $_GET['code'];
+
+// Get month and year user is requesting
+$state = explode(':', $_GET['state']);
+
+
+if (!$code) {
+ exit(1);
+}
+
+#print_r($today);
+
+#Handle Spotify Token Authorization
+
+$token_data = [
+ 'grant_type' => 'authorization_code',
+ 'code' => $code,
+ 'redirect_uri' => REDIRECT_URI
+];
+$token_data = http_build_query($token_data);
+
+$token_opts = [
+ 'http' => [
+ 'method' => 'POST',
+ /*'header' => "Content-type: application/x-www-form-urlencoded\r\n"
+ . "Content-Length: " . strlen($token_data) . "\r\n"
+ . "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET) . "\r\n",*/
+ 'header' => "Authorization: Basic " . base64_encode('93a6f9c0375c45d4b348157691aa24e8:' . CLIENT_SECRET) . " \r\n",
+ 'content' => $token_data
+ ]
+];
+
+$token_context = stream_context_create($token_opts);
+
+$spot_req = file_get_contents(AUTH_URL . 'api/token', false, $token_context);
+
+echo $spot_req;
+$spot_json = json_decode($spot_req, true);
+
+$spot_token = $spot_json['access_token'];
+
+
+$me_opts = [
+ 'http' => [
+ 'method' => 'GET',
+ 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
+ ]
+];
+
+$me_context = stream_context_create($me_opts);
+
+$me_resp = file_get_contents(BASE_URL . 'me', false, $me_context);
+$me_json = json_decode($me_resp, true);
+$me_id = $me_json['id'];
+
+# Check if this month's playlist exists
+
+$playlistName = MONTHS[$state[0]] . ' ' . $state[1] . ' Marketplace Tracks';
+
+$checkPlaylistOpts = [
+ 'http' => [
+ 'method' => 'GET',
+ 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
+ ]
+];
+
+$checkPlaylistContext = stream_context_create($checkPlaylistOpts);
+
+$checkPlaylistReq = file_get_contents(BASE_URL . 'me/playlists', false, $checkPlaylistContext);
+
+$checkPlaylistJson = json_decode($checkPlaylistReq, true);
+
+foreach ($checkPlaylistJson['items'] as $playlist) {
+ #TODO should check if $user owns playlist
+ if (!strcmp($playlistName, $playlist['name'])) {
+ $playlistID = $playlist['id'];
+ }
+}
+
+# Create new playlist if one does not exist
+# DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST
+#if (!$playlistID) {
+ if (true) {
+
+ $playlist_data = [
+ 'name' => $playlistName,
+ 'description' => 'A playlist of Marketplace tracks by Marketplaylister.',
+ ];
+
+ $playlist_opts = [
+ 'http' => [
+ 'method' => 'POST',
+ 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
+ . 'Content-Type: application/json \r\n',
+ 'content' => json_encode($playlist_data)
+ ]
+ ];
+
+ $playlist_context = stream_context_create($playlist_opts);
+ $playlist_req = file_get_contents(BASE_URL . 'users/' . $me_id . '/playlists', false, $playlist_context);
+ $playlist_json = json_decode($playlist_req, true);
+ $playlistID = $playlist_json['id'];
+
+
+ }
+
+ $uris = [];
+
+ $pdo = new PDO("sqlite:mktplc.sqlite3");
+ $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");
+ $stmt->bindParam(':month', $state[0]);
+ $stmt->bindParam(':year', $state[1]);
+ if ($stmt->execute()) {
+ while ($row = $stmt->fetch()) {
+ $uris[] = $row['uri'];
+ }
+ }
+
+ $update_data = [
+ 'uris' => $uris,
+ ];
+
+ echo '<br /> update_data <br />';
+
+ $update_opts = [
+ 'http' => [
+ 'method' => 'POST',
+ 'ignore_errors' => true,
+ 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
+ . 'Content-Type: application/json \r\n'
+ . 'Accept: application/json \r\n',
+ 'content' => json_encode($update_data)
+ ]
+ ];
+
+ $update_context = stream_context_create($update_opts);
+ $update_url = BASE_URL . 'playlists/' . $playlistID . '/tracks';
+ echo '<br />' . $update_url;
+ echo '<br />';
+ echo '<br />' . count($uris);
+ echo '<br />';
+ print_r(json_encode($update_data));
+ $update_req = file_get_contents($update_url, false, $update_context);
+ var_dump($update_req);
+