Add real download feature
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
kleph 2021-04-13 05:24:58 +02:00
parent 563bafd697
commit 3af66de155
3 changed files with 51 additions and 3 deletions

View file

@ -2,12 +2,13 @@ FROM python:slim
# flask and app # flask and app
RUN pip install flask flask-wtf RUN pip install flask flask-wtf
COPY /pytubedl/pytubedl.py /pytubedl.py COPY /pytubedl/ /pytubedl/
COPY /pytubedl/templates /templates RUN chmod 755 /pytubedl/dl_wrapper.sh
EXPOSE 5000 EXPOSE 5000
# youtube-dl # youtube-dl
RUN pip install youtube_dl RUN pip install youtube_dl
WORKDIR /pytubedl
CMD ["python", "pytubedl.py"] CMD ["python", "pytubedl.py"]

29
pytubedl/dl_wrapper.sh Executable file
View file

@ -0,0 +1,29 @@
#!/bin/bash
# wrapper around youtube-dl
DOWNLOAD_DIR="/mnt/incoming/pytubedl/"
usage(){
echo "$0 <url>"
exit 1
}
# check args
if [ -z "$1" ]; then
usage
fi
url="$1"
# check process
pgrep youtube-dl
ret=$?
if [ ${ret} -eq 0 ]; then
echo "Another youtube-dl process is already running."
exit 2
fi
pushd ${PWD}
cd "${DOWNLOAD_DIR}"
youtube-dl -t "$url"
popd

View file

@ -2,11 +2,14 @@ from flask import Flask, render_template, request, flash
from wtforms import Form, StringField from wtforms import Form, StringField
from wtforms.validators import DataRequired, URL from wtforms.validators import DataRequired, URL
import secrets import secrets
import subprocess
import time
DEBUG = False DEBUG = False
app = Flask(__name__) app = Flask(__name__)
app.config['SECRET_KEY'] = secrets.token_urlsafe(20) app.config['SECRET_KEY'] = secrets.token_urlsafe(20)
class MyForm(Form): class MyForm(Form):
url = StringField('url', validators=[DataRequired(), URL(message='Must be a valid URL')]) url = StringField('url', validators=[DataRequired(), URL(message='Must be a valid URL')])
@ -21,7 +24,22 @@ class MyForm(Form):
print("URL: " + url) print("URL: " + url)
if form.validate(): if form.validate():
flash('OK. Download in progress') flash('OK. Starting download')
process = subprocess.Popen(['./dl_wrapper.sh', url],
stdout=subprocess.PIPE,
universal_newlines=True)
# sleep 2 seconds to catch errors
time.sleep(2)
return_code = process.poll()
if return_code and return_code != 0:
# if process return early, there's good chance of an error
error_msg = f"An error occurred. return code: {return_code}.\nlogs:\n{process.stdout}"
flash(error_msg)
else:
# process is running, probably no error (yet ;-) )
flash('OK. Download in progress. File will appear in output dir')
else: else:
flash('Error: bad URL') flash('Error: bad URL')