-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
224 lines (194 loc) · 7.78 KB
/
app.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from util import get_data, get_page_by_code, get_page_by_title
from flask import Flask, request
from flask_restful import Api, Resource
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
from flask_marshmallow import Marshmallow
from datetime import date
from os import environ, path
from dotenv import load_dotenv
basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, '.env'))
app = Flask(__name__)
api = Api(app)
app.config['SQLALCHEMY_DATABASE_URI'] = environ.get("DATABASE_URI")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
# * Model
class watchListModel(db.Model):
'''
database of watched courses, connected to user
'''
__tablename__ = "watchlist"
_id = db.Column("entryid", db.Integer, primary_key=True)
uid = db.Column("uid", db.String(255))
name = db.Column('name', db.String(100), nullable=False)
identifier = db.Column('identifier', db.String(100), nullable=False)
course_code = db.Column('course_code', db.String(255), nullable=False)
course_sec = db.Column('course_sec', db.String(100))
added_on = db.Column('added_on', db.Date())
def __init__(self, name, identifier, course_code, course_sec=None):
self.uid = name + identifier
self.name = name
self.identifier = identifier
self.course_code = course_code
self.course_sec = course_sec
self.added_on = date.today()
class watchListSchema(ma.Schema):
'''
schema for what data endpoint needs access to
'''
class Meta:
fields = ("name", "identifier", "course_code",
"course_sec", "added_on")
watchList_Schema = watchListSchema()
watchList_Schemas = watchListSchema(many=True)
# * Routes
class mainApi(Resource):
def get(self):
'''
to search for course by title or code
accepts query param either: "code" or "title"
'''
code = request.args.get('code')
title = request.args.get('title')
if code is None and title is None:
return {"error": "invalid params"}, 400
elif code is None:
if len(title) <= 2:
return {"error": "param too short"}, 400
else:
soup = get_page_by_title(title)
jsonData = get_data(soup)
return jsonData, 200
else:
if len(code) <= 1:
return {"error": "param too short"}, 400
else:
soup = get_page_by_code(code)
jsonData = get_data(soup)
return jsonData, 200
class adminApi(Resource):
def get(self):
'''
to get all courses (over 2000)
Note, run time is limited by soup processing each entry rather than
selenium scrapping (page source gives all entries at once)
'''
get_type = request.args.get('type')
if get_type == "all":
soup = get_page_by_title("")
jsonData = get_data(soup)
return jsonData, 200
class myClassesApi(Resource):
def get(self):
'''
get user's watched courses' details
needs query params: "name" and "identifier"
'''
watched_classes = {
#class code 1: [class(es) data dict],
#class code 2: [class(es) data dict]
}
# * Get List of Class Codes of name+id from database
try:
name = request.args.get("name")
identifier = request.args.get("identifier")
this_uid = name + identifier
except:
return {"error": "Missing param"}, 400
raw_data = watchListModel.query.filter(func.lower(watchListModel.uid)==func.lower(this_uid)).all()
res = watchList_Schemas.dump(raw_data)
# * Search each class
for entry in res:
target_sec = entry['course_sec']
target_code = entry['course_code']
soup = get_page_by_code(target_code)
jsonData = get_data(soup)
if target_sec is None:
watched_classes[target_code] = jsonData['matches']
else:
# * filter section, if specified (can't search by section)
for entry in jsonData['matches']:
if entry['sec_code'] == target_sec:
watched_classes[target_code + ' - ' + target_sec] = [entry]
break
return watched_classes, 200
class myProfileApi(Resource):
def get(self):
'''
gets the user's watched courses
needs query param: "name" and "identifier"
'''
try:
name = request.args.get("name")
identifier = request.args.get("identifier")
this_uid = name + identifier
except:
return {"error": "Missing param"}, 400
raw_data = watchListModel.query.filter(func.lower(watchListModel.uid)==func.lower(this_uid)).all()
res = watchList_Schemas.dump(raw_data)
return res
def put(self):
'''
adds a course to watch
needs body of: "name", "identifier", and "course_code"; optionally "course_sec"
'''
json_data = request.get_json()
data = watchList_Schema.load(json_data)
try:
opt_course_sec = str(data["course_sec"])
if len(opt_course_sec) != 2:
return {"error": "course section must be two digits (eg. 02)"}
except:
opt_course_sec = None
try:
int(data['course_code'][-3:])
except:
return {"error": "course code must have three digits (eg. math032 instead of math32"}
matches = watchListModel.query.filter_by(name=data["name"], identifier=data["identifier"],
course_code=data['course_code'], course_sec=opt_course_sec).first()
if matches is not None:
return {"error": "course already added"}, 400
# Because run time or scraping for each class is slow, limit number of classes can watch
user_entries = watchListModel.query.filter_by(name=data["name"], identifier=data["identifier"]).all()
if len(user_entries) >= 10:
return {"error": "too many courses watched, create new identifier to watch another course"}, 400
try:
entry = watchListModel(name=data["name"], identifier=data["identifier"],
course_code=data['course_code'], course_sec=opt_course_sec)
db.session.add(entry)
db.session.commit()
res = watchList_Schema.dump(entry)
return {"added": res}, 201
except:
return {"error": "error in params given"}, 400
def delete(self):
'''
deletes user's watched course
needs param: "name", "identifier", and "course_code"
'''
json_data = request.get_json()
# Not deserialize as not adding entry to database, just need the dict data
try:
this_name = json_data["name"]
this_identifier = json_data["identifier"]
this_course_code = json_data["course_code"]
except:
return {"error": "Missing param"}, 400
try:
match = watchListModel.query.filter_by(
name=this_name, identifier=this_identifier, course_code=this_course_code).one()
db.session.delete(match)
db.session.commit()
return {"deleted": {"this_name": this_name, "this_identifier": this_identifier, "this_course_code": this_course_code}}
except:
return {"error": "Invalid params"}, 400
# api.add_resource(adminApi, "/aaron")
api.add_resource(mainApi, "/")
api.add_resource(myClassesApi, "/mycourses")
api.add_resource(myProfileApi, "/myclasslist")
if __name__ == "__main__":
db.create_all()
app.run(debug=True)