| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | require 'secrets.php'; |
| 5 | require 'mpfuncs.php'; |
| 6 | |
| 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 MONTHS = [ |
| 11 | '01' => 'January', |
| 12 | '02' => 'February', |
| 13 | '03' => 'March', |
| 14 | '04' => 'April', |
| 15 | '05' => 'May', |
| 16 | '06' => 'June', |
| 17 | '07' => 'July', |
| 18 | '08' => 'August', |
| 19 | '09' => 'September', |
| 20 | '10' => 'October', |
| 21 | '11' => 'November', |
| 22 | '12' => 'December', |
| 23 | ]; |
| 24 | |
| 25 | $code = $_GET['code']; |
| 26 | |
| 27 | // Get month and year user is requesting |
| 28 | $state = explode(':', $_GET['state']); |
| 29 | |
| 30 | |
| 31 | if (!$code) { |
| 32 | exit(1); |
| 33 | } |
| 34 | |
| 35 | #print_r($today); |
| 36 | |
| 37 | #Handle Spotify Token Authorization |
| 38 | |
| 39 | $token_data = [ |
| 40 | 'grant_type' => 'authorization_code', |
| 41 | 'code' => $code, |
| 42 | 'redirect_uri' => REDIRECT_URI |
| 43 | ]; |
| 44 | $token_data = http_build_query($token_data); |
| 45 | |
| 46 | $token_opts = [ |
| 47 | 'http' => [ |
| 48 | 'method' => 'POST', |
| 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 |
| 54 | ] |
| 55 | ]; |
| 56 | |
| 57 | $token_context = stream_context_create($token_opts); |
| 58 | |
| 59 | $spot_req = file_get_contents(AUTH_URL . 'api/token', false, $token_context); |
| 60 | |
| 61 | echo $spot_req; |
| 62 | $spot_json = json_decode($spot_req, true); |
| 63 | |
| 64 | $spot_token = $spot_json['access_token']; |
| 65 | |
| 66 | |
| 67 | $me_opts = [ |
| 68 | 'http' => [ |
| 69 | 'method' => 'GET', |
| 70 | 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n" |
| 71 | ] |
| 72 | ]; |
| 73 | |
| 74 | $me_context = stream_context_create($me_opts); |
| 75 | |
| 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']; |
| 79 | |
| 80 | # Check if this month's playlist exists |
| 81 | |
| 82 | $playlistName = MONTHS[$state[0]] . ' ' . $state[1] . ' Marketplace Tracks'; |
| 83 | |
| 84 | $checkPlaylistOpts = [ |
| 85 | 'http' => [ |
| 86 | 'method' => 'GET', |
| 87 | 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n" |
| 88 | ] |
| 89 | ]; |
| 90 | |
| 91 | $checkPlaylistContext = stream_context_create($checkPlaylistOpts); |
| 92 | |
| 93 | $checkPlaylistReq = file_get_contents(BASE_URL . 'me/playlists', false, $checkPlaylistContext); |
| 94 | |
| 95 | $checkPlaylistJson = json_decode($checkPlaylistReq, true); |
| 96 | |
| 97 | foreach ($checkPlaylistJson['items'] as $playlist) { |
| 98 | #TODO should check if $user owns playlist |
| 99 | if (!strcmp($playlistName, $playlist['name'])) { |
| 100 | $playlistID = $playlist['id']; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | # Create new playlist if one does not exist |
| 105 | # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST |
| 106 | #if (!$playlistID) { |
| 107 | if (true) { |
| 108 | |
| 109 | $playlist_data = [ |
| 110 | 'name' => $playlistName, |
| 111 | 'description' => 'A playlist of Marketplace tracks by Marketplaylister.', |
| 112 | ]; |
| 113 | |
| 114 | $playlist_opts = [ |
| 115 | 'http' => [ |
| 116 | 'method' => 'POST', |
| 117 | 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n" |
| 118 | . 'Content-Type: application/json \r\n', |
| 119 | 'content' => json_encode($playlist_data) |
| 120 | ] |
| 121 | ]; |
| 122 | |
| 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']; |
| 127 | |
| 128 | |
| 129 | } |
| 130 | |
| 131 | $uris = []; |
| 132 | |
| 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']; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | $update_data = [ |
| 144 | 'uris' => $uris, |
| 145 | ]; |
| 146 | |
| 147 | echo '<br /> update_data <br />'; |
| 148 | |
| 149 | $update_opts = [ |
| 150 | 'http' => [ |
| 151 | 'method' => 'POST', |
| 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) |
| 157 | ] |
| 158 | ]; |
| 159 | |
| 160 | $update_context = stream_context_create($update_opts); |
| 161 | $update_url = BASE_URL . 'playlists/' . $playlistID . '/tracks'; |
| 162 | echo '<br />' . $update_url; |
| 163 | echo '<br />'; |
| 164 | echo '<br />' . count($uris); |
| 165 | echo '<br />'; |
| 166 | print_r(json_encode($update_data)); |
| 167 | $update_req = file_get_contents($update_url, false, $update_context); |
| 168 | var_dump($update_req); |
| 169 | |