-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2419 from jupyter-naas/2408-spotify-get-artist
2408 spotify get artist
- Loading branch information
Showing
1 changed file
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,299 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7eedaf4a-cc66-4062-b079-f6d1c2b15ce7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5f7d1345-70db-4c69-a857-6c056187e443", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# Spotify - Get Artist" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "424b9e14-6130-407c-bf91-eea7089eb572", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #spotify #api #getartist #webapi #reference #uniqueid #snippet" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e7cee5c3-0ca2-458a-abd8-11a2e956c408", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3585127f-0eb4-448a-83d2-19e7d1ab69fd", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-12-01 (Created: 2023-11-24)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "384c4308-0f5c-4263-8ff0-aa9689274c82", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook retrieves Spotify catalog information for a single artist identified by their unique Spotify ID." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8c611dd7-5f56-458a-bd9e-474d515b0b89", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- [Spotify Web API Reference - Get an Artist](https://developer.spotify.com/documentation/web-api/reference/get-an-artist)\n", | ||
"- [Spotify Developer Documentation](https://developer.spotify.com/documentation/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ae2b1138-de5e-41c7-9701-ba4f8176797a", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cbda9a83-e230-4c22-918f-3b33a63a5ec0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d1626996-b265-445d-842f-764d9f8a7778", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"try:\n", | ||
" import spotipy\n", | ||
"except:\n", | ||
" !pip install spotipy --user\n", | ||
" import spotipy\n", | ||
"from spotipy.oauth2 import SpotifyClientCredentials\n", | ||
"import naas\n", | ||
"from IPython.display import Image, display" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8690dee4-7735-4a22-863b-559a1cd39c4f", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- `client_id`: Your Spotify API client ID. [Get your client ID](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n", | ||
"- `client_secret`: Your Spotify API client secret. [Get your client secret](https://developer.spotify.com/documentation/general/guides/app-settings/#register-your-app)\n", | ||
"- `artist_id`: The unique Spotify ID for the artist. [Find the artist ID](https://developer.spotify.com/console/get-artist/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e6756fee-ce1a-4e99-88cf-ec27f70d65d6", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"client_id = naas.secret.get(\"SPOTIFY_CLIENT_ID\")\n", | ||
"client_secret = naas.secret.get(\"SPOTIFY_CLIENT_SECRET\")\n", | ||
"artist_id = \"6vWDO969PvNqNYHIOW5v0m\" #beyonce" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "afe5173d-a5e9-442f-9a8c-85076e01bbed", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5b9e4cc8-d26d-4437-86ec-465acbfd3e72", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Get artist" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "794416b0-f57c-464d-9f2a-e43aec0c82e3", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"* Retrieve Spotify catalog information for a single artist identified by their unique Spotify ID.\n", | ||
"* Sets up client with client id and client secret using spotipy library and fetches information." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "10ea4a46-c567-4fd5-8c3c-507c99b3b249", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_artist(client_id, client_secret, artist_id):\n", | ||
" data = None\n", | ||
" sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))\n", | ||
" try:\n", | ||
" data = sp.artist(artist_id)\n", | ||
" except spotipy.SpotifyException as e:\n", | ||
" print(f\"Error retrieving artist information: {e}\")\n", | ||
" return data\n", | ||
" \n", | ||
"data = get_artist(client_id, client_secret, artist_id)\n", | ||
"data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "669c4ea6-a758-479d-acb4-df2298120f1c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d57bf9c5-9fc1-4e24-bb5e-c1b20568fd1e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ddb5ab93-01b1-4398-92bb-5853f6a12b54", | ||
"metadata": {}, | ||
"source": [ | ||
"* Calls the function and passes in the client ID, secret, and artist ID as parameters.\n", | ||
"* If artist information is available, this will print out the required information." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d1b83be8-4b23-4078-8a7f-7f2810dfe114", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"if data:\n", | ||
" artist_info = {\n", | ||
" \"Name\": data['name'],\n", | ||
" \"Genres\": data['genres'],\n", | ||
" \"Popularity\": data['popularity'],\n", | ||
" \"Followers\": data['followers']['total'],\n", | ||
" \"External URLs\": data['external_urls'].get(\"spotify\"),\n", | ||
" \"Image\": data['images'][0].get(\"url\"),\n", | ||
" }\n", | ||
" print(\"Artist Information:\")\n", | ||
" for key, value in artist_info.items():\n", | ||
" if key == \"Image\":\n", | ||
" display(Image(url=value))\n", | ||
" else:\n", | ||
" print(f\"{key}: {value}\")\n", | ||
"else:\n", | ||
" print(\"Failed to retrieve artist information.\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |