axebps commited on
Commit
2f2b872
·
verified ·
1 Parent(s): 044d88f

Create app.py

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