From 6e5e14137254332e1f798eaaf686531e2f58fa5e Mon Sep 17 00:00:00 2001 From: Louis Gallet Date: Mon, 1 Jul 2024 17:13:37 +0200 Subject: [PATCH] feat: :sparkles: RoomID is fully implemented --- main.py | 70 ++++++++++++++++++++----------------------- templates/add.html | 26 ++++++++++++++++ templates/create.html | 35 ++++++++++++++++++++++ templates/found.html | 56 ++++++++++++++++++++++++++++++++++ templates/index.html | 64 +++++++++++++++++++++++++++++++++++++++ templates/search.html | 32 ++++++++++++++++++++ utils/spotifyAPI.py | 22 +------------- 7 files changed, 246 insertions(+), 59 deletions(-) create mode 100644 templates/add.html create mode 100644 templates/create.html create mode 100644 templates/found.html create mode 100644 templates/index.html create mode 100644 templates/search.html diff --git a/main.py b/main.py index b65e6d6..171ed5c 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,8 @@ import requests import os -from flask import Flask, request, render_template, redirect, session, url_for, sessions -from flask_session import Session +from flask import Flask, request, render_template, redirect, url_for from flask_bootstrap import Bootstrap5 -from utils.spotifyAPI import searchSpotify, create_spotify_oauth, get_spotify_username +from utils.spotifyAPI import searchSpotify from utils.generateQRCode import generateQRCode import sqlite3 from random import randint @@ -13,7 +12,7 @@ bootstrap = Bootstrap5(app) database = sqlite3.connect("database.db", check_same_thread=False) cursor = database.cursor() -cursor.execute("CREATE TABLE IF NOT EXISTS rooms (roomid TEXT PRIMARY KEY, spotify_id TEXT, playlist_name TEXT)") +cursor.execute("CREATE TABLE IF NOT EXISTS rooms (roomid TEXT PRIMARY KEY, spotify_id TEXT, playlist_name TEXT, spotify_URL TEXT)") @app.route("/", methods=['GET', 'POST']) def main(): @@ -33,40 +32,54 @@ def create_playlist(): playlistName = request.form.get('roomName') if not playlistName: return redirect(url_for('main')) - 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() - qrcode = generateQRCode("http://localhost:3000/search/" + str(roomid)) - print(qrcode) - return render_template("create.html", response="Playlist created successfully", comment="Enjoy the night \U0001f57a (and share this QRCode with your friends)", image=qrcode, type="success", roomid=roomid) + + try: + data = requests.get((os.getenv("n8n_webhook_create_playlist") + "/" + str(roomid) + "/" + playlistName)) + playlistID = data.json()['playlistID'] + spotifyURL = data.json()['spotifyURL'] + print(playlistID) + cursor.execute("INSERT INTO rooms (roomid, spotify_id, playlist_name, spotify_URL) VALUES (?, ?, ?, ?)", + (roomid, playlistID, playlistName, spotifyURL)) + database.commit() + qrcode = generateQRCode("http://localhost:3000/search/" + str(roomid)) + return render_template("create.html", response="Playlist created successfully", + comment="Enjoy the night \U0001f57a (and share this QRCode with your friends)", + image=qrcode, type="success", roomid=roomid) + + except requests.exceptions.RequestException as e: + return render_template("create.html", response='Request failed', comment=e, type="error") @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(): return main() playlistName = cursor.execute("SELECT playlist_name FROM rooms WHERE roomid = ?", (roomid,)).fetchone()[0] + playlistID = cursor.execute("SELECT spotify_id FROM rooms WHERE roomid = ?", (roomid,)).fetchone()[0] + spotifyURL = cursor.execute("SELECT spotify_URL FROM rooms WHERE roomid = ?", (roomid,)).fetchone()[0] # handle the POST request if request.method == 'POST': search = request.form.get('search') result = searchSpotify(search) - return render_template("found.html", query=search, tracks=result, roomid=roomid, playlistName=playlistName) + return render_template("found.html", query=search, tracks=result, roomid=roomid, playlistName=playlistName, playlistID=playlistID, spotifyURL=spotifyURL) # otherwise handle the GET request - return render_template("search.html", roomid=roomid, playlistName=playlistName) + return render_template("search.html", roomid=roomid, playlistName=playlistName, spotifyURL=spotifyURL) -@app.route('/add//', methods=['GET']) -def add_to_playlist(roomid, trackid): +@app.route('/add///', methods=['GET']) +def add_to_playlist(roomid, playlistID, trackid): print(roomid) + print(playlistID) print(trackid) - if not os.getenv("n8n_webhook"): + if not os.getenv("n8n_webhook_add_tracks"): return render_template("add.html", response="No n8n webhook provided", comment="Please provide a n8n webhook in the .env file", type="error", roomid=roomid) try: - data = requests.get(os.getenv("n8n_webhook") + "/" + roomid + "/" + trackid) - if data.json()['message'] == 'Workflow was started': + data = requests.get(os.getenv("n8n_webhook_add_tracks") + "/" + playlistID + "/" + trackid) + print(data.json) + if data.json()['status'] == 'success': return render_template("add.html", response="Track added to playlist successfully", comment="Enjoy the night \U0001f57a", type="success", roomid=roomid) else: @@ -75,27 +88,8 @@ def add_to_playlist(roomid, trackid): return render_template("add.html", response='Request failed', comment=e, type="error", roomid=roomid) -@app.route('/authenticate') -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 = request.args.get('code') - token_info = sp_oauth.get_access_token(code) - session["TOKEN_INFO"] = token_info - 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"): + if not os.getenv("client_id") or not os.getenv("client_secret") or not os.getenv("n8n_webhook_add_tracks") or not os.getenv("n8n_webhook_create_playlist"): print("Please provide client_id, client_secret and n8n_webhook in the .env file") exit(1) app.secret_key = 'super secret key' diff --git a/templates/add.html b/templates/add.html new file mode 100644 index 0000000..90abaf0 --- /dev/null +++ b/templates/add.html @@ -0,0 +1,26 @@ + + + + + Fête de la musique + + {{ bootstrap.load_css() }} + + +
+
+ +
+
+ + \ No newline at end of file diff --git a/templates/create.html b/templates/create.html new file mode 100644 index 0000000..ab6e6a2 --- /dev/null +++ b/templates/create.html @@ -0,0 +1,35 @@ + + + + + Fête de la musique + + {{ bootstrap.load_css() }} + + +
+
+ +
+
+ \ No newline at end of file diff --git a/templates/found.html b/templates/found.html new file mode 100644 index 0000000..5bd224f --- /dev/null +++ b/templates/found.html @@ -0,0 +1,56 @@ + + + + + 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 new file mode 100644 index 0000000..8f5ecf6 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,64 @@ + + + + + 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 new file mode 100644 index 0000000..c9f8a33 --- /dev/null +++ b/templates/search.html @@ -0,0 +1,32 @@ + + + + + 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 c43a3b0..f78c79c 100644 --- a/utils/spotifyAPI.py +++ b/utils/spotifyAPI.py @@ -19,24 +19,4 @@ def searchSpotify(spotifySearch, limit=10): trackImage = results['tracks']['items'][i]['album']['images'][0]['url'] trackID = results['tracks']['items'][i]['uri'] tracks.append([trackName, trackArtist, trackAlbum, trackPreview, trackImage, trackID]) - return tracks - - -def createPlaylist(playlistName): - #TODO: Implement this function - pass - -def create_spotify_oauth(): - """ - This function creates a Spotify OAuth object - :return: SpotifyOAuth object - """ - return SpotifyOAuth( - client_id=os.getenv("client_id"), - client_secret=os.getenv("client_secret"), - redirect_uri=url_for('callback', _external=True), - scope='playlist-modify-public' - ) - -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 + return tracks \ No newline at end of file