Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2421 spotify get album tracks #2431

Merged
merged 3 commits into from
Dec 8, 2023
Merged
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
371 changes: 371 additions & 0 deletions Spotify/Spotify_Get_Album_Tracks.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,371 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a1bc9b4b-3da9-4d03-b259-5ff1e3bb18bf",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "d51a2ac8-dbe4-4a99-8cd1-0e67a2c5396e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Spotify - Get Album Tracks"
]
},
{
"cell_type": "markdown",
"id": "d7ffdc37-5a3f-4292-ab61-c6ba7dd1e2eb",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #spotify #api #album #tracks #get #web"
]
},
{
"cell_type": "markdown",
"id": "c5b61544-dc7f-4c02-adc3-460d2b52460f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)"
]
},
{
"cell_type": "markdown",
"id": "a337b9e6-e401-4ccd-93eb-475bc0dbfa17",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-12-08 (Created: 2023-12-01)"
]
},
{
"cell_type": "markdown",
"id": "08a63237-284b-4b61-9212-f8aa5f484911",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will get Spotify catalog information about an album’s tracks. Optional parameters can be used to limit the number of tracks returned."
]
},
{
"cell_type": "markdown",
"id": "e31f1464-01ef-4078-b26a-ec2b60a1407a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Spotify - Get an Album's Tracks](https://developer.spotify.com/documentation/web-api/reference/get-an-albums-tracks)\n",
"- [Spotify - Web API Overview](https://developer.spotify.com/documentation/web-api/)"
]
},
{
"cell_type": "markdown",
"id": "e00f1ca2-39e8-4c73-bdc4-d9a758338c68",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "c811dad9-7aa5-4005-b24d-1bfa6fe02c66",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "067a8d2b-9fcb-491e-975a-1c6f2ccd0a87",
"metadata": {
"execution": {
"iopub.execute_input": "2023-12-03T23:27:25.464664Z",
"iopub.status.busy": "2023-12-03T23:27:25.464454Z",
"iopub.status.idle": "2023-12-03T23:27:27.789060Z",
"shell.execute_reply": "2023-12-03T23:27:27.788332Z",
"shell.execute_reply.started": "2023-12-03T23:27:25.464602Z"
},
"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": "efc67696-83f4-4ee7-9c98-db30aff18dec",
"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 album. [Find the album ID](https://developer.spotify.com/console/get-album/)\n",
"- `limit`: The number of results you want to receive. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b0d03ea7-7900-4306-9f99-2a7d788a76c3",
"metadata": {
"execution": {
"iopub.execute_input": "2023-12-03T23:35:36.561477Z",
"iopub.status.busy": "2023-12-03T23:35:36.561243Z",
"iopub.status.idle": "2023-12-03T23:35:36.710004Z",
"shell.execute_reply": "2023-12-03T23:35:36.709366Z",
"shell.execute_reply.started": "2023-12-03T23:35:36.561453Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"client_id = naas.secret.get(\"SPOTIFY_CLIENT_ID\")\n",
"client_secret = naas.secret.get(\"SPOTIFY_CLIENT_SECRET\")\n",
"album_id = \"1gIC63gC3B7o7FfpPACZQJ\"\n",
"limit = 5"
]
},
{
"cell_type": "markdown",
"id": "d156bce7-fba5-48d5-812f-1fe62f9526b7",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "b5d2685a-6fcf-4c6a-9c31-133a56fe8557",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Get album tracks"
]
},
{
"cell_type": "markdown",
"id": "aeaef046-9179-4e13-ba8f-492ae13bf17a",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This function will get Spotify catalog information about an album’s tracks."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4aa966b-a88a-4023-b63a-fe64a1abdf5e",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"def get_album_tracks(client_id, client_secret, album_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_tracks(client_id, client_secret, album_id)\n",
"# pprint(data)"
]
},
{
"cell_type": "markdown",
"id": "b4369aed-7c1c-4954-ade8-4e6e98b7bb64",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "104b4fa0-9d31-4701-b8b5-5fa2b1c102bb",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "markdown",
"id": "19debebf-8c64-4f86-b55b-36bb66c6223e",
"metadata": {},
"source": [
"If data is present, print out the album information and tracks with their relevant ID."
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "d379078d-cc22-4c6e-aba1-365008049bb3",
"metadata": {
"execution": {
"iopub.execute_input": "2023-12-03T23:52:56.196554Z",
"iopub.status.busy": "2023-12-03T23:52:56.196325Z",
"iopub.status.idle": "2023-12-03T23:52:56.204437Z",
"shell.execute_reply": "2023-12-03T23:52:56.203725Z",
"shell.execute_reply.started": "2023-12-03T23:52:56.196530Z"
},
"papermill": {},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album Information:\n",
"Name: 4\n",
"Artist: Beyoncé\n"
]
},
{
"data": {
"text/html": [
"<img src=\"https://i.scdn.co/image/ab67616d0000b273ff5429125128b43572dbdccd\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Tracks:\n",
"1. Love On Top | ID: 1z6WtY7X4HQJvzxC4UgkSf\n",
"2. Party (feat. André 3000) | ID: 42qh86p7TLXyumxSHn65kc\n",
"3. Schoolin' Life | ID: 6JoIs4XLvBgjZOkKnyuPPv\n",
"4. Countdown | ID: 3axkNosdVQLZiq1HakuGhc\n",
"5. I Miss You | ID: 6Vv9wMxIc6OKRluQefy441\n"
]
}
],
"source": [
"if data:\n",
" album_info = {\n",
" \"Name\": data['name'],\n",
" \"Artist\": data['artists'][0]['name'],\n",
" \"Image\": data['images'][0].get(\"url\"),\n",
" \"Tracks\": [(track['name'], track['id']) for track in data['tracks']['items'][:limit]]\n",
" }\n",
" \n",
" print(\"Album Information:\")\n",
" for key, value in album_info.items():\n",
" if key == \"Image\":\n",
" display(Image(url=value))\n",
" elif key == \"Tracks\":\n",
" print(f\"\\n{key}:\")\n",
" for i, (track_name, track_id) in enumerate(value, start=1):\n",
" print(f\"{i}. {track_name} | ID: {track_id}\")\n",
" else:\n",
" print(f\"{key}: {value}\")\n",
"\n",
"else:\n",
" print(\"Failed to retrieve album information.\")"
]
},
{
"cell_type": "markdown",
"id": "3e43a89f-1eaf-438a-928b-b99a13dadc25",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
}
],
"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
}
Loading