From 8b8579e988a14f4d32022ef1fc4dd14f176af213 Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Mon, 1 Jul 2024 01:01:35 +0200 Subject: [PATCH] feat: :construction: Continue working on spotify integration --- main.py | 17 ++++++++---- templates/add.html | 26 ------------------ templates/create.html | 35 ----------------------- templates/found.html | 53 ----------------------------------- templates/index.html | 64 ------------------------------------------- templates/search.html | 29 -------------------- utils/spotifyAPI.py | 5 +++- 7 files changed, 16 insertions(+), 213 deletions(-) delete mode 100644 templates/add.html delete mode 100644 templates/create.html delete mode 100644 templates/found.html delete mode 100644 templates/index.html delete mode 100644 templates/search.html diff --git a/main.py b/main.py index 76b196e..b65e6d6 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,9 @@ import requests import os -from flask import Flask, request, render_template, redirect, session, url_for +from flask import Flask, request, render_template, redirect, session, url_for, sessions +from flask_session import Session from flask_bootstrap import Bootstrap5 -from utils.spotifyAPI import searchSpotify, create_spotify_oauth +from utils.spotifyAPI import searchSpotify, create_spotify_oauth, get_spotify_username from utils.generateQRCode import generateQRCode import sqlite3 from random import randint @@ -27,7 +28,7 @@ def main(): return render_template("index.html", response="We could not find your collaborative playlist", comment="Please check the room id and try again or create one.", type="error", roomid=roomid) return render_template("index.html") -@app.route('/create', methods=["POST"]) +@app.route('/create', methods=["GET", "POST"]) def create_playlist(): playlistName = request.form.get('roomName') if not playlistName: @@ -78,20 +79,26 @@ def add_to_playlist(roomid, trackid): def authentification(): sp_auth = create_spotify_oauth() auth_url = sp_auth.get_authorize_url() + session["token_info"] = sp_auth.get_cached_token() return redirect(auth_url) @app.route('/callback') def callback(): sp_oauth = create_spotify_oauth() - code = requests.args.get('code') + code = request.args.get('code') token_info = sp_oauth.get_access_token(code) session["TOKEN_INFO"] = token_info - return redirect(url_for('create')) + return redirect(url_for('info')) + +@app.route("/info") +def info(): + return get_spotify_username() if __name__ == '__main__': if not os.getenv("client_id") or not os.getenv("client_secret") or not os.getenv("n8n_webhook"): print("Please provide client_id, client_secret and n8n_webhook in the .env file") exit(1) + app.secret_key = 'super secret key' if os.getenv("enviroment") != "production": app.run(debug=True, port="3000", host="0.0.0.0") else: diff --git a/templates/add.html b/templates/add.html deleted file mode 100644 index 90abaf0..0000000 --- a/templates/add.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - - Fête de la musique - - {{ bootstrap.load_css() }} - - -
-
- -
-
- - \ No newline at end of file diff --git a/templates/create.html b/templates/create.html deleted file mode 100644 index ab6e6a2..0000000 --- a/templates/create.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - - Fête de la musique - - {{ bootstrap.load_css() }} - - -
-
- -
-
- \ No newline at end of file diff --git a/templates/found.html b/templates/found.html deleted file mode 100644 index 4abb2f2..0000000 --- a/templates/found.html +++ /dev/null @@ -1,53 +0,0 @@ - - - - - Results - {{ bootstrap.load_css() }} - - - -
-

Welcome

-

Choose the music that will play tonight.

-

Info: You're in room: {{ roomid }} , the playlist is called: {{ playlistName }}

-
-
-
- - -
-
- -
- -
-
- -
-
- {% for track in tracks %} -
-
- Card image cap -
-
{{ track[0] }}
-

{{ track[1] }} - {{ track[2] }}

- -
- - -
-
-
-
- {% endfor %} -
- -
- - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index 8f5ecf6..0000000 --- a/templates/index.html +++ /dev/null @@ -1,64 +0,0 @@ - - - - - Fête de la musique - - {{ bootstrap.load_css() }} - - -
-

Make this playlist

-

You too, choose the music that will play tonight.

-
-
-

Join a room

-
-
- - - If you don't have a room id, you can create one. -
- -
-
-
-

Create a room

-
-
- - - This will be the playlist name -
- -
-
-
-
-
- -
- - \ No newline at end of file diff --git a/templates/search.html b/templates/search.html deleted file mode 100644 index e702982..0000000 --- a/templates/search.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - - Fête de la musique - {{ bootstrap.load_css() }} - - -
-

Welcome

-

Choose the music that will play tonight.

-

Info: You're in room: {{ roomid }} , the playlist is called: {{ playlistName }}

-
-
-
- - -
-
- -
- -
-
-
- - \ No newline at end of file diff --git a/utils/spotifyAPI.py b/utils/spotifyAPI.py index eba4f7c..c43a3b0 100644 --- a/utils/spotifyAPI.py +++ b/utils/spotifyAPI.py @@ -36,4 +36,7 @@ def create_spotify_oauth(): client_secret=os.getenv("client_secret"), redirect_uri=url_for('callback', _external=True), scope='playlist-modify-public' - ) \ No newline at end of file + ) + +def get_spotify_username(): + return spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=os.getenv("client_id"), client_secret=os.getenv("client_secret"), redirect_uri=url_for('callback', _external=True))).me() \ No newline at end of file