Skip to content

Commit

Permalink
fixes #6: search using multiple school IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
mahfoozm committed Jul 9, 2023
1 parent 5e556eb commit ab6af88
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
87 changes: 48 additions & 39 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,67 @@

// publicly available token, yorkU school ID
const AUTH_TOKEN = 'dGVzdDp0ZXN0';
const SCHOOL_ID = 'U2Nob29sLTE0OTU=';

// currently saerching for profs at:
// YorkU (Keele and Glendon), TMU, and UofT (SG)
const SCHOOL_IDS = [
"U2Nob29sLTE0OTU=",
"U2Nob29sLTEyMTI1",
"U2Nob29sLTE0NzE=",
"U2Nob29sLTE0ODQ=",
];

// for searchProfessor and getProfessor, use a self hosted proxy to bypass CORS restrictions
const searchProfessor = async (name, schoolID) => {
const response = await fetch(
// self hosted proxy
`http://140.238.154.147:8088/https://www.ratemyprofessors.com/graphql`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${AUTH_TOKEN}`,
},
body: JSON.stringify({
query: `query NewSearchTeachersQuery($text: String!, $schoolID: ID!) {
newSearch {
teachers(query: {text: $text, schoolID: $schoolID}) {
edges {
cursor
node {
id
firstName
lastName
school {
name
const searchProfessor = async (name, schoolIDs) => {
for (const schoolID of schoolIDs) {
const response = await fetch(
// self hosted proxy
`https://www.ratemyprofessors.com/graphql`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${AUTH_TOKEN}`,
},
body: JSON.stringify({
query: `query NewSearchTeachersQuery($text: String!, $schoolID: ID!) {
newSearch {
teachers(query: {text: $text, schoolID: $schoolID}) {
edges {
cursor
node {
id
firstName
lastName
school {
name
id
}
}
}
}
}
}
}`,
variables: {
text: name,
schoolID,
},
}),
}`,
variables: {
text: name,
schoolID,
},
}),
}
);
const json = await response.json();
if (json.data.newSearch.teachers.edges.length > 0) {
return json.data.newSearch.teachers.edges.map((edge) => edge.node);
}
);
const json = await response.json();
if (json.data.newSearch.teachers === null) {
return [];
}

return json.data.newSearch.teachers.edges.map((edge) => edge.node);
return [];
};


const getProfessor = async (id) => {
const response = await fetch(
// self hosted proxy
`http://140.238.154.147:8088/https://www.ratemyprofessors.com/graphql`,
`https://www.ratemyprofessors.com/graphql`,
{
method: "POST",
headers: {
Expand Down Expand Up @@ -98,14 +107,14 @@ async function sendProfessorInfo(professorName) {
// normalize the prof's name before sending to RMP API
// (the source of all of my pain)
const normalizedName = professorName.normalize("NFKD");
const professors = await searchProfessor(normalizedName, SCHOOL_ID);
const professors = await searchProfessor(normalizedName, SCHOOL_IDS);

if (professors.length === 0) {
// try searching without the middle name/initial
const names = normalizedName.split(" ");
if (names.length >= 2) {
const modifiedName = `${names[0]} ${names[names.length - 1]}`;
const modifiedProfessors = await searchProfessor(modifiedName, SCHOOL_ID);
const modifiedProfessors = await searchProfessor(modifiedName, SCHOOL_IDS);

if (modifiedProfessors.length === 0) {
return { error: "Professor not found" };
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "YorkRMP",
"description": "RMP extension for YorkU's course page.",
"version": "1.2.4",
"version": "1.2.6",
"icons": { "128": "assets/icon128.png" },
"permissions": ["activeTab", "webRequest", "https://w2prod.sis.yorku.ca/*", "https://schedulebuilder.yorku.ca/*", "https://www.ratemyprofessors.com/*"],
"optional_permissions": ["webNavigation", "tabs"],
Expand Down

0 comments on commit ab6af88

Please sign in to comment.