v0.3.2:
[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 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 #echo 'playlistID' . $playlistID;
105
106 # Create new playlist if one does not exist
107 # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST
108 #if (!$playlistID) {
109 if (true) {
110
111 $playlist_data = [
112 'name' => $playlistName,
113 'description' => 'A playlist of Marketplace tracks by Marketplaylister.',
114 ];
115
116 $playlist_opts = [
117 'http' => [
118 'method' => 'POST',
119 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
120 . 'Content-Type application/json \r\n',
121 'content' => json_encode($playlist_data)
122 ]
123 ];
124
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'];
129
130 #echo '<br />' . $playlistID;
131
132 }
133
134 $uris = [];
135
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'];
143 }
144 }
145
146 $update_data = [
147 'uris' => $uris,
148 ];
149
150 echo '<br /> update_data <br />';
151 #print_r($update_data);
152
153 $update_opts = [
154 'http' => [
155 'method' => 'POST',
156 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
157 . 'Content-Type application/json \r\n',
158 'content' => json_encode($update_data)
159 ]
160 ];
161
162 $update_context = stream_context_create($update_opts);
163 $update_url = BASE_URL . 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks';
164 echo '<br />' . $update_url;
165 echo '<br />';
166 echo '<br />' . count($uris);
167 echo '<br />';
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);