-
Notifications
You must be signed in to change notification settings - Fork 1
/
rmp.py
199 lines (170 loc) · 5.65 KB
/
rmp.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
import logging
import re
from json import JSONDecodeError
from logging import Logger
import requests
from bs4 import BeautifulSoup
from json_serializable import JsonSerializable
rmp_url = "https://www.ratemyprofessors.com/"
rmp_graphql_url = "https://www.ratemyprofessors.com/graphql"
graph_ql_query = """
query NewSearchTeachersQuery($query: TeacherSearchQuery!) {
newSearch {
teachers(query: $query, first: 50) {
didFallback
edges {
cursor
node {
id
legacyId
firstName
lastName
school {
legacyId
name
id
}
avgRatingRounded
avgDifficultyRounded
numRatings
wouldTakeAgainPercentRounded
mandatoryAttendance {
yes
no
neither
total
}
ratingsDistribution {
r1
r2
r3
r4
r5
total
}
ratings(first: 10) {
edges {
node {
comment
qualityRating
difficultyRatingRounded
}
}
}
}
}
}
}
}
"""
class RMPData(JsonSerializable):
def __init__(self, id, legacy_id, average_rating, average_difficulty, num_ratings, would_take_again_percent, mandatory_attendance, ratings_distribution, ratings):
self.id = id
self.legacy_id = legacy_id
self.average_rating = average_rating
self.average_difficulty = average_difficulty
self.num_ratings = num_ratings
self.would_take_again_percent = would_take_again_percent
self.mandatory_attendance = mandatory_attendance
self.ratings_distribution = ratings_distribution
self.ratings = ratings
@classmethod
def from_json(cls, json_data) -> "RMPData":
return RMPData(
id=json_data["id"],
legacy_id=json_data["legacy_id"],
average_rating=json_data["average_rating"],
average_difficulty=json_data["average_difficulty"],
num_ratings=json_data["num_ratings"],
would_take_again_percent=json_data["would_take_again_percent"],
mandatory_attendance=json_data["mandatory_attendance"],
ratings_distribution=json_data["ratings_distribution"],
ratings=json_data["ratings"]
)
@classmethod
def from_rmp_data(cls, rmp_data) -> "RMPData":
id = rmp_data["id"]
legacy_id = rmp_data["legacyId"]
average_rating = rmp_data["avgRatingRounded"]
average_difficulty = rmp_data["avgDifficultyRounded"]
num_ratings = rmp_data["numRatings"]
would_take_again_percent = rmp_data["wouldTakeAgainPercentRounded"]
mandatory_attendance = rmp_data["mandatoryAttendance"]
ratings_distribution = rmp_data["ratingsDistribution"]
ratings = []
for rating in rmp_data["ratings"]["edges"]:
node = rating["node"]
ratings.append({
"comment": node["comment"],
"quality_rating": node["qualityRating"],
"difficulty_rating": node["difficultyRatingRounded"]
})
return RMPData(
id=id,
legacy_id=legacy_id,
average_rating=average_rating,
average_difficulty=average_difficulty,
num_ratings=num_ratings,
would_take_again_percent=would_take_again_percent,
mandatory_attendance=mandatory_attendance,
ratings_distribution=ratings_distribution,
ratings=ratings
)
def to_dict(self):
return {
"id": self.id,
"legacy_id": self.legacy_id,
"average_rating": self.average_rating,
"average_difficulty": self.average_difficulty,
"num_ratings": self.num_ratings,
"would_take_again_percent": self.would_take_again_percent,
"mandatory_attendance": self.mandatory_attendance,
"ratings_distribution": self.ratings_distribution,
"ratings": self.ratings
}
def produce_query(instructor_name):
return {
"query": {
"text": instructor_name,
"schoolID": "U2Nob29sLTE4NDE4", # UW-Madison
}
}
def get_rating(name, api_key, logger: Logger):
auth_header = { "Authorization": f"Basic {api_key}" }
response = requests.post(
url=rmp_graphql_url,
headers=auth_header,
json={"query": graph_ql_query, "variables": produce_query(name)}
)
try:
data = response.json()
except JSONDecodeError:
logger.error(f"Failed to decode JSON response: {response.text}")
return None
results = data["data"]["newSearch"]["teachers"]["edges"]
for result in results:
result = result["node"]
if result["firstName"].lower() in name.lower() and result["lastName"].lower() in name.lower():
return RMPData.from_rmp_data(result)
logger.debug(f"Failed to find rating for {name}")
return None
def scrape_api_key():
response = requests.get(rmp_url)
match = re.search(r'"REACT_APP_GRAPHQL_AUTH"\s*:\s*"([^"]+)"', response.text)
graphql_auth = match.group(1)
return graphql_auth
def get_ratings(instructors, stats, logger: Logger):
api_key = scrape_api_key()
ratings = {}
total = len(instructors)
with_ratings = 0
for i, instructor in enumerate(instructors):
logger.info(f"Fetching rating for {instructor} ({i * 100 / total:.2f}%).")
rating = get_rating(instructor, api_key, logger)
if rating:
with_ratings += 1
ratings[instructor] = rating
logger.info(f"Found ratings for {with_ratings} out of {total} instructors ({with_ratings * 100 / total:.2f}%).")
stats["instructors"] = total
stats["instructors_with_ratings"] = with_ratings
return ratings