Skip to content

Commit

Permalink
Uploaded the latest scraper version & backend API powered by flask
Browse files Browse the repository at this point in the history
  • Loading branch information
dkat0 committed Jun 19, 2024
1 parent dc83d20 commit 85cc5a3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 1 deletion.
1 change: 0 additions & 1 deletion backend/api
Submodule api deleted from 2bbd2a
3 changes: 3 additions & 0 deletions backend/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
examples/
config.ini
env/
18 changes: 18 additions & 0 deletions backend/api/README.md
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.
71 changes: 71 additions & 0 deletions backend/api/app.py
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()
2 changes: 2 additions & 0 deletions backend/api/config.ini.example
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
4 changes: 4 additions & 0 deletions backend/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Flask
pymongo[srv]
Flask-PyMongo
requests
23 changes: 23 additions & 0 deletions backend/api/test_api.py
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())

0 comments on commit 85cc5a3

Please sign in to comment.