Skip to content

Commit

Permalink
feat: Get recently played tracks on Spotify
Browse files Browse the repository at this point in the history
This is a template used to get the recently played tracks on your Spotify account using client ID and client secret on the Spotify developer's page.
altonliew11 committed Nov 20, 2023
1 parent 6d51df9 commit 1528728
Showing 1 changed file with 133 additions and 14 deletions.
147 changes: 133 additions & 14 deletions Spotify/Spotify_Get_Recently_Played_Tracks.ipynb
Original file line number Diff line number Diff line change
@@ -103,16 +103,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "dd2add74-2e0e-466c-a90c-a24f73cfe525",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T18:17:18.502959Z",
"iopub.status.busy": "2023-11-19T18:17:18.502693Z",
"iopub.status.idle": "2023-11-19T18:17:20.718907Z",
"shell.execute_reply": "2023-11-19T18:17:20.718274Z",
"shell.execute_reply.started": "2023-11-19T18:17:18.502885Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import requests\n",
"import json"
"try:\n",
" import spotipy\n",
"except:\n",
" !pip install spotipy --user\n",
" import spotipy\n",
"from spotipy.oauth2 import SpotifyOAuth\n",
"import naas"
]
},
{
@@ -124,21 +136,31 @@
},
"source": [
"### Setup variables\n",
"- **token**: Access token to be used for authorization. [Follow this guide to get the token](https://developer.spotify.com/documentation/general/guides/authorization-guide/)"
"### Mandatory\n",
"- `client_id` & `client_secret`: retrieve from Spotify Developers website.\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,
"execution_count": 2,
"id": "4fd005ee-de77-4c57-bec6-2e2ba039a801",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T18:17:20.720745Z",
"iopub.status.busy": "2023-11-19T18:17:20.720282Z",
"iopub.status.idle": "2023-11-19T18:17:21.008286Z",
"shell.execute_reply": "2023-11-19T18:17:21.005527Z",
"shell.execute_reply.started": "2023-11-19T18:17:20.720712Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"token = \"\"\n",
"limit = 10"
"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')"
]
},
{
@@ -159,11 +181,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "81dfb3ae-61c9-4098-a390-8c2db0fb4950",
"metadata": {},
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T18:17:21.010482Z",
"iopub.status.busy": "2023-11-19T18:17:21.009954Z",
"iopub.status.idle": "2023-11-19T18:17:21.015733Z",
"shell.execute_reply": "2023-11-19T18:17:21.014972Z",
"shell.execute_reply.started": "2023-11-19T18:17:21.010449Z"
},
"tags": []
},
"outputs": [],
"source": []
"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"
]
},
{
"cell_type": "markdown",
@@ -183,11 +229,84 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "8913840c-1192-4335-8a11-5e9c1c513915",
"metadata": {},
"outputs": [],
"source": []
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T18:17:21.017731Z",
"iopub.status.busy": "2023-11-19T18:17:21.017182Z",
"iopub.status.idle": "2023-11-19T18:17:21.475869Z",
"shell.execute_reply": "2023-11-19T18:17:21.475228Z",
"shell.execute_reply.started": "2023-11-19T18:17:21.017701Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1: dothatshit!, Artist: Playboi Carti, Album: Playboi Carti\n",
"2: Choppa Won't Miss (feat. Young Thug), Artist: Playboi Carti, Album: Die Lit\n",
"3: Foreign, Artist: Playboi Carti, Album: Die Lit\n",
"4: Beno!, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"5: Meh, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"6: Poke It Out (with Nicki Minaj), Artist: Playboi Carti, Album: Die Lit\n",
"7: Over, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"8: Yah Mean, Artist: Playboi Carti, Album: Playboi Carti\n",
"9: FlatBed Freestyle, Artist: Playboi Carti, Album: Die Lit\n",
"10: Die4Guy, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"11: Let It Go, Artist: Playboi Carti, Album: Playboi Carti\n",
"12: Go2DaMoon (feat. Kanye West), Artist: Playboi Carti, Album: Whole Lotta Red\n",
"13: Teen X (feat. Future), Artist: Playboi Carti, Album: Whole Lotta Red\n",
"14: Sky, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"15: Fell In Luv (feat. Bryson Tiller), Artist: Playboi Carti, Album: Die Lit\n",
"16: Vamp Anthem, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"17: Miss The Rage, Artist: Trippie Redd, Album: Trip At Knight\n",
"18: Rockstar Made, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"19: New Tank, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"20: Mileage (feat. Chief Keef), Artist: Playboi Carti, Album: Die Lit\n",
"21: @ MEH, Artist: Playboi Carti, Album: @ MEH\n",
"22: R.I.P. Fredo (feat. Young Nudy) - Notice Me, Artist: Playboi Carti, Album: Die Lit\n",
"23: ILoveUIHateU, Artist: Playboi Carti, Album: Whole Lotta Red\n",
"24: wokeuplikethis*, Artist: Playboi Carti, Album: Playboi Carti\n",
"25: Shoota (feat. Lil Uzi Vert), Artist: Playboi Carti, Album: Die Lit\n",
"26: Flex, Artist: Playboi Carti, Album: Playboi Carti\n",
"27: New Choppa, Artist: Playboi Carti, Album: Playboi Carti\n",
"28: Lean 4 Real (feat. Skepta), Artist: Playboi Carti, Album: Die Lit\n",
"29: R.I.P., Artist: Playboi Carti, Album: Die Lit\n",
"30: Location, Artist: Playboi Carti, Album: Playboi Carti\n",
"31: Wildin', Artist: Berhana, Album: Wildin'\n",
"32: Grey Luh, Artist: Berhana, Album: Berhana\n",
"33: Streetcar, Artist: Daniel Caesar, Album: Pilgrim's Paradise\n",
"34: Take Me Away (feat. Syd), Artist: Daniel Caesar, Album: Freudian\n",
"35: Donchano, Artist: Steve Lacy, Album: The Lo-Fis\n",
"36: Ghost in the Machine (feat. Phoebe Bridgers), Artist: SZA, Album: SOS\n",
"37: Easy v2, Artist: Baird, Album: Easy v2\n",
"38: SANTA MONICA, Artist: Aries, Album: WELCOME HOME\n",
"39: The Need to Know (feat. SZA), Artist: Wale, Album: The Album About Nothing\n",
"40: Blessed, Artist: Daniel Caesar, Album: Freudian\n",
"41: Legends, Artist: Juice WRLD, Album: Legends\n",
"42: Sick, Artist: Dominic Fike, Album: Sunburn\n",
"43: INDIGO GIRL, Artist: A.CHAL, Album: EXOTIGAZ\n",
"44: THE zone~, Artist: Lil Yachty, Album: Let’s Start Here.\n",
"45: Life is Like a Dice Game - Spotify Singles, Artist: Nas, Album: Life is Like a Dice Game - Spotify Singles\n",
"46: SUPERPOSITION, Artist: Daniel Caesar, Album: CASE STUDY 01\n",
"47: GEEKIN, Artist: Ski Mask The Slump God, Album: BEWARE THE BOOK OF ELI\n",
"48: Ring Ring (with Rvssian feat. Clever), Artist: Juice WRLD, Album: Death Race For Love (Bonus Track Version)\n",
"49: Cherry Hill, Artist: Russ, Album: There's Really A Wolf\n",
"50: Matt Hardy 999, Artist: Trippie Redd, Album: Trip At Knight\n"
]
}
],
"source": [
"index = 1\n",
"recently_played = get_recently_played_tracks(client_id, client_secret, redirect_uri)\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": {

0 comments on commit 1528728

Please sign in to comment.