From 708c8beac65a613f9a586b072077a84e6551e7a7 Mon Sep 17 00:00:00 2001 From: Louis Gallet <me@louisgallet.fr> Date: Mon, 1 Jul 2024 00:19:39 +0200 Subject: [PATCH] feat: :construction: Starting working on Spotify authentification for playlist creation --- main.py | 18 +++++++++++++++--- templates/create.html | 34 ++++++++++++++++++++++++++++++++++ utils/spotifyAPI.py | 11 ++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 templates/create.html diff --git a/main.py b/main.py index a7e0173..aee6936 100644 --- a/main.py +++ b/main.py @@ -3,8 +3,8 @@ import os from flask import Flask, request, render_template from flask_bootstrap import Bootstrap5 from utils.spotifyAPI import searchSpotify - import sqlite3 +from random import randint app = Flask(__name__) @@ -24,11 +24,23 @@ def main(): if roomSearch: return render_template("index.html", response="We found your collaborative playlist", comment="It is a great news, click the button below to access it.", type="success", roomid=roomid) else: - cursor.execute("INSERT INTO rooms (roomid, spotify_id, playlist_name) VALUES (?, ?, ?)", (roomid, "test", "test")) - database.commit() 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"]) +def create_playlist(): + playlistName = request.form.get('roomName') + if not playlistName: + return render_template("create.html", response="Invalid playlist name", comment="Please provide a valid playlist name", type="error") + playlistID = "test" + # check if the room id already exists + roomid = randint(10000000, 99999999) + while cursor.execute("SELECT * FROM rooms WHERE roomid = ?", (roomid,)).fetchone(): + roomid = randint(10000000, 99999999) + cursor.execute("INSERT INTO rooms (roomid, spotify_id, playlist_name) VALUES (?, ?, ?)", (roomid, playlistID, playlistName)) + database.commit() + return render_template("create.html", response="Playlist created successfully", comment="Enjoy the night \U0001f57a", type="success", roomid=roomid) + @app.route('/search/<string:roomid>', methods=['GET', 'POST']) def main_app(roomid): if len(roomid) != 8 or not cursor.execute("SELECT * FROM rooms WHERE roomid = ?", (roomid,)).fetchone(): diff --git a/templates/create.html b/templates/create.html new file mode 100644 index 0000000..9f9d553 --- /dev/null +++ b/templates/create.html @@ -0,0 +1,34 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8"> + <title>FĂȘte de la musique</title> + <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> + {{ bootstrap.load_css() }} + </head> + <body> + <div> + <div> + <script> + if ("{{type}}" === "error") { + Swal.fire({ + title: "{{ response }}", + text: "{{ comment }}", + icon: "{{ type }}", + confirmButtonText: "Try again", + }).then(function() { + window.location = "/"; + }) + } + Swal.fire({ + title:"{{ response }}", + text: "{{ comment }}", + icon: "{{ type }}", + confirmButtonText: "Add some music!", + }).then(function() { + window.location = "/search/{{ roomid }}"; + }) + </script> + </div> + </div> + </body> \ No newline at end of file diff --git a/utils/spotifyAPI.py b/utils/spotifyAPI.py index a9c514b..3161b24 100644 --- a/utils/spotifyAPI.py +++ b/utils/spotifyAPI.py @@ -23,4 +23,13 @@ def searchSpotify(spotifySearch, limit=10): def createPlaylist(playlistName): #TODO: Implement this function - pass \ No newline at end of file + pass + +def spotifyLogin(): + """ + This function is used to login to spotify to get the user token + :return: user token + """ + credentials = spotipy.oauth2.SpotifyClientCredentials(client_id=os.getenv("client_id"), client_secret=os.getenv("client_secret")) + spotify_token = credentials.get_access_token() + return spotify_token \ No newline at end of file