-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
50 lines (40 loc) · 1.48 KB
/
app.py
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
46
47
48
49
50
from flask import Flask, request, jsonify, send_file, render_template
from web_to_md import get_webpage, clean_html, html_to_markdown, enhance_markdown_with_gpt, save_markdown
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/convert', methods=['POST'])
def convert():
try:
data = request.json
url = data.get('url')
if not url:
return jsonify({'error': 'URL is required'}), 400
# Process the webpage
html_content = get_webpage(url)
clean_content = clean_html(html_content)
markdown_content = html_to_markdown(clean_content)
enhanced_markdown = enhance_markdown_with_gpt(markdown_content, url)
# Save the file and get the filename
output_file = save_markdown(enhanced_markdown, url)
# Return the file path for download
return jsonify({
'success': True,
'file': output_file,
'message': 'Conversion successful'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/download/<path:filename>')
def download_file(filename):
try:
return send_file(filename, as_attachment=True)
except Exception as e:
return jsonify({'error': str(e)}), 404
if __name__ == '__main__':
os.makedirs('output', exist_ok=True)
app.run(host='0.0.0.0', port=8000, debug=True)