Skip to content

Commit

Permalink
Merge pull request #2397 from jupyter-naas/2371-spotify-get-recently-…
Browse files Browse the repository at this point in the history
…played-tracks

feat: Get recently played tracks on Spotify
  • Loading branch information
FlorentLvr authored Nov 22, 2023
2 parents de1fcdc + 2b0b510 commit 476b060
Show file tree
Hide file tree
Showing 2 changed files with 543 additions and 0 deletions.
258 changes: 258 additions & 0 deletions Spotify/Spotify_Get_Recently_Played_Tracks.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ae4e5f38-8ec3-42df-a16d-b7ffe17ae776",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"8%\" alt=\"Spotify\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/Spotify.png\" style=\"border-radius: 15%\">"
]
},
{
"cell_type": "markdown",
"id": "8753843b-4031-412c-8318-ede7474d982f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Spotify - Get Recently Played Tracks"
]
},
{
"cell_type": "markdown",
"id": "24ae204c-3131-4a95-ba33-e52b015b7b80",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #spotify #api #web-api #recently-played #get #tracks #snippet"
]
},
{
"cell_type": "markdown",
"id": "34ba67bd-7d4a-4e47-a399-0f44d627d828",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Alton Liew](https://www.linkedin.com/in/alton-liew-749944182/)"
]
},
{
"cell_type": "markdown",
"id": "0d3ad6ac-143b-41f4-a5e0-fda6f692549e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-11-10 (Created: 2023-11-10)"
]
},
{
"cell_type": "markdown",
"id": "9bd83a34-4993-4925-9cf6-3252b043b1bf",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will get tracks from the current user's recently played tracks. It is useful for organizations to keep track of the user's music preferences."
]
},
{
"cell_type": "markdown",
"id": "024e529a-c9f2-4957-a81f-f7049aeeda87",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [Spotify Web API Reference - Get Recently Played Tracks](https://developer.spotify.com/documentation/web-api/reference/get-recently-played)\n",
"- [Spotify Authorization Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/)"
]
},
{
"cell_type": "markdown",
"id": "f17acda3-f0ff-42fa-9bcc-1f03ccd4e0ab",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "f376d209-a7be-4c4d-a6b8-4855563a3a31",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd2add74-2e0e-466c-a90c-a24f73cfe525",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" import spotipy\n",
"except:\n",
" !pip install spotipy --user\n",
" import spotipy\n",
"from spotipy.oauth2 import SpotifyOAuth\n",
"import naas"
]
},
{
"cell_type": "markdown",
"id": "2d20e100-eb10-45ae-9fca-2829e8b35cf8",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"**Mandatory**\n",
"- `client_id`: retrieve from Spotify Developers website. https://developer.spotify.com/\n",
"- `client_secret`: Spotify client secret from Spotify Developers website. https://developer.spotify.com/\n",
"- `redirect_uri`: redirect user to a page for authentication. Example: \"localhost: http://localhost:8888/callback\". Make sure this is the same URI used in the Spotify Developers website."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4fd005ee-de77-4c57-bec6-2e2ba039a801",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"client_id = naas.secret.get('SPOTIFY_CLIENT_ID')\n",
"client_secret = naas.secret.get('SPOTIFY_CLIENT_SECRET')\n",
"redirect_uri = naas.secret.get('SPOTIFY_REDIRECT_URI')"
]
},
{
"cell_type": "markdown",
"id": "3abae558-7ddd-476f-9116-f62aa1396dc8",
"metadata": {},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "21f1e2a3-35dd-4a95-ae44-6ab5bebaf29d",
"metadata": {},
"source": [
"### Get Recently Played Tracks"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81dfb3ae-61c9-4098-a390-8c2db0fb4950",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def get_recently_played_tracks(client_id, client_secret, redirect_uri):\n",
" # Set up the Spotify OAuth manager\n",
" sp_oauth = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope='user-read-recently-played')\n",
"\n",
" # Get the authorization token\n",
" token_info = sp_oauth.get_cached_token()\n",
"\n",
" # Use the obtained token to authenticate with the Spotify API\n",
" sp = spotipy.Spotify(auth=token_info['access_token'])\n",
"\n",
" # Get the current user's recently played tracks\n",
" recently_played = sp.current_user_recently_played()\n",
"\n",
" return recently_played\n",
"\n",
"recently_played = get_recently_played_tracks(client_id, client_secret, redirect_uri)\n",
"recently_played"
]
},
{
"cell_type": "markdown",
"id": "399a05c3-9180-49d3-8041-43facb9ca38e",
"metadata": {},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "05c038d8-04a0-4b6f-87f6-c9825e05ae88",
"metadata": {},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8913840c-1192-4335-8a11-5e9c1c513915",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"index = 1\n",
"for item in recently_played['items']:\n",
" track = item['track']\n",
" print(f\"{index}: {track['name']}, Artist: {track['artists'][0]['name']}, Album: {track['album']['name']}\")\n",
" index+=1"
]
}
],
"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

0 comments on commit 476b060

Please sign in to comment.