Added working WTForm to get URL at /downloader

This commit is contained in:
Maximilian Wagner
2023-07-30 13:59:11 +02:00
parent 1f070ece5d
commit 62ab798e7c
4 changed files with 36 additions and 18 deletions

5
app.py
View File

@@ -1,8 +1,10 @@
from flask import Flask, current_app, g from flask import Flask, current_app, g
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
from flask_wtf.csrf import CSRFProtect
from frontend import frontend, get_db from frontend import frontend, get_db
from nav import nav from nav import nav
from os import urandom
def init_db(): def init_db():
@@ -17,6 +19,9 @@ def create_app():
Bootstrap(app) Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True app.config['BOOTSTRAP_SERVE_LOCAL'] = True
app.config['SECRET_KEY'] = urandom(32)
CSRFProtect(app)
app.register_blueprint(frontend) app.register_blueprint(frontend)
app.app_context().push() app.app_context().push()
nav.init_app(app) nav.init_app(app)

7
forms/download.py Normal file
View File

@@ -0,0 +1,7 @@
from flask_wtf import FlaskForm
from wtforms import URLField
from wtforms.validators import URL, DataRequired
class DownloadForm(FlaskForm):
url = URLField('url', validators=[DataRequired(), URL()])

View File

@@ -3,7 +3,9 @@ from flask_nav3.elements import Navbar, View
import sqlite3 import sqlite3
import os import os
import forms.download
from nav import nav from nav import nav
from forms.download import DownloadForm
frontend = Blueprint('frontend', __name__) frontend = Blueprint('frontend', __name__)
@@ -23,16 +25,22 @@ def index():
@frontend.route('/downloader', methods=['GET', 'POST']) @frontend.route('/downloader', methods=['GET', 'POST'])
def downloader(): def downloader():
if request.method == 'GET': form = DownloadForm()
return render_template('downloader.html') if form.validate_on_submit():
else: # put here the interaction with youtube-dl
return 'Hello there!' # or forward to site that shows details of yt link
url = form.url.data
return f"Hello There {url}"
return render_template('downloader.html', form=form)
@frontend.route('/download/<path:file>', methods=['GET']) @frontend.route('/download/<path:file>', methods=['GET'])
def download(file): def download(file):
dir = os.path.join(current_app.root_path, 'downloads/') return send_from_directory(
return send_from_directory(dir, ) os.path.join(current_app.root_path, 'downloads/'),
file
)
@frontend.route('/update', methods=['GET', 'POST']) @frontend.route('/update', methods=['GET', 'POST'])
@@ -51,7 +59,7 @@ def library():
def collection(): def collection():
query = query_db(""" query = query_db("""
SELECT video.name FROM video SELECT video.name FROM video
INNER JOIN collection ON collection.path = video.path INNER JOIN collection ON collection.path = video.filename
INNER JOIN playlist ON playlist.ROWID = collection.playlist INNER JOIN playlist ON playlist.ROWID = collection.playlist
WHERE video.name IS ?; WHERE video.name IS ?;
""", ("",)) """, ("",))

View File

@@ -1,17 +1,15 @@
{%- extends "base.html" %} {%- extends "base.html" %}
{% import "bootstrap/utils.html" as utils %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
{%- with messages = get_flashed_messages(with_categories=True) %} <form method="POST">
{%- if messages %} {{ form.csrf_token }}
<div class="row"> <label for="url">Video, Channel or Playlist</label> <br>
<div class="col-md-12"> <input id="url" name="url" required type="url" placeholder="YouTube Link">
{{utils.flashed_messages(messages)}} <input type="submit" value="Go">
</div> </form>
</div> {% if form.errors %}
{%- endif %} {{ form.errors['url'][0][:-1] + ', try again.' }}
{%- endwith %} {% endif %}
</div> </div>
{%- endblock %} {%- endblock %}