Created
October 19, 2024 21:52
-
-
Save kevinthecity/ccc97515ad77f7e1e80a3832248f70ca to your computer and use it in GitHub Desktop.
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
from flask import Blueprint, redirect, request, session, render_template | |
from spotipy.oauth2 import SpotifyOAuth | |
import spotipy | |
import random | |
# Spotify credentials | |
SPOTIPY_CLIENT_ID = "your client id" | |
SPOTIPY_CLIENT_SECRET = "your secret" | |
SPOTIPY_REDIRECT_URI = "http://localhost:5000/spotify/callback" | |
scope = "playlist-modify-public playlist-modify-private playlist-read-private" | |
spotify = Blueprint("spotify", __name__) | |
# Home route | |
@spotify.route("/spotify") | |
def index(): | |
auth_url = SpotifyOAuth( | |
client_id=SPOTIPY_CLIENT_ID, | |
client_secret=SPOTIPY_CLIENT_SECRET, | |
redirect_uri=SPOTIPY_REDIRECT_URI, | |
scope=scope, | |
).get_authorize_url() | |
return render_template("spotify.html", auth_url=auth_url) | |
# Redirect to Spotify login | |
@spotify.route("/spotify/callback") | |
def callback(): | |
sp_oauth = SpotifyOAuth( | |
client_id=SPOTIPY_CLIENT_ID, | |
client_secret=SPOTIPY_CLIENT_SECRET, | |
redirect_uri=SPOTIPY_REDIRECT_URI, | |
scope=scope, | |
) | |
session.clear() | |
code = request.args.get("code") | |
token_info = sp_oauth.get_access_token(code) | |
session["token_info"] = token_info | |
return redirect("/playlists") | |
# Show playlists | |
@spotify.route("/playlists") | |
def playlists(): | |
sp = spotipy.Spotify(auth=session.get("token_info")["access_token"]) | |
playlists = sp.current_user_playlists() | |
return render_template("playlists.html", playlists=playlists["items"]) | |
# Generate randomized playlist | |
@spotify.route("/randomize", methods=["POST"]) | |
def randomize_playlist(): | |
playlist_id = request.form.get("playlist_id") | |
sp = spotipy.Spotify(auth=session.get("token_info")["access_token"]) | |
# Get the original playlist and its tracks | |
original_playlist = sp.playlist(playlist_id) | |
original_playlist_name = original_playlist["name"] | |
tracks = original_playlist["tracks"]["items"] | |
track_uris = [track["track"]["uri"] for track in tracks] | |
# Shuffle the tracks | |
random.shuffle(track_uris) | |
# Generate a new playlist with a random color name appended | |
colors = [ | |
"Red", | |
"Blue", | |
"Green", | |
"Yellow", | |
"Magenta", | |
"Cyan", | |
"Orange", | |
"Purple", | |
"Pink", | |
] | |
random_color = random.choice(colors) | |
new_playlist_name = f"[Randomized] {original_playlist_name} - {random_color}" | |
new_playlist = sp.user_playlist_create( | |
user=sp.current_user()["id"], | |
name=new_playlist_name, | |
public=False, | |
description="Generated by Playlist Randomizer", | |
) | |
# Add the shuffled tracks to the new playlist | |
sp.playlist_add_items(new_playlist["id"], track_uris) | |
# Create link to new playlist | |
new_playlist_url = new_playlist["external_urls"]["spotify"] | |
return render_template( | |
"playlist_created.html", | |
new_playlist_name=new_playlist_name, | |
new_playlist_url=new_playlist_url, | |
) | |
@spotify.route("/delete_generated_playlists") | |
def delete_generated_playlists(): | |
sp = spotipy.Spotify(auth=session.get("token_info")["access_token"]) | |
playlists = sp.current_user_playlists() | |
deleted_playlists = [] | |
# Loop through playlists and delete those with "[Randomized]" in the name | |
for playlist in playlists["items"]: | |
if playlist["name"].startswith("[Randomized]"): | |
# Set the playlist to private before unfollowing | |
sp.playlist_change_details(playlist["id"], public=False) | |
sp.user_playlist_unfollow(sp.current_user()["id"], playlist["id"]) | |
deleted_playlists.append(playlist["name"]) | |
if deleted_playlists: | |
return f"The following playlists were deleted: {', '.join(deleted_playlists)}" | |
else: | |
return "No playlists were deleted." | |
if __name__ == "__main__": | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment