Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
317 changes: 309 additions & 8 deletions lab-apis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,159 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "29694252-f217-454d-8881-681b2b6eeb1e",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'The Weeknd': ['Blinding Lights',\n",
" 'Starboy',\n",
" 'Save Your Tears',\n",
" 'The Hills',\n",
" \"Can't Feel My Face\"],\n",
" 'Drake': [\"God's Plan\",\n",
" 'One Dance',\n",
" 'Hotline Bling',\n",
" 'In My Feelings',\n",
" 'Started From the Bottom'],\n",
" 'Bad Bunny': ['Tití Me Preguntó',\n",
" 'Moscow Mule',\n",
" 'Me Porto Bonito',\n",
" 'Ojitos Lindos',\n",
" 'Yonaguni']}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"artists = [\"The Weeknd\", \"Drake\", \"Bad Bunny\"]\n",
"\n",
"def get_top_tracks(artist_name):\n",
" top_tracks_data = {\n",
" \"The Weeknd\": [\"Blinding Lights\", \"Starboy\", \"Save Your Tears\", \"The Hills\", \"Can't Feel My Face\"],\n",
" \"Drake\": [\"God's Plan\", \"One Dance\", \"Hotline Bling\", \"In My Feelings\", \"Started From the Bottom\"],\n",
" \"Bad Bunny\": [\"Tití Me Preguntó\", \"Moscow Mule\", \"Me Porto Bonito\", \"Ojitos Lindos\", \"Yonaguni\"]\n",
" }\n",
" \n",
" return top_tracks_data.get(artist_name, [])\n",
"\n",
"top_tracks_list = {}\n",
"\n",
"for artist in artists:\n",
" top_tracks_list[artist] = get_top_tracks(artist)\n",
"\n",
"top_tracks_list"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c03ae0ac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'The Weeknd': ['Bruno Mars', 'Post Malone', 'Doja Cat', 'SZA', 'Tory Lanez'],\n",
" 'Drake': ['Future', 'Lil Wayne', 'Travis Scott', 'Kanye West', 'J. Cole'],\n",
" 'Bad Bunny': ['J Balvin', 'Rauw Alejandro', 'Ozuna', 'Anuel AA', 'Karol G']}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def find_related_artists(artist_name):\n",
" related_artists_data = {\n",
" \"The Weeknd\": [\"Bruno Mars\", \"Post Malone\", \"Doja Cat\", \"SZA\", \"Tory Lanez\"],\n",
" \"Drake\": [\"Future\", \"Lil Wayne\", \"Travis Scott\", \"Kanye West\", \"J. Cole\"],\n",
" \"Bad Bunny\": [\"J Balvin\", \"Rauw Alejandro\", \"Ozuna\", \"Anuel AA\", \"Karol G\"]\n",
" }\n",
" \n",
" return related_artists_data.get(artist_name, [])\n",
"\n",
"related_artists_list = {}\n",
"\n",
"for artist in artists:\n",
" related_artists_list[artist] = find_related_artists(artist)\n",
"\n",
"related_artists_list"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cf189e0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Blinding Lights',\n",
" 'Starboy',\n",
" 'Save Your Tears',\n",
" 'The Hills',\n",
" \"Can't Feel My Face\",\n",
" 'Top tracks from Bruno Mars',\n",
" 'Top tracks from Post Malone',\n",
" 'Top tracks from Doja Cat',\n",
" 'Top tracks from SZA',\n",
" 'Top tracks from Tory Lanez',\n",
" \"God's Plan\",\n",
" 'One Dance',\n",
" 'Hotline Bling',\n",
" 'In My Feelings',\n",
" 'Started From the Bottom',\n",
" 'Top tracks from Future',\n",
" 'Top tracks from Lil Wayne',\n",
" 'Top tracks from Travis Scott',\n",
" 'Top tracks from Kanye West',\n",
" 'Top tracks from J. Cole',\n",
" 'Tití Me Preguntó',\n",
" 'Moscow Mule',\n",
" 'Me Porto Bonito',\n",
" 'Ojitos Lindos',\n",
" 'Yonaguni',\n",
" 'Top tracks from J Balvin',\n",
" 'Top tracks from Rauw Alejandro',\n",
" 'Top tracks from Ozuna',\n",
" 'Top tracks from Anuel AA',\n",
" 'Top tracks from Karol G']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"playlist = []\n",
"\n",
"for artist in artists:\n",
" playlist.extend(get_top_tracks(artist))\n",
" \n",
" related_artists = find_related_artists(artist)\n",
" \n",
" for related_artist in related_artists:\n",
" playlist.append(f\"Top tracks from {related_artist}\")\n",
"\n",
"playlist"
]
},
{
"cell_type": "markdown",
"id": "51e8379d",
"metadata": {},
"source": [
"# Your answer here"
"Spotify returned a 403 Forbidden error because the app requires an active subscription or permission to use the API endpoint. \n",
"For this reason, I completed the exercise using simulated dictionaries while keeping the same logic requested in the lab."
]
},
{
Expand Down Expand Up @@ -533,18 +680,172 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "ed92d961-9646-4375-a386-ccc320a958f5",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Top 5 Featured Playlists:\n",
"\n",
"Name: Today's Top Hits | ID: 001\n",
"Name: RapCaviar | ID: 002\n",
"Name: Viva Latino | ID: 003\n",
"Name: Rock Classics | ID: 004\n",
"Name: Chill Hits | ID: 005\n"
]
}
],
"source": [
"featured_playlists = [\n",
" {\"name\": \"Today's Top Hits\", \"id\": \"001\"},\n",
" {\"name\": \"RapCaviar\", \"id\": \"002\"},\n",
" {\"name\": \"Viva Latino\", \"id\": \"003\"},\n",
" {\"name\": \"Rock Classics\", \"id\": \"004\"},\n",
" {\"name\": \"Chill Hits\", \"id\": \"005\"}\n",
"]\n",
"\n",
"print(\"Top 5 Featured Playlists:\\n\")\n",
"\n",
"for playlist in featured_playlists:\n",
" print(f\"Name: {playlist['name']} | ID: {playlist['id']}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "61e821f8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Playlist Details:\n",
"\n",
"Name: Today's Top Hits\n",
"Description: The biggest hits right now.\n",
"Total Tracks: 10\n"
]
}
],
"source": [
"chosen_playlist = {\n",
" \"name\": \"Today's Top Hits\",\n",
" \"description\": \"The biggest hits right now.\",\n",
" \"total_tracks\": 10\n",
"}\n",
"\n",
"print(\"Playlist Details:\\n\")\n",
"\n",
"print(\"Name:\", chosen_playlist[\"name\"])\n",
"print(\"Description:\", chosen_playlist[\"description\"])\n",
"print(\"Total Tracks:\", chosen_playlist[\"total_tracks\"])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f71be3c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First 10 Tracks:\n",
"\n",
"Blinding Lights\n",
"As It Was\n",
"Flowers\n",
"One Dance\n",
"Tití Me Preguntó\n",
"Starboy\n",
"Bad Habit\n",
"Levitating\n",
"Stay\n",
"Shape of You\n"
]
}
],
"source": [
"tracks = [\n",
" \"Blinding Lights\",\n",
" \"As It Was\",\n",
" \"Flowers\",\n",
" \"One Dance\",\n",
" \"Tití Me Preguntó\",\n",
" \"Starboy\",\n",
" \"Bad Habit\",\n",
" \"Levitating\",\n",
" \"Stay\",\n",
" \"Shape of You\"\n",
"]\n",
"\n",
"print(\"First 10 Tracks:\\n\")\n",
"\n",
"for track in tracks:\n",
" print(track)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "eac6bbb5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Blinding Lights': ['The Weeknd'],\n",
" 'As It Was': ['Harry Styles'],\n",
" 'Flowers': ['Miley Cyrus'],\n",
" 'One Dance': ['Drake'],\n",
" 'Tití Me Preguntó': ['Bad Bunny'],\n",
" 'Starboy': ['The Weeknd', 'Daft Punk'],\n",
" 'Bad Habit': ['Steve Lacy'],\n",
" 'Levitating': ['Dua Lipa'],\n",
" 'Stay': ['The Kid LAROI', 'Justin Bieber'],\n",
" 'Shape of You': ['Ed Sheeran']}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"track_artists = {\n",
" \"Blinding Lights\": [\"The Weeknd\"],\n",
" \"As It Was\": [\"Harry Styles\"],\n",
" \"Flowers\": [\"Miley Cyrus\"],\n",
" \"One Dance\": [\"Drake\"],\n",
" \"Tití Me Preguntó\": [\"Bad Bunny\"],\n",
" \"Starboy\": [\"The Weeknd\", \"Daft Punk\"],\n",
" \"Bad Habit\": [\"Steve Lacy\"],\n",
" \"Levitating\": [\"Dua Lipa\"],\n",
" \"Stay\": [\"The Kid LAROI\", \"Justin Bieber\"],\n",
" \"Shape of You\": [\"Ed Sheeran\"]\n",
"}\n",
"\n",
"track_artists"
]
},
{
"cell_type": "markdown",
"id": "0357cce2",
"metadata": {},
"source": [
"# Your answer here"
"The Spotify API returned a 403 Forbidden error due to subscription or permission restrictions. \n",
"To complete the lab, simulated playlist and track data were used while maintaining the required structure and logic."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -558,7 +859,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down