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