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/', 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 @@ + + + + + FĂȘte de la musique + + {{ bootstrap.load_css() }} + + +
+
+ +
+
+ \ 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