yt2podcast/rest_server.py

88 lines
2.6 KiB
Python

import os
import flask
from flask import render_template, send_from_directory, redirect
from flask import request
from flask import url_for
from flask_cors import CORS
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import check_password_hash
# import main
# from werkzeug.utils import redirect
import config_parser
# from yt2podcast.ytdownloader import call_youtubedl
from yt2podcast.ytdl_handler import call_youtubedl
from yt2podcast.feed_generator import generate_feed_for_yt_file
app = flask.Flask(__name__)
auth = HTTPBasicAuth()
app.config["DEBUG"] = True
CORS(app)
URL_BASE_PATH = "/"
# URL_BASE_PATH = "/tagebuch"
@auth.verify_password
def verify_password(username, password):
if username == config_parser.credentials['username'] and \
check_password_hash(config_parser.credentials['hashedPassword'], password):
return username
@app.route(URL_BASE_PATH, methods=['POST', 'GET'])
@auth.login_required
def handle_yt_url():
if request.method == "POST":
if request.form.get('generate-feed') == 'generate-feed':
generate_feed_for_yt_file()
return render_template("yt.html", isValidUrl=True)
# if request.form.get('open-podcast') == 'open-podcast':
# return redirect(url_for('serve_static'), path='podcast.xml')
# return url_for('static', filename='podcast.xml')
# from flask import Response
# xml = 'foo'
# resp = app.make_response(xml)
# resp.mimetype = "text/xml"
# return resp
# r = Response(response='./feed/podcast.xml', status=200, mimetype="application/xml")
# r.headers["Content-Type"] = "text/xml; charset=utf-8"
# return r
# return app.send_static_file('./feed/podcast.xml')
user_input = request.form['ytlink']
# return redirect(url_for("return_result", ytlink=user_input))
print(user_input)
# TODO verify its a url
import validators
is_valid_url=validators.url(user_input)
if is_valid_url:
call_youtubedl(user_input)
return render_template("yt.html", isValidUrl=is_valid_url)
else:
return render_template("yt.html", isValidUrl=True)
# @app.route(URL_BASE_PATH + 'podcast', methods=['GET'])
# def serve_podcast_xml():
# return app.send_static_file('podcast.xml')
@app.route('/static/<path:path>')
def serve_static(path):
return send_from_directory('static', path)
if __name__ == '__main__':
flask_options = dict(
host='0.0.0.0',
debug=True,
port=4001,
threaded=True,
)
app.run(**flask_options)