feat/room-id #2

Merged
lgallet merged 7 commits from feat/room-id into main 2024-07-01 15:14:05 +00:00
7 changed files with 137 additions and 32 deletions
Showing only changes of commit 788b81402b - Show all commits

View File

@ -1,3 +1,4 @@
client_id=
client_secret=
n8n_webhook=
n8n_webhook=
enviroment=dev

12
.idea/dataSources.xml generated Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="database.db" uuid="bc21304a-7893-4f0b-bc20-7e9bb19b7991">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:database.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

7
.idea/sqldialects.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/main.py" dialect="GenericSQL" />
<file url="PROJECT" dialect="SQLite" />
</component>
</project>

32
main.py
View File

@ -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/<string:roomid>', 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/<string:roomid>/<string:trackid>', 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")
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")

View File

@ -1,28 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fête de la musique</title>
{{ bootstrap.load_css() }}
</head>
<body>
<div class="container">
<form method="POST">
<div class="form-group">
<div class="form-inline text-center">
<label>Enter here the music that you want:</label>
<label>
<input type="text" name="search" class="form-control" />
</label>
<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 class="container">
<h1>Make this playlist</h1>
<p>You too, choose the music that will play tonight.</p>
<div class="row">
<div class="col-sm">
<h2>Join a room</h2>
<form method="POST" >
<div class="form-group">
<label for="roomID">Enter here the room id:</label>
<input type="text" class="form-control" name="roomID" placeholder="00000000" maxlength="8" minlength="8" />
<small id="roomIDHelp" class="form-text text-muted">If you don't have a room id, you can create one.</small>
</div>
<button type="submit" class="btn btn-primary">Join</button>
</form>
</div>
<div class="col-sm">
<h2>Create a room</h2>
<form method="POST" action="/create">
<div class="form-group">
<label for="roomName">Enter here the room name:</label>
<input type="text" class="form-control" name="roomName" placeholder="Room name" />
<small id="roomNameHelp" class="form-text text-muted">This will be the playlist name</small>
</div>
<button type="submit" class="btn btn-success">Create (require a Spotify account)</button>
</form>
</div>
</div>
</div>
<div class="text-center">
<input type="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
</body>
</div>
<div class="sweetalert">
<script>
if ("{{ type }}" !== "") {
if ("{{ type }}" === "success") {
Swal.fire({
title: "{{ response }}",
text: "{{ comment }}",
icon: "{{ type }}",
confirmButtonText: "Join another room",
}).then(function() {
window.location = "/search/{{ roomid }}";
})
} else {
Swal.fire({
title: "{{ response }}",
text: "{{ comment }}",
icon: "{{ type }}",
confirmButtonText: "Try again",
}).then(function() {
window.location = "/";
})
}
}
</script>
</div>
</body>
</html>

26
templates/search.html Normal file
View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fête de la musique</title>
{{ bootstrap.load_css() }}
</head>
<body>
<div class="container">
<form method="POST">
<div class="form-group">
<div class="form-inline text-center">
<label>Enter here the music that you want:</label>
<label>
<input type="text" name="search" class="form-control" />
</label>
</div>
</div>
<div class="text-center">
<input type="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
</body>
</html>

View File

@ -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