Created
March 8, 2023 23:20
-
-
Save peterc/852b18f566dd55e3be2a22184cabd3f6 to your computer and use it in GitHub Desktop.
Python app to take a list of songs and add them to a Spotify playlist
This file contains 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
# finds songs by names listed in a text file | |
# and adds them to a spotify playlist | |
import pprint | |
import re | |
import spotipy | |
from spotipy.oauth2 import SpotifyOAuth | |
from iteration_utilities import grouper | |
playlist = ... | |
scope = 'playlist-modify-public' | |
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope,open_browser=False,redirect_uri='http://example.com')) | |
f = open('songs.txt', 'r') | |
songs = f.readlines() | |
songs = filter(lambda line: len(line) >= 10, songs) | |
songs = map(lambda line: re.sub(r"\(.*?\)", "", line), songs) | |
songs = map(lambda line: ' '.join(re.findall(r'[A-Za-z’]+', line)), songs) | |
for song_set in grouper(songs, 10): | |
ids = [] | |
for song in song_set: | |
results = sp.search(song, limit=1) | |
if len(results['tracks']['items']) > 0: | |
track = results['tracks']['items'][0] | |
print(track['uri']) | |
ids.append(track['uri']) | |
sp.playlist_add_items(playlist, ids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment