Spaces:
Sleeping
Sleeping
File size: 1,328 Bytes
2f2b872 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
from flask import Flask, request, Response, send_from_directory
import subprocess
app = Flask(__name__, static_folder='static')
@app.route('/')
def serve_index():
return send_from_directory(app.static_folder, 'index.html')
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory(app.static_folder, path)
@app.route('/download', methods=['POST'])
def download():
url = request.form.get('url')
if not url:
return Response('Error: URL not provided', status=400)
try:
p = subprocess.Popen([
'node', '/usr/src/app/node_modules/single-file-cli/single-file-node.js',
'--browser-executable-path', '/usr/bin/chromium-browser',
url, '--dump-content'
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
return Response(f"Error downloading the page: {stderr.decode('utf-8')}", status=500)
return Response(
stdout,
mimetype="text/html",
headers={
"Content-Disposition": "attachment; filename=downloaded_page.html"
}
)
except Exception as e:
return Response(f"Error: {str(e)}", status=500)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)
|