Add real download feature
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
563bafd697
commit
3af66de155
3 changed files with 51 additions and 3 deletions
|
@ -2,12 +2,13 @@ FROM python:slim
|
|||
|
||||
# flask and app
|
||||
RUN pip install flask flask-wtf
|
||||
COPY /pytubedl/pytubedl.py /pytubedl.py
|
||||
COPY /pytubedl/templates /templates
|
||||
COPY /pytubedl/ /pytubedl/
|
||||
RUN chmod 755 /pytubedl/dl_wrapper.sh
|
||||
EXPOSE 5000
|
||||
|
||||
# youtube-dl
|
||||
RUN pip install youtube_dl
|
||||
|
||||
WORKDIR /pytubedl
|
||||
CMD ["python", "pytubedl.py"]
|
||||
|
||||
|
|
29
pytubedl/dl_wrapper.sh
Executable file
29
pytubedl/dl_wrapper.sh
Executable 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
|
|
@ -2,11 +2,14 @@ from flask import Flask, render_template, request, flash
|
|||
from wtforms import Form, StringField
|
||||
from wtforms.validators import DataRequired, URL
|
||||
import secrets
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
DEBUG = False
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = secrets.token_urlsafe(20)
|
||||
|
||||
|
||||
class MyForm(Form):
|
||||
url = StringField('url', validators=[DataRequired(), URL(message='Must be a valid URL')])
|
||||
|
||||
|
@ -21,7 +24,22 @@ class MyForm(Form):
|
|||
print("URL: " + url)
|
||||
|
||||
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:
|
||||
flash('Error: bad URL')
|
||||
|
||||
|
|
Loading…
Reference in a new issue