diff --git a/Spotify Playlist Generator(Popular Tracks Analyzer).ipynb b/Spotify Playlist Generator(Popular Tracks Analyzer).ipynb index 2665c6b..95ef357 100644 --- a/Spotify Playlist Generator(Popular Tracks Analyzer).ipynb +++ b/Spotify Playlist Generator(Popular Tracks Analyzer).ipynb @@ -35,14 +35,14 @@ "output_type": "stream", "text": [ "Please authorize the application by visiting the following URL:\n", - "https://accounts.spotify.com/en/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%3Fscope%3Duser-library-read%2Buser-follow-read%2Bplaylist-modify-private%2Bplaylist-modify-public%2Bplaylist-read-private%2Buser-read-private%2Buser-read-email%26response_type%3Dcode%26redirect_uri%3Dhttps%253A%252F%252Fshegszz.github.io%252FSpotify-Playlist-Generator%252F%26code_challenge_method%3DS256%26client_id%3Da5203ec276044c97a6ec5389248f8b79%26code_challenge%3DXRo28PSiSIJfSKfxylh6Mza10eTZdEYdljyzVy2mUtE\n" + "https://accounts.spotify.com/en/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%3Fscope%3Duser-library-read%2Buser-follow-read%2Bplaylist-modify-private%2Bplaylist-modify-public%2Bplaylist-read-private%2Buser-read-private%2Buser-read-email%26response_type%3Dcode%26redirect_uri%3Dhttps%253A%252F%252Fshegszz.github.io%252FSpotify-Playlist-Generator%252F%26code_challenge_method%3DS256%26client_id%3Da5203ec276044c97a6ec5389248f8b79%26code_challenge%3Dw6IwBVBKBlMcbrPUNXmw0hhJfcYnzWKxd_sbj7Y1fhk\n" ] } ], "source": [ "# Spotify API credentials\n", - "client_id = 'your-client-id-here'\n", - "redirect_uri = 'your-redirect-uri-here'\n", + "client_id = 'your_client_id_here'\n", + "redirect_uri = 'your_redirect_uri_here'\n", "\n", "# Generate a random string as the code verifier for PKCE\n", "code_verifier = base64.urlsafe_b64encode(os.urandom(32)).decode().rstrip('=')\n", @@ -75,7 +75,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Enter the authorization code from the redirected URL: AQDoLR1429o_gm8DtTa3coWUM10983SDqvuKJk6gC5y3DRttdjt6a8mUXgUsnjO3HbFLFWh82YHLAE9vNRaNEUCcOMhvkEsZ2LrpWUTR7uJ9UgAcKXud6AbV0PPIHVlltQvjK7l-yA3gZbV8qIRjU95v92j_wBzhRThpix2bKBrxIW7rNMS6foUyhswoMOqKhtxL9GMgFEogAeoaH4bx1kPxFxR_M9ie3iuxldP09aR5Ll4G6yP2Rldhh3Y2nI6fnENeiEgGY6Sun6Zy8ltGvwAvwOVtjpkpdTUYSRdPIU2d6uDstq2xLXEmxCrFYRfCs_QLGAeXTGCd5Fhwd7UPKQOdSm84nC4kBWSQ0Q5s8WrVvmi-7Ukbm9HfCGUVeyq8573xMl4dx_yVxkTnVrw5f-QDSRtzuvtFz1JEtKgNwYGt6N7pwyyH_-cuwXT9JO-mQn2vDgpJFspWlsKHEA\n" + "Enter the authorization code from the redirected URL: AQAStg3Q4WeRQrpF3SPKBEkvqi5JKg657QzBXiyxz-X1j67qP7JMNJx4jIV_b3daIilopoca1NBA79he7H3RK-ZRAB1Go3rs3RsJf3sSTBIQFmuMJI9ngLM2DqNc60HkIliw8XpcmmjqHXnnb-ksLReC5lZ8ozdGs1R3j039_HAWOuQwdQuPnp9Y2K9FMmYE8wGs_MBchtRivOriywvZqE4zx9kP5AVrDgjbaHfaZyW06BKJkWR1iE2wTrS4fAFPbCVfQppmEPM7lILF_efYyjjZhaVoG_thLzi2ky3xCyWH3bFIOu6Is1RaNll2p8KjXkmFF2Wf4MyN5sxfEM-g6ttBEe9NDWsJwKUdb1-991IzwvLhTjP-jMRAb2sas4VdEW3FS6rpo14cVc2DKBSbB5Il8ACX6I9GaHpJcMOYm9paAJQRs7sH5Yxbjbk8KTMClhcMxE9PJ3nGHuHjig\n" ] } ], @@ -126,7 +126,7 @@ " break\n", " all_liked_songs.extend(liked_songs_data['items'])\n", " offset += limit\n", - "#print(all_liked_songs)" + "print(all_liked_songs)" ] }, { @@ -149,19 +149,41 @@ " Popularity = song['track']['popularity'] # Extract popularity score\n", " Track_id = song['track']['id'] # Extract track id\n", " Date_Added = song['added_at'] # Extract date added\n", - " \n", + " \n", + "#check if features key exists\n", + " features = song['track'].get('features', None)\n", + " if features:\n", + " danceability = features['danceability']\n", + " energy = features['energy']\n", + " valence = features['valence']\n", + " tempo = features['tempo']\n", + " else: # set to None if features not available\n", + " danceability = None\n", + " energy = None\n", + " valence = None\n", + " tempo = None\n", + " \n", + " # Still access genres \n", + " genres = ', '.join(song['track'].get('genres', []))\n", + "\n", + " \n", " song_data.append({\n", " 'Track': Track_name,\n", " 'Artist': Artist_names,\n", " 'Album': Album_name,\n", " 'Popularity': Popularity,\n", + " 'Danceability': danceability,\n", + " 'Energy': energy,\n", + " 'Valence': valence,\n", + " 'Tempo': tempo,\n", " 'id': Track_id,\n", - " 'Date Added': Date_Added\n", + " 'Date Added': Date_Added,\n", + " 'Genres': genres if genres else 'N/A'\n", " })\n", "\n", "#Create DataFrame from song data\n", "all_liked_songs_df = pd.DataFrame(song_data)\n", - "#print(song_data)" + "print(song_data)" ] }, { @@ -176,7 +198,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Playlist 'My_Playlist_Project' created successfully!\n" + "Playlist 'Repeat on Shuffle: My Top 100' created successfully!\n" ] } ], @@ -185,14 +207,14 @@ "sorted_songs = sorted(song_data, key=lambda x: x['Popularity'], reverse=True)\n", "\n", "# Create a new playlist\n", - "playlist_name = 'My_Playlist_Project'\n", + "playlist_name = 'Repeat on Shuffle: My Top 100'\n", "create_playlist_url = 'https://api.spotify.com/v1/me/playlists'\n", "create_playlist_headers = {\n", " 'Authorization': f'Bearer {access_token}',\n", " 'Content-Type': 'application/json'\n", "}\n", "create_playlist_data = {\n", - " 'name': 'My_Playlist_Project',\n", + " 'name': 'Repeat on Shuffle: My Top 100',\n", " 'public': True # Change to True if you want the playlist to be public\n", " }\n", "\n", @@ -210,7 +232,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "id": "1cc33f89", "metadata": {}, "outputs": [], @@ -234,9 +256,11 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 10, "id": "3d2ba2b0", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "data": { @@ -263,46 +287,71 @@ "
100 rows × 6 columns
\n", + "100 rows × 11 columns
\n", "" ], "text/plain": [ - " Track \\\n", - "0 Beautiful Things \n", - "1 Like That \n", - "2 euphoria \n", - "3 FE!N (feat. Playboi Carti) \n", - "4 Push Ups \n", - ".. ... \n", - "95 Jealous \n", - "96 Hours In Silence \n", - "97 Way 2 Sexy (with Future & Young Thug) \n", - "98 They Don't Care About Us \n", - "99 3001 \n", + " Track \\\n", + "0 Like That \n", + "1 Beautiful Things \n", + "2 euphoria \n", + "3 FE!N (feat. Playboi Carti) \n", + "4 Push Ups \n", + ".. ... \n", + "95 Hours In Silence \n", + "96 Way 2 Sexy (with Future & Young Thug) \n", + "97 They Don't Care About Us \n", + "98 I'm the One (feat. Justin Bieber, Quavo, Chanc... \n", + "99 Gently (feat. Bad Bunny) \n", "\n", - " Artist \\\n", - "0 [Benson Boone] \n", - "1 [Future, Metro Boomin, Kendrick Lamar] \n", - "2 [Kendrick Lamar] \n", - "3 [Travis Scott, Playboi Carti] \n", - "4 [Drake] \n", - ".. ... \n", - "95 [Labrinth] \n", - "96 [Drake, 21 Savage] \n", - "97 [Drake, Future, Young Thug] \n", - "98 [Michael Jackson] \n", - "99 [J. Cole] \n", + " Artist \\\n", + "0 [Future, Metro Boomin, Kendrick Lamar] \n", + "1 [Benson Boone] \n", + "2 [Kendrick Lamar] \n", + "3 [Travis Scott, Playboi Carti] \n", + "4 [Drake] \n", + ".. ... \n", + "95 [Drake, 21 Savage] \n", + "96 [Drake, Future, Young Thug] \n", + "97 [Michael Jackson] \n", + "98 [DJ Khaled, Justin Bieber, Quavo, Chance the R... \n", + "99 [Drake, Bad Bunny] \n", "\n", - " Album Popularity \\\n", - "0 Beautiful Things 96 \n", - "1 WE DON'T TRUST YOU 95 \n", - "2 euphoria 93 \n", - "3 UTOPIA 92 \n", - "4 Push Ups 91 \n", - ".. ... ... \n", - "95 Jealous - EP 73 \n", - "96 Her Loss 73 \n", - "97 Certified Lover Boy 73 \n", - "98 HIStory - PAST, PRESENT AND FUTURE - BOOK I 73 \n", - "99 Might Delete Later 72 \n", + " Album Popularity Danceability \\\n", + "0 WE DON'T TRUST YOU 96 None \n", + "1 Beautiful Things 96 None \n", + "2 euphoria 94 None \n", + "3 UTOPIA 93 None \n", + "4 Push Ups 91 None \n", + ".. ... ... ... \n", + "95 Her Loss 73 None \n", + "96 Certified Lover Boy 73 None \n", + "97 HIStory - PAST, PRESENT AND FUTURE - BOOK I 73 None \n", + "98 Grateful 72 None \n", + "99 For All The Dogs 72 None \n", "\n", - " id Date Added \n", - "0 6tNQ70jh4OwmPGpYy6R2o9 2024-03-22T22:44:09Z \n", - "1 2tudvzsrR56uom6smgOcSf 2024-04-01T06:47:28Z \n", - "2 77DRzu7ERs0TX3roZcre7Q 2024-05-01T07:22:08Z \n", - "3 42VsgItocQwOQC3XWZ8JNA 2023-12-31T10:03:50Z \n", - "4 3eh51r6rFWAlGQRlHx9QnQ 2024-04-19T17:22:58Z \n", - ".. ... ... \n", - "95 4G92yYrUs0cvY7G41YRI0z 2023-07-01T04:19:58Z \n", - "96 0sSRLXxknVTQDStgU1NqpY 2023-05-29T06:25:17Z \n", - "97 0k1WUmIRnG3xU6fvvDVfRG 2023-02-06T21:45:38Z \n", - "98 3wuCCNCnBhJlwkIJTBZFiv 2023-01-02T19:13:43Z \n", - "99 7JRwdwgeTrYkJvwmecrPSW 2024-04-07T10:43:25Z \n", + " Energy Valence Tempo id Date Added Genres \n", + "0 None None None 2tudvzsrR56uom6smgOcSf 2024-04-01T06:47:28Z N/A \n", + "1 None None None 6tNQ70jh4OwmPGpYy6R2o9 2024-03-22T22:44:09Z N/A \n", + "2 None None None 77DRzu7ERs0TX3roZcre7Q 2024-05-01T07:22:08Z N/A \n", + "3 None None None 42VsgItocQwOQC3XWZ8JNA 2023-12-31T10:03:50Z N/A \n", + "4 None None None 3eh51r6rFWAlGQRlHx9QnQ 2024-04-19T17:22:58Z N/A \n", + ".. ... ... ... ... ... ... \n", + "95 None None None 0sSRLXxknVTQDStgU1NqpY 2023-05-29T06:25:17Z N/A \n", + "96 None None None 0k1WUmIRnG3xU6fvvDVfRG 2023-02-06T21:45:38Z N/A \n", + "97 None None None 3wuCCNCnBhJlwkIJTBZFiv 2023-01-02T19:13:43Z N/A \n", + "98 None None None 3DXncPQOG4VBw3QHh3S817 2024-02-27T09:29:43Z N/A \n", + "99 None None None 6ZprqcYFC6D9eIZaCoqrSn 2023-12-16T06:53:42Z N/A \n", "\n", - "[100 rows x 6 columns]" + "[100 rows x 11 columns]" ] }, - "execution_count": 15, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -437,12 +521,24 @@ "#Create DataFrame from playlist created\n", "My_Playlist_Project = sorted_songs[:100]\n", "df = pd.DataFrame(My_Playlist_Project)\n", - "df" + "df\n", + "# Spotify lacks a direct genre API endpoint" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 11, + "id": "1426e88d", + "metadata": {}, + "outputs": [], + "source": [ + "# Saving DataFrame to csv\n", + "df.to_csv('My Top100 spotify playlist data.csv', index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, "id": "e08995ce", "metadata": {}, "outputs": [ @@ -452,30 +548,26 @@ "text": [ "User Playlists:\n", "Ughhh\n", - "My_Playlist_Project\n", + "Repeat on Shuffle: My Top 100\n", "Repeat Rewind\n", "On Repeat\n", "My recommendation playlist\n", "Horladee + D_grey\n", "Beast Mode Hip-Hop\n", - "My recommendation playlist\n", "Most Streamed Rap Songs on Spotify\n", "Dwizztheman\n", - "Get Turnt\n", "Your Top Songs 2023\n", "Motivation\n", "Comedy\n", "Jazz_Soul_Blues\n", "Rap Workout\n", "Blues Classics\n", - "Big Sean FT Jhene Aiko\n", "Favorites\n", "Billboard Hot 100\n", "The GRAMMYs Official Playlist\n", "I Love My '00s R&B\n", "Gospel\n", "RNB WORKOUT\n", - "Top 100 Nigeria on Apple Music\n", "Power Soundtrack\n", "J. Cole Mix\n", "I Love My '90s Hip-Hop\n", @@ -497,14 +589,6 @@ " for playlist in playlists_data['items']:\n", " print(playlist['name'])\n" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6380fab7", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {