forked from katieboetig/ChompCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_server.py
More file actions
73 lines (60 loc) · 2.29 KB
/
Copy pathflask_server.py
File metadata and controls
73 lines (60 loc) · 2.29 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
import json
import os
import sys
app = Flask(__name__)
CORS(app) # Allow cross-origin requests from React Native
@app.route("/")
def home():
return "✅ Flask server is running."
@app.route("/generate", methods=["POST"])
def generate():
try:
data = request.get_json()
print("🔍 Full received data:", data)
major = data.get("major")
preferences = data.get("preferences", {})
taken_courses = data.get("takenCourses", [])
remaining_courses = data.get("remainingCourses", [])
if not major:
return jsonify({"error": "Major is required"}), 400
print(f"🧠 Major: {major}")
print(f"🎯 Preferences: {preferences}")
print(f"✅ Taken Courses: {len(taken_courses)}")
print(f"📚 Remaining Courses: {len(remaining_courses)}")
# Example: Run scrape.py with the major as argument (optional)
result = subprocess.run(
[sys.executable, "scrape.py", major],
capture_output=True,
text=True,
check=True,
encoding="utf-8" # ✅ this is the key!
)
print("✅ scrape.py completed.")
# Example: read the output JSON file from scrape.py
output_path = os.path.join("assets", "courses_by_semester.json")
with open(output_path, "r", encoding="utf-8") as f:
scraped_data = json.load(f)
# ⬇️ Replace this with actual scheduling logic
# For now, return a mocked schedule
mock_schedule = [
{
"semester": "Fall 2025",
"courses": ["CS101 - Intro to CS", "MATH201 - Calculus I"]
},
{
"semester": "Spring 2026",
"courses": ["CS102 - Data Structures", "ENGL101 - Writing I"]
}
]
return jsonify({"schedule": mock_schedule})
except subprocess.CalledProcessError as e:
print("❌ Error running scrape.py:", e.stderr)
return jsonify({"error": "Scrape failed", "details": e.stderr}), 500
except Exception as e:
print("❌ General error:", str(e))
return jsonify({"error": "Unexpected error", "details": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001, debug=True)