-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uploaded the latest scraper version & backend API powered by flask
- Loading branch information
Showing
7 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
Submodule api
deleted from
2bbd2a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
examples/ | ||
config.ini | ||
env/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
To use, first create a virtual environment: `python3 -m venv <myenvname>` | ||
|
||
Then activate it: | ||
|
||
On windows: `.\env\Scripts\activate.bat` | ||
On mac: `source venv/bin/activate` | ||
|
||
Then install needed packages: `pip3 install -r requirements.txt` | ||
|
||
Then create a file `config.ini` that contains the link to the MONGO database in the format seen in `config.ini.example`. | ||
|
||
Then launch the backend API: `python3 app.py` | ||
|
||
To test, you can utilize this command: `python3 test_api.py` | ||
|
||
-------- | ||
|
||
Updates for the future: expand filters to include dates/categories, for better interaction with the frontend. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
import json | ||
from flask import Flask, jsonify, request, url_for, redirect | ||
from flask_pymongo import PyMongo | ||
import os | ||
import configparser | ||
|
||
config = configparser.ConfigParser() | ||
config.read(os.path.abspath(os.path.join("config.ini"))) | ||
|
||
app = Flask(__name__) | ||
app.config["MONGO_URI"] = config['PROD']['DB_URI'] | ||
mongo = PyMongo(app) | ||
|
||
# Define the mapping between category and MongoDB collections | ||
category_collections = { | ||
'academic': ['drop-in-tutoring', 'office-hours', 'peer-tutoring', 'supplemental-instruction'], | ||
'career': ['handshake'], | ||
'club': ['tartanconnect'] | ||
} | ||
|
||
""" | ||
# Create text indexes for the collections | ||
def create_text_indexes(): | ||
for category, collections in category_collections.items(): | ||
for collection in collections: | ||
if category == 'academic': | ||
mongo.db[collection].create_index([('course_name', 'text')]) | ||
else: | ||
mongo.db[collection].create_index([('event_name', 'text')]) | ||
create_text_indexes() | ||
""" | ||
|
||
@app.route('/events', methods=['POST']) | ||
def search_events(): | ||
data = request.json | ||
|
||
query = {} | ||
if 'category' in data and data['category'] in category_collections: | ||
collections = category_collections[data['category']] | ||
else: | ||
collections = sum(category_collections.values(), []) # All collections | ||
|
||
if 'name' in data: | ||
query['$text'] = { | ||
'$search': data['name'], | ||
'$caseSensitive': False, | ||
'$diacriticSensitive': False | ||
} | ||
|
||
results = [] | ||
for collection in collections: | ||
if 'startDate' in data or 'endDate' in data: | ||
date_query = {} | ||
if 'startDate' in data: | ||
date_query['events.start_time'] = {'$gte': data['startDate']} | ||
if 'endDate' in data: | ||
date_query['events.end_time'] = {'$lte': data['endDate']} | ||
query.update(date_query) | ||
|
||
print(f"Checking {collection}, query:") | ||
print(query) | ||
found_events = mongo.db[collection].find(query) | ||
for event in found_events: | ||
event['_id'] = str(event['_id']) | ||
results.append(event) | ||
|
||
return jsonify(results) | ||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[PROD] | ||
DB_URI = mongodb+srv://USER:[email protected]_blah_blah.mongodb.net/CMUCal?retryWrites=true&w=majority |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Flask | ||
pymongo[srv] | ||
Flask-PyMongo | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import requests | ||
|
||
# URL of the Flask application's `/events` endpoint | ||
url = 'http://localhost:5000/events' | ||
|
||
# Example payload for the POST request | ||
payload = { | ||
'name': 'Business', | ||
'category': 'academic', | ||
#'startDate': '10:00AM', | ||
#'endDate': '02:00PM' | ||
} | ||
|
||
print("Calling API with payload:") | ||
print(payload) | ||
|
||
# Make the POST request | ||
response = requests.post(url, json=payload) | ||
|
||
# Print the status code and response data | ||
print(f'Status Code: {response.status_code}') | ||
print('Response:') | ||
print(response.json()) |