Skip to content

Commit

Permalink
Merge pull request #2430 from jupyter-naas/2420-spotify-get-album
Browse files Browse the repository at this point in the history
2420 spotify get album
  • Loading branch information
FlorentLvr authored Dec 5, 2023
2 parents 69c91b1 + 36d7928 commit 6a11405
Showing 1 changed file with 285 additions and 0 deletions.
285 changes: 285 additions & 0 deletions Spotify/Spotify_Get_Album.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "aad19294-8b09-4b6b-9780-e4cc631ac2d9",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "0542bfa0-f364-4074-8cd5-8d9f6953d4c9",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Spotify - Get Album"
]
},
{
"cell_type": "markdown",
"id": "6233f3e3-4d2a-4a13-ab18-78eaa505723f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #spotify #api #album #get #web #catalog #snippet"
]
},
{
"cell_type": "markdown",
"id": "d5c19f09-036e-4897-bfc7-3b7bfc4b7f2d",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)"
]
},
{
"cell_type": "markdown",
"id": "226c745a-7b10-44a1-942c-bfb2a664dc9b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-12-05 (Created: 2023-12-01)"
]
},
{
"cell_type": "markdown",
"id": "d067b11d-7a36-4668-beb7-f6edb1fc5ca4",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook retrieves Spotify catalog information for a single album."
]
},
{
"cell_type": "markdown",
"id": "020bc59a-6dfb-4fe2-aad8-49a9e6ef9b7f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Spotify Web API Reference - Get an Album](https://developer.spotify.com/documentation/web-api/reference/get-an-album)\n",
"- [Spotify Web API Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)"
]
},
{
"cell_type": "markdown",
"id": "9a70e2e5-a127-4faf-865d-6712ac66f9b2",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "5ade603e-9598-4cc3-bbe4-e9a3abee1c35",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16966ab8-0d84-4445-aac1-d6f2466573f6",
"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 pprint import pprint\n",
"from IPython.display import Image, display"
]
},
{
"cell_type": "markdown",
"id": "b85875fc-3816-4951-a057-18e525abf100",
"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",
"- `album_id`: The unique Spotify ID for the album. [Find the album ID](https://developer.spotify.com/documentation/web-api/reference/get-an-album)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af6f0cdb-8d28-4eca-b647-944cb7101e4e",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"client_id = naas.secret.get(\"SPOTIFY_CLIENT_ID\")\n",
"client_secret = naas.secret.get(\"SPOTIFY_CLIENT_SECRET\")\n",
"album_id = \"7ivbFszr1TbVadj89BIy1y\" #1gIC63gC3B7o7FfpPACZQJ"
]
},
{
"cell_type": "markdown",
"id": "a58c7275-47b2-4bd1-930c-d12c71fddd34",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "06746239-063c-4896-852c-44105be8571a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Get album\n",
"* Retrieve Spotify catalog information for an album identified by their unique Spotify ID.\n",
"* Sets up client with client id and client secret using spotipy library and fetches album information."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73784996-f674-40f7-a2ad-1c86b033668d",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"def get_album(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.album(album_id)\n",
" except spotipy.SpotifyException as e:\n",
" print(f\"Error retrieving album information: {e}\")\n",
" return data\n",
" \n",
"data = get_album(client_id, client_secret, album_id)\n",
"pprint(data)"
]
},
{
"cell_type": "markdown",
"id": "73b76c2c-90d9-4284-8350-659597a7bf3d",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "3547cd1d-dfb6-4320-8368-33e5c7175715",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result\n",
"* If album information is available, this will print out the required information."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1ed1991-1809-4bb1-a7b7-451f0e8f7f58",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"if data:\n",
" album_info = {\n",
" \"Name\": data['name'],\n",
" \"Artist\": data['artists'][0]['name'],\n",
" \"Release date\": data['release_date'],\n",
" \"Total Tracks\": data['total_tracks'],\n",
" \"Popularity\": data['popularity'],\n",
" \"External URLs\": data['external_urls'].get(\"spotify\"),\n",
" \"Copyrights\": data['copyrights'][0]['text'],\n",
" \"Image\": data['images'][0].get(\"url\"),\n",
" }\n",
" print(\"Album Information\")\n",
" print(\"-----------------\")\n",
" for key, value in album_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 album 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
}

0 comments on commit 6a11405

Please sign in to comment.