feat: Song can be added without quitting app

This commit is contained in:
Louis Gallet 2024-06-29 22:59:25 +02:00
parent 1dceebdbef
commit 7d507e7c46
Signed by: lgallet
GPG Key ID: 84D3DF1528A84511
3 changed files with 22 additions and 2 deletions

19
main.py
View File

@ -1,3 +1,6 @@
import requests
import os
from dotenv import load_dotenv
from flask import Flask, request, render_template from flask import Flask, request, render_template
from flask_bootstrap import Bootstrap5 from flask_bootstrap import Bootstrap5
@ -8,7 +11,7 @@ app = Flask(__name__)
bootstrap = Bootstrap5(app) bootstrap = Bootstrap5(app)
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def form_example(): def main_app():
# handle the POST request # handle the POST request
if request.method == 'POST': if request.method == 'POST':
search = request.form.get('search') search = request.form.get('search')
@ -18,6 +21,20 @@ def form_example():
# otherwise handle the GET request # otherwise handle the GET request
return render_template("index.html") return render_template("index.html")
@app.route('/add/<string:trackid>', methods=['GET'])
def add_to_playlist(trackid):
if not os.getenv("n8n_webhook"):
return 'n8n_webhook not set'
if not trackid:
return 'No trackid provided'
try:
data = requests.get(os.getenv("n8n_webhook") + "/" + trackid)
if data.json()['message'] == 'Workflow was started':
return 'Track added to playlist'
else:
return 'Invalid response from server: {}'.format(data.text)
except requests.exceptions.RequestException as e:
return 'Request failed: {}'.format(e)
if __name__ == '__main__': if __name__ == '__main__':

0
templates/add.html Normal file
View File

View File

@ -35,7 +35,10 @@
<h5 class="card-title"> {{ track[0] }} </h5> <h5 class="card-title"> {{ track[0] }} </h5>
<p class="card-text">{{ track[1] }} - {{ track[2] }}</p> <p class="card-text">{{ track[1] }} - {{ track[2] }}</p>
<audio controls><source src="{{ track[3] }}"></audio> <audio controls><source src="{{ track[3] }}"></audio>
<a href="https://n8n.louisgallet.fr/webhook/61f08b8c-5bca-4091-a934-da66d9ae09e7/61f08b8c-5bca-4091-a934-da66d9ae09e7/{{ track[5] }}" class="btn btn-primary">Add to the playlist</a> <form method="GET" action="/add/{{track[5]}}">
<input type="hidden" name="track" value="{{ track[5] }}">
<input type="submit" value="Add to the playlist" class="btn btn-primary">
</form>
</div> </div>
</div> </div>
</div> </div>