-
Notifications
You must be signed in to change notification settings - Fork 5
Pushing course data to DB & Added logging #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
1203ac2
5a1e7b8
ea63cd8
0ebdfc0
416a6ec
73a7cf3
a11992c
cb8520e
02bb2e9
b29287e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ | |
| """ | ||
|
|
||
| import sys | ||
| import requests | ||
|
|
||
| from sqlalchemy import create_engine | ||
|
|
||
| from datetime import date, datetime | ||
|
|
||
| from labconnect import create_app, db | ||
|
|
@@ -28,12 +32,48 @@ | |
| UserCourses, | ||
| UserDepartments, | ||
| UserMajors, | ||
| UserSavedOpportunities, | ||
| Codes, | ||
| Codes | ||
| ) | ||
|
|
||
|
|
||
| def fetch_json_data(json_url): | ||
| response = requests.get(json_url) | ||
|
|
||
| if response.status_code != 200: | ||
| raise ValueError(f"Error: Received status code {response.status_code}") | ||
| try: | ||
| return response.json() | ||
| except requests.exceptions.JSONDecodeError: | ||
| raise ValueError("Error: Received invalid JSON response") | ||
|
|
||
|
|
||
| def insert_courses_from_json(session, courses_data): | ||
| # Fetch existing courses to avoid multiple queries | ||
| existing_courses = {course.code: course for course in session.query(Courses).all()} | ||
| new_courses = [] | ||
|
|
||
| for course, course_info in courses_data.items(): | ||
| course_name = course_info.get("name") | ||
| course_code = course_info.get('subj') + course_info.get('crse') | ||
|
|
||
| if len(course_code) != 8: | ||
| continue | ||
| if course_code in existing_courses: | ||
| # Update name if changed | ||
| existing_course = existing_courses[course_code] | ||
| if existing_course.name != course_name: | ||
| existing_course.name = course_name | ||
| else: | ||
| new_courses.append(Courses(code=course_code, name=course_name)) | ||
|
|
||
| if new_courses: | ||
| session.add_all(new_courses) | ||
| session.commit() | ||
|
||
|
|
||
|
|
||
| app = create_app() | ||
|
|
||
|
|
||
| if len(sys.argv) < 2: | ||
| sys.exit("No argument or exsisting argument found") | ||
|
|
||
|
|
@@ -42,14 +82,28 @@ | |
| if db.inspect(db.engine).get_table_names(): | ||
| print("Tables already exist.") | ||
| # clear the codes table | ||
| db.session.query(Codes).delete() | ||
| db.session.delete(Codes) | ||
| db.session.commit() | ||
| sys.exit() | ||
| db.create_all() | ||
|
|
||
| elif sys.argv[1] == "clear": | ||
| with app.app_context(): | ||
| db.drop_all() | ||
|
|
||
| elif sys.argv[1] == "addCourses": | ||
| with app.app_context(): | ||
| db.create_all() | ||
|
|
||
| json_url = "https://raw.githubusercontent.com/quacs/quacs-data/master/semester_data/202409/catalog.json" | ||
|
||
|
|
||
| courses_data = fetch_json_data(json_url) | ||
| if not courses_data: | ||
| sys.exit("Failed to fetch courses data. Exiting...") | ||
|
|
||
| insert_courses_from_json(db.session, courses_data) | ||
|
|
||
| db.session.close() | ||
|
|
||
| elif sys.argv[1] == "create": | ||
| with app.app_context(): | ||
|
|
@@ -443,10 +497,10 @@ | |
| UserCourses, | ||
| UserDepartments, | ||
| UserMajors, | ||
| UserSavedOpportunities, | ||
| ] | ||
|
|
||
| for table in tables: | ||
|
|
||
| stmt = db.select(table) | ||
| result = db.session.execute(stmt).scalars() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Echoing the ruff comment, this should be removed since it's not needed/used