diff --git a/Spotify/Spotify_Get_New_Releases.ipynb b/Spotify/Spotify_Get_New_Releases.ipynb new file mode 100644 index 0000000000..824a8b89f8 --- /dev/null +++ b/Spotify/Spotify_Get_New_Releases.ipynb @@ -0,0 +1,589 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b2ac8b90-3854-4d41-9bce-f451c6a363d1", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "f000c58a-0f9d-4f43-a2bc-6476f08929f3", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Spotify - Get New Releases" + ] + }, + { + "cell_type": "markdown", + "id": "e1c1176b-a310-4632-a1bb-0683ad474d65", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #spotify #web-api #get-new-releases #music #album #browse" + ] + }, + { + "cell_type": "markdown", + "id": "9968953e-3be2-456b-8d4b-ec89151fd8ce", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)" + ] + }, + { + "cell_type": "markdown", + "id": "38723d5b-7b6c-43de-8245-4a0f2af31051", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-12-08 (Created: 2023-12-01)" + ] + }, + { + "cell_type": "markdown", + "id": "4b9b1808-906d-4d0e-880a-d347cfa5cb8c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook will get a list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab). It is useful for organizations to keep track of new releases and to create playlists." + ] + }, + { + "cell_type": "markdown", + "id": "df920020-173f-4177-8a1c-3c0d97dc923a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n", + "- [Spotify Web API Reference - Get New Releases](https://developer.spotify.com/documentation/web-api/reference/get-new-releases)\n", + "- [Spotify Web API - Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)" + ] + }, + { + "cell_type": "markdown", + "id": "b807597c-2168-4ccb-ab0d-5e94ae1169af", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "e43c73a0-6da0-4c29-a8a1-c2083b6cf662", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "a06cb0c6-2104-481b-8675-9e1fc9454cf6", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-05T10:31:07.589481Z", + "iopub.status.busy": "2023-12-05T10:31:07.589183Z", + "iopub.status.idle": "2023-12-05T10:31:09.745240Z", + "shell.execute_reply": "2023-12-05T10:31:09.744552Z", + "shell.execute_reply.started": "2023-12-05T10:31:07.589401Z" + }, + "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": "60696794-254b-4673-85dd-75c5ab0d542c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n", + "- **`client_id`**: Client ID of the Spotify app. [Get Client ID](https://developer.spotify.com/documentation/general/guides/authorization-guide/)\n", + "- **`client_secret`**: Client Secret of the Spotify app. [Get Client Secret](https://developer.spotify.com/documentation/general/guides/authorization-guide/)\n", + "- **`country`**: [ISO 3166-1 alpha-2 country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c33fd41a-9d2a-4fb8-8cb2-764c51ac5e9c", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-05T10:31:09.748833Z", + "iopub.status.busy": "2023-12-05T10:31:09.748652Z", + "iopub.status.idle": "2023-12-05T10:31:09.940751Z", + "shell.execute_reply": "2023-12-05T10:31:09.940152Z", + "shell.execute_reply.started": "2023-12-05T10:31:09.748814Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "client_id = naas.secret.get(\"SPOTIFY_CLIENT_ID\")\n", + "client_secret = naas.secret.get(\"SPOTIFY_CLIENT_SECRET\")\n", + "country_code = \"AU\"" + ] + }, + { + "cell_type": "markdown", + "id": "b6dd8293-8a0d-451f-8fe2-952caf104b6a", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "ea0913d1-d83f-4e65-b20f-de7eeebbbab7", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Get new releases" + ] + }, + { + "cell_type": "markdown", + "id": "f0cc8f43-8dae-4691-917a-3e5201ad1006", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "This function will get a list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab)." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "7a9e89f0-12a1-4827-b836-90553f4f2e60", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-05T11:17:53.637533Z", + "iopub.status.busy": "2023-12-05T11:17:53.637306Z", + "iopub.status.idle": "2023-12-05T11:17:53.960777Z", + "shell.execute_reply": "2023-12-05T11:17:53.959996Z", + "shell.execute_reply.started": "2023-12-05T11:17:53.637509Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "def get_new_releases(client_id, client_secret, country_code):\n", + " data = None\n", + " sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))\n", + " try:\n", + " data = sp.new_releases(country=country_code, limit=10)\n", + " except spotipy.SpotifyException as e:\n", + " print(f\"Error retrieving new releases: {e}\")\n", + " return data\n", + " \n", + "data = get_new_releases(client_id, client_secret, country_code)" + ] + }, + { + "cell_type": "markdown", + "id": "b6e1d681-0107-4575-9772-1e81848355b0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "b0f6cddc-1e65-459b-91fe-2e8f7ac094ce", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "markdown", + "id": "b6bd9317-de56-4bc4-b671-433596d7a379", + "metadata": {}, + "source": [ + "If albums is present in data and if it is, it checks if items is within albums. If both checks are true, then print out the relevant information." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "cc874d6b-5587-4aba-8ece-51986f0a389b", + "metadata": { + "execution": { + "iopub.execute_input": "2023-12-05T11:08:03.336372Z", + "iopub.status.busy": "2023-12-05T11:08:03.336139Z", + "iopub.status.idle": "2023-12-05T11:08:03.363663Z", + "shell.execute_reply": "2023-12-05T11:08:03.362957Z", + "shell.execute_reply.started": "2023-12-05T11:08:03.336348Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "New Releases:\n", + "\n", + "\n", + "Name: Ahead of Time\n", + "Artists: L D R U\n", + "Release Date: 2023-11-30\n", + "ID: 5CPpqjZ4GMCmKd8qwsRxtj\n", + "External URLs: https://open.spotify.com/album/5CPpqjZ4GMCmKd8qwsRxtj\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: Growing Pains\n", + "Artists: Newport\n", + "Release Date: 2023-12-01\n", + "ID: 6G3EkYUBKmRVY3R3iauYbY\n", + "External URLs: https://open.spotify.com/album/6G3EkYUBKmRVY3R3iauYbY\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: Dorothy Painted Portraits\n", + "Artists: Adam Newling\n", + "Release Date: 2023-12-01\n", + "ID: 1pwZG3h3HUBMXpacg7MhIg\n", + "External URLs: https://open.spotify.com/album/1pwZG3h3HUBMXpacg7MhIg\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: The Great Escape\n", + "Artists: NO CIGAR\n", + "Release Date: 2023-12-01\n", + "ID: 6FFK9dODdAHQuxCaldqIhb\n", + "External URLs: https://open.spotify.com/album/6FFK9dODdAHQuxCaldqIhb\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: Upside\n", + "Artists: Drax Project\n", + "Release Date: 2023-11-24\n", + "ID: 3DJIsKB1vlhdn0hDAwqiTH\n", + "External URLs: https://open.spotify.com/album/3DJIsKB1vlhdn0hDAwqiTH\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: Like An Orchid\n", + "Artists: merci, mercy\n", + "Release Date: 2023-11-24\n", + "ID: 5rNbsQ5CQ8K3lPkpAiyjoS\n", + "External URLs: https://open.spotify.com/album/5rNbsQ5CQ8K3lPkpAiyjoS\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: True Love And Its Consequences\n", + "Artists: Memphis LK\n", + "Release Date: 2023-11-24\n", + "ID: 0RaCyomR79XSN0bkzaecsf\n", + "External URLs: https://open.spotify.com/album/0RaCyomR79XSN0bkzaecsf\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: Wabi Sabi\n", + "Artists: The Moving Stills\n", + "Release Date: 2023-11-24\n", + "ID: 0kPjOeepHuRmHR43CtmUlD\n", + "External URLs: https://open.spotify.com/album/0kPjOeepHuRmHR43CtmUlD\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: HATE 2 LUV U\n", + "Artists: AViVA\n", + "Release Date: 2023-11-24\n", + "ID: 5exZjAvyfhdDdssxOcWfRj\n", + "External URLs: https://open.spotify.com/album/5exZjAvyfhdDdssxOcWfRj\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "Name: BULLETPROOF\n", + "Artists: Yb.\n", + "Release Date: 2023-11-24\n", + "ID: 6DUgS2iZJnwzvjoRfgE3Xt\n", + "External URLs: https://open.spotify.com/album/6DUgS2iZJnwzvjoRfgE3Xt\n" + ] + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "if 'albums' in data and 'items' in data['albums']:\n", + " print(\"New Releases:\")\n", + " for album in data['albums']['items']:\n", + " releases_info = {\n", + " \"Name\": album['name'],\n", + " \"Artists\": album['artists'][0]['name'],\n", + " \"Release Date\": album['release_date'],\n", + " \"ID\": album['id'],\n", + " \"External URLs\": album['external_urls'].get(\"spotify\"),\n", + " \"Image\": album['images'][0].get(\"url\"),\n", + " }\n", + " print(\"\\n\")\n", + " for key, value in releases_info.items():\n", + " if key == \"Image\":\n", + " display(Image(url=value, width=300))\n", + " else:\n", + " print(f\"{key}: {value}\")\n", + "else:\n", + " print(\"Failed to retrieve new releases.\")" + ] + }, + { + "cell_type": "markdown", + "id": "4f354354-318b-4947-b3a6-f0f0665355e8", + "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 +}