From be854ce3c5e59efcade65dcfd0af5b819b692c5d Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Sun, 2 Feb 2025 12:07:01 -0700 Subject: [PATCH] on first load, redirect to login api for spotify to load user's top tracks. --- api/api | Bin 7932018 -> 7932018 bytes music/explore.php | 53 +++++++++++++++++++++++----------------------- music/index.php | 9 +++++--- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/api/api b/api/api index 8f7fd40fbab6c6ef1c9ddd2170663725b137b220..4d69d63bd084b1af9166c55a5eca163c6d207fa3 100755 GIT binary patch delta 703 zcmbWx%TiJS0D$39^O9C*rFluqTUuV=TsdNzfPf03bRh4^;lNQzP!PdI8>boBuyE&V zxZ{E737m1QzpvopTmCcu-Sz*5JKSE|^u+pwkj#7XxlphW3=7g~Xwz>mMYqEFsb=sR6pKKfU)m20*- z)T03}(1<29qXjR~iZ;B$YrMfz+l~&r#XG!5C%W(f-RMCt`tT9`7{Did#vq0;j1i3D z3&t>xulR-uOkxVtu)_fXA|%LAsx_P1^mp786fY#>#~BJZM-ip!!{x=<)g$~wESLZQ delta 703 zcmbWxO;Zwa0D$o^-_t^~va&Kw%T!|5{qG;77o}BKp;ZtN#Zng8O*1GI(VQ+$GjZYI z&9Bjo4@9598PEFo6*~Nm&&+dv`mgaEe@{K}ylTxKM9Zt8R3w^CTf$q`cYKMG%*TK3 zrYw;x9xkSq49jc!bR)TCo33dTLis{b-HbbPrq2w|?v<24%vxAE(vD-o2qm1~bx)=4 zX}}FMq6y7tK`YwOj+^K}CvM?3uG=nj;|_Xo7x!=jSxP-zzk+F2NUyH zKo}8xtYxZJ;^IH=D%@cSQN*x}I1)%=1u3jz4QZ_76Fy@Do5;XM7F)={fr~HL#tyz> b7vGRa0pIZhMeJc8B^;oP!`kJ^(f*$+FefVz diff --git a/music/explore.php b/music/explore.php index 85fd472..b38a1fc 100644 --- a/music/explore.php +++ b/music/explore.php @@ -7,7 +7,7 @@ $dotenv->safeLoad(); session_start(); $client_id = $_ENV['SPOTIFY_CLIENT_ID']; $client_secret = $_ENV['SPOTIFY_CLIENT_SECRET']; -$auth_code = $_GET['code']; +$_SESSION['auth_code'] = $_GET['code']; $client_credentials = get_client_credentials($client_id, $client_secret); @@ -112,31 +112,31 @@ function get_access_token($auth_code, $client_id, $client_secret) function get_albums($auth_code, $client_id, $client_secret) { - $access_token = get_access_token($auth_code, $client_id, $client_secret); - $url = 'https://api.spotify.com/v1/me/top/tracks'; - - $ch = curl_init($url); - - curl_setopt_array($ch, array( - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => array( - "Authorization: Bearer $access_token" - ) - )); - - $response = curl_exec($ch); - - if (curl_errno($ch)) { - echo curl_error($ch); - return; - } else { - $data = json_decode($response, true); - $top_songs = $data['items']; - } - curl_close($ch); - - $song_data = []; if ($_SESSION['data'] == null) { + $access_token = get_access_token($auth_code, $client_id, $client_secret); + $url = 'https://api.spotify.com/v1/me/top/tracks'; + + $ch = curl_init($url); + + curl_setopt_array($ch, array( + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => array( + "Authorization: Bearer $access_token" + ) + )); + + $response = curl_exec($ch); + + if (curl_errno($ch)) { + echo curl_error($ch); + return; + } else { + $data = json_decode($response, true); + $top_songs = $data['items']; + } + curl_close($ch); + + $song_data = []; foreach ($top_songs as $song) { $song_data[] = array( 'image' => $song['album']['images'][0]['url'], @@ -148,7 +148,6 @@ function get_albums($auth_code, $client_id, $client_secret) ); } - echo "saving data to $_SESSION"; $_SESSION['data'] = $song_data; } else { $song_data = $_SESSION['data']; @@ -165,5 +164,5 @@ function get_albums($auth_code, $client_id, $client_secret) } } -get_albums($auth_code, $client_id, $client_secret); +get_albums($_SESSION['auth_code'], $client_id, $client_secret); ?> diff --git a/music/index.php b/music/index.php index 4aaf000..f6945b6 100644 --- a/music/index.php +++ b/music/index.php @@ -1,6 +1,10 @@ @@ -13,8 +17,7 @@ include ('login.php');

My Favorite Albums

- - +