-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathflask_server.py
More file actions
56 lines (46 loc) · 1.89 KB
/
flask_server.py
File metadata and controls
56 lines (46 loc) · 1.89 KB
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
51
52
53
54
55
56
from flask import Flask, request, jsonify
from flask_cors import CORS
from anthropic_api import analyze_with_anthropic
import json
site_structure = {}
app = Flask(__name__)
CORS(app)
def load_crawled_data(filename='links.json'):
global site_structure
try:
with open(filename, 'r') as f:
site_structure = json.load(f)
print(f"Successfully loaded {len(site_structure)} URLs from {filename}")
except FileNotFoundError:
print(f"ERROR: {filename} not found. Make sure app.py has run and created it.")
site_structure = {}
except json.JSONDecodeError:
print(f"ERROR: Could not decode JSON from {filename}. It might be corrupted.")
site_structure = {}
@app.route('/api/analyze', methods=['POST'])
def analyze():
data = request.json
if not data or 'url' not in data:
return jsonify({"error": "Missing 'url' in request body"}), 400
requested_url = data['url'].rstrip("/")
page_data = site_structure.get(requested_url) or site_structure.get(requested_url + "/")
if not page_data:
print(f"Debug: URL '{requested_url}' not found in site_structure.")
print(f"Debug: Available keys: {list(site_structure.keys())[:5]}")
return jsonify({"error": "No data found for this URL"}), 404
try:
analysis = analyze_with_anthropic(page_data)
return jsonify({"analysis": analysis})
except Exception as e:
print(f"Error during analysis for {requested_url}: {e}")
return jsonify({"error": f"An error occurred during analysis: {str(e)}"}), 500
@app.route('/api/urls')
def list_urls():
return jsonify(list(site_structure.keys()))
def attach_data(structure):
global site_structure
print("attach_data called. Note: Server primarily loads data from links.json on startup.")
site_structure = structure
if __name__ == "__main__":
load_crawled_data()
app.run(debug=True)