76 lines
2.1 KiB
Plaintext
76 lines
2.1 KiB
Plaintext
from flask import Flask, request, redirect
|
|
import spotipy
|
|
from spotipy.oauth2 import SpotifyOAuth
|
|
import subprocess
|
|
import os
|
|
import time
|
|
from datetime import datetime
|
|
|
|
app = Flask(__name__)
|
|
|
|
client_id = "c0020cc0e05245efb2ddb61b7045e4f2"
|
|
client_secret = "2e2e7c98f849403fbacc219c1f01c17d"
|
|
redirect_uri = "https://localhost:8888/callback"
|
|
|
|
sp_oauth = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope="playlist-modify-private user-library-read")
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return "Welcome! <a href='/login'>Login to Spotify</a>"
|
|
|
|
@app.route('/login')
|
|
def login():
|
|
return redirect(sp_oauth.get_authorize_url())
|
|
|
|
@app.route('/callback')
|
|
def callback():
|
|
code = request.args.get('code')
|
|
token_info = sp_oauth.get_access_token(code)
|
|
access_token = token_info['access_token']
|
|
sp = spotipy.Spotify(auth=access_token)
|
|
|
|
print("Fetching liked songs...")
|
|
liked_songs = []
|
|
results = sp.current_user_saved_tracks()
|
|
|
|
while results:
|
|
for item in results['items']:
|
|
liked_songs.append(item['track']['id'])
|
|
if results['next']:
|
|
results = sp.next(results)
|
|
time.sleep(1)
|
|
else:
|
|
break
|
|
|
|
playlist_name = datetime.now().strftime("Liked Songs Collection - %Y-%m-%d %H-%M-%S")
|
|
user_id = sp.me()['id']
|
|
new_playlist = sp.user_playlist_create(user_id, playlist_name, public=False)
|
|
|
|
print(f"Created playlist: {playlist_name}")
|
|
|
|
for i in range(0, len(liked_songs), 100):
|
|
batch = liked_songs[i:i + 100]
|
|
sp.playlist_add_items(new_playlist['id'], batch)
|
|
print(f"Added {len(batch)} songs...")
|
|
time.sleep(1)
|
|
|
|
playlist_url = new_playlist['external_urls']['spotify']
|
|
print(f"Playlist URL: {playlist_url}")
|
|
|
|
# Write playlist URL for the download script
|
|
with open("last_playlist_url.txt", "w") as f:
|
|
f.write(playlist_url)
|
|
|
|
return f"{playlist_url}"
|
|
|
|
if __name__ == "__main__":
|
|
print("Starting Flask server...")
|
|
os.execvp('flask', [
|
|
'flask',
|
|
'run',
|
|
'--host=0.0.0.0',
|
|
'--port=8888',
|
|
'--cert=ssl/server.cert',
|
|
'--key=ssl/server.key'
|
|
])
|