126 lines
3.6 KiB
Python
126 lines
3.6 KiB
Python
__version__ = "pre-dev"
|
|
__all__ = ["quizify"]
|
|
__author__ = "SimolZimol"
|
|
|
|
from flask import Flask, redirect, request, session, url_for, render_template
|
|
import os
|
|
import spotipy
|
|
from spotipy.oauth2 import SpotifyOAuth
|
|
import random
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = os.getenv("SECRET_KEY")
|
|
|
|
# Erweiterte Berechtigungen für Web Playback SDK
|
|
SCOPE = "user-library-read playlist-read-private streaming user-read-email user-read-private"
|
|
|
|
def get_spotify_client():
|
|
return spotipy.Spotify(auth_manager=SpotifyOAuth(
|
|
client_id=os.getenv("SPOTIPY_CLIENT_ID"),
|
|
client_secret=os.getenv("SPOTIPY_CLIENT_SECRET"),
|
|
redirect_uri=os.getenv("SPOTIPY_REDIRECT_URI"),
|
|
scope=SCOPE,
|
|
cache_path=".cache"
|
|
))
|
|
|
|
@app.route("/")
|
|
def home():
|
|
return render_template("login.html")
|
|
|
|
@app.route("/login")
|
|
def login():
|
|
sp_oauth = SpotifyOAuth(
|
|
client_id=os.getenv("SPOTIPY_CLIENT_ID"),
|
|
client_secret=os.getenv("SPOTIPY_CLIENT_SECRET"),
|
|
redirect_uri=os.getenv("SPOTIPY_REDIRECT_URI"),
|
|
scope=SCOPE
|
|
)
|
|
auth_url = sp_oauth.get_authorize_url()
|
|
return redirect(auth_url)
|
|
|
|
@app.route("/callback")
|
|
def callback():
|
|
sp_oauth = SpotifyOAuth(
|
|
client_id=os.getenv("SPOTIPY_CLIENT_ID"),
|
|
client_secret=os.getenv("SPOTIPY_CLIENT_SECRET"),
|
|
redirect_uri=os.getenv("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")
|
|
|
|
@app.route("/playlists")
|
|
def playlists():
|
|
sp = get_spotify_client()
|
|
playlists = sp.current_user_playlists()["items"]
|
|
return render_template("playlists.html", playlists=playlists)
|
|
|
|
@app.route("/quiz/<playlist_id>")
|
|
def quiz(playlist_id):
|
|
sp = get_spotify_client()
|
|
items = sp.playlist_items(playlist_id, additional_types=["track"])["items"]
|
|
|
|
tracks = [item["track"] for item in items]
|
|
if not tracks:
|
|
return "Keine Tracks verfügbar!"
|
|
|
|
track = random.choice(tracks)
|
|
|
|
# Token aus der Sitzung holen für das Web Playback SDK
|
|
token_info = session.get('token_info', None)
|
|
if not token_info:
|
|
return redirect('/login')
|
|
|
|
# Zugriff auf access_token
|
|
access_token = token_info['access_token']
|
|
|
|
return render_template("quiz.html", track=track, access_token=access_token, playlist_id=playlist_id)
|
|
|
|
@app.route("/play_track", methods=["POST"])
|
|
def play_track():
|
|
device_id = request.args.get('device_id')
|
|
track_uri = request.args.get('track_uri')
|
|
|
|
if not device_id or not track_uri:
|
|
return {"error": "Missing device_id or track_uri"}, 400
|
|
|
|
sp = get_spotify_client()
|
|
sp.start_playback(device_id=device_id, uris=[track_uri])
|
|
|
|
return {"success": True}
|
|
|
|
@app.route("/toggle_playback", methods=["POST"])
|
|
def toggle_playback():
|
|
data = request.json
|
|
device_id = data.get('device_id')
|
|
|
|
if not device_id:
|
|
return {"error": "Missing device_id"}, 400
|
|
|
|
sp = get_spotify_client()
|
|
# Aktuellen Playback-Status abrufen
|
|
current_playback = sp.current_playback()
|
|
|
|
if current_playback and current_playback.get('is_playing'):
|
|
sp.pause_playback(device_id=device_id)
|
|
else:
|
|
sp.start_playback(device_id=device_id)
|
|
|
|
return {"success": True}
|
|
|
|
@app.route('/logout')
|
|
def logout():
|
|
session.pop('user', None)
|
|
return redirect(url_for('index'))
|
|
|
|
@app.route('/')
|
|
def index():
|
|
user = session.get('user') # Benutzerinfos aus der Session holen, falls vorhanden
|
|
return render_template('index.html', user=user)
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=5000, debug=True)
|