v0.1
[Marketplaylister.git] / callback.php
1 <?php
2
3 require 'secrets.php';
4
5 const BASE_URL = 'https://api.spotify.com/v1/';
6 const AUTH_URL = 'https://accounts.spotify.com/';
7 #TODO correctly get tracks only for current month as well as only new tracks
8 const MONTHS = [];
9
10 $code = $_GET['code'];
11
12 if (!$code) {
13 exit(1);
14 }
15
16 #Handle Spotify Token Authorization
17
18 $token_data = [
19 'grant_type' => 'authorization_code',
20 'code' => $code,
21 'redirect_uri' => REDIRECT_URI
22 ];
23 $token_data = http_build_query($token_data);
24
25 $token_opts = [
26 'http' => [
27 'method' => 'POST',
28 /*'header' => "Content-type: application/x-www-form-urlencoded\r\n"
29 . "Content-Length: " . strlen($token_data) . "\r\n"
30 . "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET) . "\r\n",*/
31 'header' => "Authorization: Basic " . base64_encode('868e2cba00de4819900dd8a647a7ba7d:' . CLIENT_SECRET) . " \r\n",
32 'content' => $token_data
33 ]
34 ];
35
36 $token_context = stream_context_create($token_opts);
37
38 $spot_req = file_get_contents(AUTH_URL . 'api/token', false, $token_context);
39
40 echo $spot_req;
41 $spot_json = json_decode($spot_req, true);
42
43 $spot_token = $spot_json['access_token'];
44
45 $me_opts = [
46 'http' => [
47 'method' => 'GET',
48 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
49 ]
50 ];
51
52 $me_context = stream_context_create($me_opts);
53
54 $me_resp = file_get_contents(BASE_URL . 'me', false, $me_context);
55 $me_json = json_decode($me_resp, true);
56 $me_id = $me_json['id'];
57
58 echo '<br />';
59 print_r($me_resp);
60
61 $html = file_get_contents('https://www.marketplace.org/latest-music');
62 $DOM = new DOMDocument;
63 $DOM->loadHTML($html);
64 $headers = $DOM->getElementsByTagName('h2');
65 $divs = $DOM->getElementsByTagName('div');
66
67 $date_headers = [];
68 $music_group = [];
69
70 foreach ($headers as $header) {
71 if ($header->hasAttribute('class') && $header->getAttribute('class') === 'river--hed') {
72 $date_headers[] = $header->nodeValue;
73 }
74 }
75 foreach ($divs as $div) {
76 if ($div->hasAttribute('class') && $div->getAttribute('class') === 'episode-music') {
77 $songs = [];
78 foreach ($div->childNodes as $row) {
79 $children = $row->childNodes[0]->childNodes;
80 $songs[] = [
81 'title' => $children[0]->nodeValue,
82 'artist' => $children[1]->nodeValue
83 ];
84 }
85 $music_group[] = $songs;
86 }
87 }
88
89 echo '<br />';
90
91 print_r($date_headers);
92
93 echo '<br />';
94 print_r($music_group);
95
96 #TODO Check if this month's playlist exists first
97
98 $playlist_data = [
99 'name' => 'March Marketplace Tracks'
100 ];
101
102 $playlist_opts = [
103 'http' => [
104 'method' => 'POST',
105 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
106 . 'Content-Type application/json \r\n',
107 'content' => json_encode($playlist_data)
108 ]
109 ];
110
111 $playlist_context = stream_context_create($playlist_opts);
112
113 $playlist_req = file_get_contents(BASE_URL . 'users/' . $me_id . '/playlists', false, $playlist_context);
114
115 $playlist_json = json_decode($playlist_req, true);
116
117 $playlist_id = $playlist_json['id'];
118
119 echo '<br />' . $playlist_id;
120
121 $uris = [];
122
123 for ($i = count($music_group) - 1; $i >= 0; $i--) {
124
125 $track_opts = [
126 'http' => [
127 'method' => 'GET',
128 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
129 ]
130 ];
131
132 $track_context = stream_context_create($track_opts);
133
134 foreach ($music_group[$i] as $song_info) {
135
136 $track_search_url = BASE_URL . 'search?q=track:' . urlencode($song_info['title']) . '+artist:' . urlencode($song_info['artist']) . '&type=track';
137
138 echo '<br />' . $track_search_url;
139 echo '<br />';
140
141 $track_req = file_get_contents($track_search_url, false, $track_context);
142 $track_json = json_decode($track_req, true);
143
144 print_r($track_json);
145
146 $uris[] = $track_json['tracks']['items'][0]['uri'];
147
148 #rate limit
149 sleep(1);
150
151 }
152
153 }
154
155 $update_data = [
156 'uris' => array_values(array_filter($uris, function($uri) {return !is_null($uri);} ))
157 ];
158
159 echo '<br /> update_data <br />';
160 print_r($update_data);
161
162 $update_opts = [
163 'http' => [
164 'method' => 'POST',
165 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
166 . 'Content-Type application/json \r\n',
167 'content' => json_encode($update_data)
168 ]
169 ];
170
171 $update_context = stream_context_create($update_opts);
172 $update_url = BASE_URL . 'users/' . $me_id . '/playlists/' . $playlist_id . '/tracks';
173 echo '<br />' . $update_url;
174 echo '<br />';
175 echo '<br />' . count($uris);
176 echo '<br />';
177 print_r(json_encode($update_data));
178 $update_req = file_get_contents(BASE_URL . 'users/' . $me_id . '/playlists/' . $playlist_id . '/tracks', false, $update_context);
179