Spaces:
Sleeping
Sleeping
| from flask import Flask, request, Response, send_from_directory | |
| import subprocess | |
| app = Flask(__name__, static_folder='static') | |
| def serve_index(): | |
| return send_from_directory(app.static_folder, 'index.html') | |
| def serve_static(path): | |
| return send_from_directory(app.static_folder, path) | |
| 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) | |