Created
December 29, 2021 05:19
-
-
Save preraku/c53dd924f1519f4734cc7ff411bbb40b to your computer and use it in GitHub Desktop.
Get Spotify track preview URLs from Spotify playlists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import requests | |
GET_NEW_ACCESS_TOKEN = False | |
if GET_NEW_ACCESS_TOKEN: | |
# Create and send an authorization request to Spotify | |
client_id = 'CLIENT_ID' | |
client_secret = 'CLIENT_SECRET' | |
auth_url = 'https://accounts.spotify.com/api/token' | |
auth_b64_str = (base64.b64encode((client_id + ':' + client_secret).encode('utf-8'))).decode('utf-8') | |
content_type = 'application/x-www-form-urlencoded' | |
headers = {'Authorization': 'Basic ' + auth_b64_str, 'Content-Type': content_type} | |
data = {'grant_type': 'client_credentials'} | |
response = requests.post(auth_url, headers=headers, data=data) | |
access_token = response.json()['access_token'] | |
print("=" * 35 + " NEW ACCESS TOKEN " + "=" * 35) | |
print() | |
print(access_token) | |
print() | |
print("=" * 88) | |
else: | |
access_token = "ACCESS_TOKEN" | |
spotify_auth_header = {"Authorization": "Bearer " + access_token} | |
# Retrieve information about a track using the Spotify Web API Get a track endpoint | |
# song_id = "2TpxZ7JUBn3uw46aR7qd6V" # All I Want by Tania Bowra (has preview URL). | |
# song_id = "3uCkIqD0VzQUijbs8WIizs" # These Days by Nico does not have a preview URL. | |
# url = f'https://api.spotify.com/v1/tracks/{song_id}' | |
# response = requests.get(url, headers=spotify_auth_header) | |
# song_name = response.json()['name'] | |
# arist_name = response.json()['artists'][0]['name'] | |
# preview_url = response.json()['preview_url'] | |
# print(f'{song_name} by {arist_name}: {preview_url}') | |
# exit() | |
# Get track info from a playlist the user provides | |
# Ask the user for a URL to their playlist | |
while(True): | |
playlist_url = input("Enter the URL of the playlist: ") | |
# Get the playlist ID from the URL | |
# e.g. for https://open.spotify.com/playlist/6BXoXd2ytpUnRiotc6nomC, | |
# the playlist ID is 6BXoXd2ytpUnRiotc6nomC | |
playlist_id = playlist_url.split('/')[-1] | |
# Confirm the playlist ID. Ask the user to confirm. | |
print(f"The playlist ID is {playlist_id}") | |
confirm = input("Is this correct? (y/n) ") | |
if confirm in ('y', 'Y'): | |
break | |
url = f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks" | |
response = requests.get(url, headers=spotify_auth_header) | |
songs = response.json()['items'] | |
for song in songs: | |
track_name = song['track']['name'] | |
artist_name = song['track']['artists'][0]['name'] | |
preview_url = song['track']['preview_url'] | |
print(f"{track_name} by {artist_name}: \n\t{preview_url}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment