diff --git a/Dockerfile b/Dockerfile index 69e4d2e..532d224 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/pytubedl/dl_wrapper.sh b/pytubedl/dl_wrapper.sh new file mode 100755 index 0000000..3bd129f --- /dev/null +++ b/pytubedl/dl_wrapper.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# wrapper around youtube-dl + +DOWNLOAD_DIR="/mnt/incoming/pytubedl/" + +usage(){ + echo "$0 " + 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 diff --git a/pytubedl/pytubedl.py b/pytubedl/pytubedl.py index 20c39a2..3887d8e 100644 --- a/pytubedl/pytubedl.py +++ b/pytubedl/pytubedl.py @@ -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')