diff --git a/.env.example b/.env.example index cb8416c..ecf80a4 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ client_id= client_secret= -n8n_webhook= \ No newline at end of file +n8n_webhook= +enviroment=dev \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..4dd9ac1 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:database.db + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..9011cb5 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/main.py b/main.py index 22381bd..2a67d82 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,8 @@ import requests import os -from dotenv import load_dotenv from flask import Flask, request, render_template from flask_bootstrap import Bootstrap5 -from spotifySearch import searchSpotify +from utils.spotifyAPI import searchSpotify import sqlite3 @@ -11,11 +10,24 @@ import sqlite3 app = Flask(__name__) bootstrap = Bootstrap5(app) -database = sqlite3.connect("database.db") +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)") -@app.route("/", methods=['GET']) +@app.route("/", methods=['GET', 'POST']) def main(): - return "Hello, World!" + if request.method == 'POST': + roomid = request.form.get('roomID') + if len(roomid) != 8: + return render_template("index.html", response="Invalid room id", comment="A room ID should be composed of number and have a lenght of 8 characters", type="error", roomid=roomid) + roomSearch = cursor.execute("SELECT * FROM rooms WHERE roomid = ?", (roomid,)).fetchone() + if roomSearch: + return render_template("index.html", response="We found your collaborative playlist", comment="It's great news, click the button below to access it.", type="success", roomid=roomid) + else: + cursor.execute("INSERT INTO rooms (roomid, spotify_id) VALUES (?, ?)", (roomid, "test")) + database.commit() + return render_template("index.html", response="We couldn't 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('/search/', methods=['GET', 'POST']) def main_app(roomid): @@ -26,7 +38,7 @@ def main_app(roomid): return render_template("found.html", query=search, tracks=result, roomid=roomid) # otherwise handle the GET request - return render_template("index.html") + return render_template("search.html") @app.route('/add//', methods=['GET']) def add_to_playlist(roomid, trackid): @@ -46,4 +58,10 @@ def add_to_playlist(roomid, trackid): if __name__ == '__main__': - app.run(debug=True, port="3000", host="0.0.0.0") \ No newline at end of file + 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) + if os.getenv("enviroment") != "production": + app.run(debug=True, port="3000", host="0.0.0.0") + else: + app.run(debug=False, port="3000", host="0.0.0.0") \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 602fde9..2d4920e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,28 +1,64 @@ - - - Fête de la musique - {{ bootstrap.load_css() }} - - -
-
-
-
- - + + + 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..2777ac7 --- /dev/null +++ b/templates/search.html @@ -0,0 +1,26 @@ + + + + + Fête de la musique + {{ bootstrap.load_css() }} + + +
+
+
+
+ + +
+
+ +
+ +
+
+
+ + \ No newline at end of file diff --git a/spotifySearch.py b/utils/spotifyAPI.py similarity index 92% rename from spotifySearch.py rename to utils/spotifyAPI.py index 7b3bb69..a9c514b 100644 --- a/spotifySearch.py +++ b/utils/spotifyAPI.py @@ -19,3 +19,8 @@ def searchSpotify(spotifySearch, limit=10): trackID = results['tracks']['items'][i]['uri'] tracks.append([trackName, trackArtist, trackAlbum, trackPreview, trackImage, trackID]) return tracks + + +def createPlaylist(playlistName): + #TODO: Implement this function + pass \ No newline at end of file