Skip to content

Instantly share code, notes, and snippets.

@alex-phillips
Created June 24, 2024 18:37
Show Gist options
  • Save alex-phillips/2d23f9477b9b71c4cac8a73dad7718b7 to your computer and use it in GitHub Desktop.
Save alex-phillips/2d23f9477b9b71c4cac8a73dad7718b7 to your computer and use it in GitHub Desktop.
import os, argparse, spotipy, json
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth
for env_var in [
"SPOTIPY_CLIENT_ID",
"SPOTIPY_CLIENT_SECRET",
"SPOTIPY_REDIRECT_URI",
]:
if os.getenv(env_var, default=None) == None:
print(f"Environment variable {env_var} is not set")
quit(1)
parser = argparse.ArgumentParser()
parser.add_argument("--user", "-u", help="User to follow playlists of")
args = parser.parse_args()
scope = "playlist-modify-private playlist-modify-public"
# spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, open_browser=False))
playlists = spotify.user_playlists(args.user)
while playlists:
for i, playlist in enumerate(playlists["items"]):
print(
"%4d %s %s %s"
% (
i + 1 + playlists["offset"],
playlist["uri"],
playlist["name"],
playlist["id"],
)
)
print(json.dumps(playlist))
if playlist["public"] == True:
spotify.current_user_unfollow_playlist(playlist["id"])
if playlists["next"]:
playlists = sp.next(playlists)
else:
playlists = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment