X-Git-Url: https://git.jacobcasper.com/?p=Marketplaylister.git;a=blobdiff_plain;f=callback.php;h=f47e73bb6f85eaa41f162ae2f48ae6e3503c03b5;hp=7488530d6ecdcd046c652c5c2fe7c6ddd72dbff0;hb=HEAD;hpb=bed05b1a3c4b40cb71564fbebb2cb41242163dbd
diff --git a/callback.php b/callback.php
index 7488530..f47e73b 100644
--- a/callback.php
+++ b/callback.php
@@ -1,256 +1,169 @@
- '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'];
-
- if (!$code) {
- exit(1);
- }
-
- $today = new DateTime;
-
- #print_r($today);
-
- $prevDTTxt = file_get_contents(DATE_FILE);
-
- $prevDT = $prevDTTxt ? DateTime::createFromFormat(DATE_FORM, $prevDTTxt) : DateTime::createFromFormat(DATE_FORM, $today->format('m/') . '01' . $today->format('/Y'));
-
- if (strcmp($prevDT->format('m'), $today->format('m')) < 0) {
- $prevDT = DateTime::createFromFormat(DATE_FORM, $today->format('m/') . '01' . $today->format('/Y'));
- }
-
-
- #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('868e2cba00de4819900dd8a647a7ba7d:' . 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'];
-
- echo '
';
- #print_r($me_resp);
-
- $page = 1;
- $html = file_get_contents('https://www.marketplace.org/latest-music');
- $DOM = new DOMDocument;
- $DOM->loadHTML($html);
- $headers = $DOM->getElementsByTagName('h2');
- $divs = $DOM->getElementsByTagName('div');
-
- $recentEpDT;
- $episodePages = [];
-
- foreach ($headers as $header) {
- if ($header->hasAttribute('class') && $header->getAttribute('class') === 'river--hed') {
- $recentEpDT = DateTime::createFromFormat(DATE_FORM, explode(':', $header->nodeValue)[0]);
- break;
- }
- }
-
- $prevDate = (int) $prevDT->format('d');
- $recentEpDate = (int) $recentEpDT->format('d');
- $daysToGet = ($prevDate === 1) ? $recentEpDate : $recentEpDate - $prevDate;
- $daysToGet = $daysToGet - 2 * (int) ($daysToGet / 7);
-
- if ($daysToGet === 0) {
- echo 'No new episodes since last check.';
- exit(0);
- }
-
-
- do {
- $episodePages[] = parseEpisodePage($divs, $daysToGet);
- } while ($daysToGet > 0 && ($DOM->loadHTML(file_get_contents('https://www.marketplace.org/latest-music?page=' . ++$page))) && ($divs = $DOM->getElementsByTagName('div')) );
-
- /*
- echo '
';
-
- print_r($date_headers);
-
- echo '
';
- print_r($episodes);
- */
-
- print_r($episodePages);
-
- # Check if this month's playlist exists
-
- $playlistName = MONTHS[$today->format('m')] . ' 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'];
- }
- }
-
- #echo 'playlistID' . $playlistID;
-
- # Create new playlist if one does not exist
- # DEVELOPMENT TEMP ALWAYS CREATE NEW PLAYLIST
- #if (!$playlistID) {
- if (true) {
-
- $playlist_data = [
- 'name' => $playlistName,
- ];
-
- $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'];
-
- #echo '
' . $playlistID;
-
- }
-
- $uris = [];
-
- foreach ( array_reverse($episodePages) as $episodes) {
- foreach ( array_reverse($episodes) as $episode) {
-
- $track_opts = [
- 'http' => [
- 'method' => 'GET',
- 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
- ]
- ];
-
- $track_context = stream_context_create($track_opts);
-
- foreach ($episode as $song_info) {
-
- $track_search_url = BASE_URL . 'search?q=track:' . urlencode($song_info['title'])
- . '+artist:' . urlencode($song_info['artist']) . '&type=track';
-
- #echo '
' . $track_search_url;
- #echo '
';
-
- $trackReq = file_get_contents($track_search_url, false, $track_context);
- if ($trackReq) {
- $trackJSON = json_decode($trackReq, true);
- $trackJSON = $trackJSON['tracks'];
-
- print_r($trackJSON);
-
- if ($trackJSON['total'] === 0) {
- continue;
- }
-
- $uris[] = $trackJSON['items'][0]['uri'];
-
- #rate limit
- sleep(1);
-
- }
- }
- }
- }
-
- $update_data = [
- 'uris' => $uris,
- ];
-
- echo '
update_data
';
- #print_r($update_data);
-
- $update_opts = [
- 'http' => [
- 'method' => 'POST',
- 'header' => 'Authorization: Bearer ' . $spot_token . "\r\n"
- . 'Content-Type application/json \r\n',
- 'content' => json_encode($update_data)
- ]
- ];
-
- $update_context = stream_context_create($update_opts);
- $update_url = BASE_URL . 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks';
- echo '
' . $update_url;
- echo '
';
- echo '
' . count($uris);
- echo '
';
- print_r(json_encode($update_data));
- $update_req = file_get_contents(BASE_URL . 'users/' . $me_id . '/playlists/' . $playlistID . '/tracks', false, $update_context);
- print_r($update_req);
-
- file_put_contents(DATE_FILE, $recentEpDT->format(DATE_FORM));
-
\ No newline at end of file
+ '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 '
update_data
';
+
+ $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 '
' . $update_url;
+ echo '
';
+ echo '
' . count($uris);
+ echo '
';
+ print_r(json_encode($update_data));
+ $update_req = file_get_contents($update_url, false, $update_context);
+ var_dump($update_req);
+