diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/.cache b/.cache new file mode 100644 index 0000000..a7a5898 --- /dev/null +++ b/.cache @@ -0,0 +1 @@ +{"access_token": "BQB_g1ou-kO7wXzH3QDs79zKadU2lOf3q0Gr0fOdup1tUHDDmprNbKYlIIGYa4nQ9UVndRhfkx8RlLJgk8IRszNNipXO6JHs1ZqpfeLVp55cQ_q8qXykWT11VF7hqP4-vCY6vivw2x0", "token_type": "Bearer", "expires_in": 3600, "expires_at": 1748198117} \ No newline at end of file diff --git a/lab-apis.ipynb b/lab-apis.ipynb index e923554..990de6b 100644 --- a/lab-apis.ipynb +++ b/lab-apis.ipynb @@ -382,12 +382,192 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "29694252-f217-454d-8881-681b2b6eeb1e", "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "\n", + "CLIENT_ID = \"8af7bcd8b1114c6baaf42cb5a57b67a9\"\n", + "CLIENT_SECRET = \"80dae3cdd5654e4f858abbe98fd5a339\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "459d173a", + "metadata": {}, + "outputs": [], + "source": [ + "import spotipy\n", + "from spotipy.oauth2 import SpotifyClientCredentials\n", + "\n", + "#Initialize SpotiPy with user credentials\n", + "sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID,\n", + " client_secret=CLIENT_SECRET))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5cd5844d", + "metadata": {}, + "outputs": [], + "source": [ + "#STEP 1 AND 2 FROM CHALLENGE 1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "64f954a2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Top tracks for Oques Grasses:\n", + "- SORT DE TU\n", + "- La Gent Que Estimo\n", + "- Elefants\n", + "- TOCA\n", + "- Serem Ocells\n", + "\n", + "Top tracks for Ginestà:\n", + "- Tot el que ens queda\n", + "- Només viure\n", + "- Estimar-te com la terra\n", + "- Deixar-se anar la mà\n", + "- TOT ESTÀ PER FER\n", + "\n", + "Top tracks for Mushkaa:\n", + "- Sempapa\n", + "- SexeSexy\n", + "- Rifle Talibán - remix\n", + "- DIABLA\n", + "- 1 CUMBIA AMB EL GUILLEM (1vs1)\n", + "\n" + ] + } + ], + "source": [ + "from re import search\n", + "\n", + "#1.list with my 3 favourtie artists\n", + "artists=['Oques Grasses', 'Ginestà', 'Mushkaa']\n", + "\n", + "#list to populate with the top 5 tracks for each of the 3 artists using the function\n", + "top_tracks_list=[]\n", + "\n", + "#2. Fetch the top 5 tracks for each artist\n", + "def get_top_tracks():\n", + " for artist in artists:\n", + " # Search for the artist's Spotify ID using their name\n", + " result = sp.search(q=artist, type='artist', limit=1)\n", + " artist_id = result['artists']['items'][0]['id'] # Get the artist's ID\n", + "\n", + " # Get the top tracks for the artist, passing the artist's ID and a country code\n", + " top_tracks = sp.artist_top_tracks(artist_id, country='ES')['tracks'][:5] # Get top tracks for the artist\n", + " top_tracks_list.append(top_tracks) # Add the tracks to the list\n", + "\n", + "# Print the top tracks for each artist\n", + " for i, tracks in enumerate(top_tracks_list):\n", + " print(f\"Top tracks for {artists[i]}:\")\n", + " for track in tracks:\n", + " print(f\"- {track['name']}\")\n", + " print()\n", + "\n", + "# Call the function\n", + "get_top_tracks()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "45216e4e", + "metadata": {}, + "outputs": [], + "source": [ + "import spotipy as sp\n", + "from spotipy.oauth2 import SpotifyClientCredentials\n", + "import os\n", + "from dotenv import load_dotenv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73dbd374", + "metadata": {}, + "outputs": [], + "source": [ + "#STEP 3 FROM CHALLENGE 1. \n", + "# Discover Related Artists:\n", + " #- Write a function named `find_related_artists`.\n", + " #- This function should accept an artist's name and return the names of the first 5 artists related to the provided artist.\n", + " #- Store the results in a list named `related_artists_list`." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "3a68705c", + "metadata": {}, + "outputs": [], + "source": [ + "def find_related_artists(artist_name):\n", + " try:\n", + " results = sp.search(q=artist_name, type='artist', limit=1)\n", + " artist_items = results['artists']['items']\n", + " if not artist_items:\n", + " print(f\"No artist found for: {artist_name}\")\n", + " return []\n", + "\n", + " artist_id = artist_items[0]['id']\n", + " related_artists_data = sp.artist_related_artists(artist_id)\n", + " related_names = [artist['name'] for artist in related_artists_data['artists'][:5]]\n", + " return related_names\n", + "\n", + " except spotipy.exceptions.SpotifyException as e:\n", + " print(f\"Spotify error with artist '{artist_name}': {e}\")\n", + " return []\n", + "\n", + " except Exception as e:\n", + " print(f\"Unexpected error with artist '{artist_name}': {e}\")\n", + " return []" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "163fa5a6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unexpected error with artist 'Oques Grasses': module 'spotipy' has no attribute 'search'\n", + "Unexpected error with artist 'Ginestà': module 'spotipy' has no attribute 'search'\n", + "Unexpected error with artist 'Mushkaa': module 'spotipy' has no attribute 'search'\n" + ] + }, + { + "data": { + "text/plain": [ + "[[], [], []]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "related_artists_list = [find_related_artists(artist) for artist in artists]\n", + "related_artists_list" ] }, { @@ -404,10 +584,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "25fb0cf8-c13a-41b0-b8f8-7e0700fd1e41", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'spotipy' has no attribute 'featured_playlists'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[14]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43msp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfeatured_playlists\u001b[49m() \u001b[38;5;66;03m# We get a playlist id of a playlist we like\u001b[39;00m\n", + "\u001b[31mAttributeError\u001b[39m: module 'spotipy' has no attribute 'featured_playlists'" + ] + } + ], "source": [ "sp.featured_playlists() # We get a playlist id of a playlist we like" ] @@ -431,10 +623,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "46d35121-9256-4cf4-81f5-118b87f7af32", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'spotipy' has no attribute 'playlist'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[17]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m playlist_id = \u001b[33m\"\u001b[39m\u001b[33m37i9dQZF1DXd9zR7tdziuQ\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m playlist = \u001b[43msp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mplaylist\u001b[49m(playlist_id)\n", + "\u001b[31mAttributeError\u001b[39m: module 'spotipy' has no attribute 'playlist'" + ] + } + ], "source": [ "playlist_id = \"37i9dQZF1DXd9zR7tdziuQ\"\n", "playlist = sp.playlist(playlist_id)" @@ -442,10 +646,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "5260f67f-6024-4fee-8449-30904f03bf76", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'playlist' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[18]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mplaylist\u001b[49m[\u001b[33m'\u001b[39m\u001b[33mname\u001b[39m\u001b[33m'\u001b[39m]) \u001b[38;5;66;03m# Print the playlist's name\u001b[39;00m\n\u001b[32m 2\u001b[39m \u001b[38;5;28mprint\u001b[39m(playlist[\u001b[33m'\u001b[39m\u001b[33mdescription\u001b[39m\u001b[33m'\u001b[39m]) \u001b[38;5;66;03m# Print the playlist's description\u001b[39;00m\n", + "\u001b[31mNameError\u001b[39m: name 'playlist' is not defined" + ] + } + ], "source": [ "print(playlist['name']) # Print the playlist's name\n", "print(playlist['description']) # Print the playlist's description" @@ -462,10 +678,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "69c78f8d-7e6a-4d15-bcbb-fc93edb82433", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'spotipy' has no attribute 'playlist_tracks'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m tracks = \u001b[43msp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mplaylist_tracks\u001b[49m(playlist_id)\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m track \u001b[38;5;129;01min\u001b[39;00m tracks[\u001b[33m'\u001b[39m\u001b[33mitems\u001b[39m\u001b[33m'\u001b[39m]:\n\u001b[32m 3\u001b[39m \u001b[38;5;28mprint\u001b[39m(track[\u001b[33m'\u001b[39m\u001b[33mtrack\u001b[39m\u001b[33m'\u001b[39m][\u001b[33m'\u001b[39m\u001b[33mname\u001b[39m\u001b[33m'\u001b[39m]) \u001b[38;5;66;03m# Print each track's name\u001b[39;00m\n", + "\u001b[31mAttributeError\u001b[39m: module 'spotipy' has no attribute 'playlist_tracks'" + ] + } + ], "source": [ "tracks = sp.playlist_tracks(playlist_id)\n", "for track in tracks['items']:\n", @@ -533,18 +761,86 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "ed92d961-9646-4375-a386-ccc320a958f5", "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "module 'spotipy' has no attribute 'featured_playlists'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# 1. Featured Exploration:\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m featured_playlists = \u001b[43msp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfeatured_playlists\u001b[49m(limit=\u001b[32m5\u001b[39m)\n\u001b[32m 5\u001b[39m \u001b[38;5;66;03m# Get the first 5 playlists\u001b[39;00m\n\u001b[32m 6\u001b[39m playlists = featured_playlists[\u001b[33m'\u001b[39m\u001b[33mplaylists\u001b[39m\u001b[33m'\u001b[39m][\u001b[33m'\u001b[39m\u001b[33mitems\u001b[39m\u001b[33m'\u001b[39m]\n", + "\u001b[31mAttributeError\u001b[39m: module 'spotipy' has no attribute 'featured_playlists'" + ] + } + ], + "source": [ + "# 1. Featured Exploration:\n", + "\n", + "featured_playlists = sp.featured_playlists(limit=5)\n", + "\n", + "# Get the first 5 playlists\n", + "playlists = featured_playlists['playlists']['items']\n", + "top_5_playlists=[]\n", + "\n", + "# Loop through them and extract name and id\n", + "for playlist in playlists:\n", + " name = playlist['name']\n", + " playlist_id = playlist['id']\n", + " top_5_playlists.append((name, playlist_id))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4eb398f6", + "metadata": {}, + "outputs": [], + "source": [ + "#2. Deep dive\n", + "chosen_playlist_id = top_5_playlists[0][1] # Pick first one\n", + "chosen_playlist_data = sp.playlist(chosen_playlist_id)\n", + "\n", + "playlist_name = chosen_playlist_data['name']\n", + "playlist_description = chosen_playlist_data['description']\n", + "playlist_total_tracks = chosen_playlist_data['tracks']['total']\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8ea82c2e", + "metadata": {}, + "outputs": [], + "source": [ + "#3. Track-tastic\n", + "tracks_data = sp.playlist_items(chosen_playlist_id, limit=10)\n", + "first_10_tracks = [item['track']['name'] for item in tracks_data['items']]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f89e9afc", + "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "#4. Artistic flair\n", + "track_artist_map = {}\n", + "\n", + "for item in tracks_data['items']:\n", + " track_name = item['track']['name']\n", + " artists = [artist['name'] for artist in item['track']['artists']]\n", + " track_artist_map[track_name] = artists" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -558,7 +854,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,