Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue w/ getting ratings #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const SCHOOL_IDS = [
"U2Nob29sLTEzNDgz",
"U2Nob29sLTEzNjQ3",
"U2Nob29sLTE3MTA4",
"U2Nob29sLTE1NzIz",
];
const PROFESSOR_ID = `
query ($query: TeacherSearchQuery!) {
Expand Down Expand Up @@ -38,6 +39,51 @@ query ($id: ID!) {
}
`;

const MAX_RETRIES = 3;
const RETRY_DELAY_MS = 1000;

const fetchProfIDFallbackLoop = async (profName, retryCount = 0) => {
try {
const raw_response = await fetch(
"https://www.ratemyprofessors.com/graphql",
{
method: "POST",
headers: {
Authorization: AUTHORIZATION_TOKEN,
},
body: JSON.stringify({
query: PROFESSOR_ID,
variables: {
query: { text: profName, schoolID: SCHOOL_ID },
},
}),
}
);

const response = await raw_response.json();
if (response.data && response.data.newSearch && response.data.newSearch.teachers && response.data.newSearch.teachers.edges && response.data.newSearch.teachers.edges.length > 0) {
return response;
} else if (retryCount < MAX_RETRIES) {
console.log(`No matching professor found for "${profName}". Retrying (${retryCount + 1}/${MAX_RETRIES})...`);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS));
return fetchProfIDFallbackLoop(profName, retryCount + 1);
} else {
console.error(`Maximum number of retries (${MAX_RETRIES}) exceeded for "${profName}".`);
return null;
}
} catch (error) {
console.error(`Error fetching professor ID for "${profName}":`, error);
if (retryCount < MAX_RETRIES) {
console.log(`Retrying (${retryCount + 1}/${MAX_RETRIES})...`);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS));
return fetchProfIDFallbackLoop(profName, retryCount + 1);
} else {
console.error(`Maximum number of retries (${MAX_RETRIES}) exceeded for "${profName}".`);
return null;
}
}
};

const fetchProfIDFallbackLoop = (profName) => {
return new Promise(async (resolve, reject) => {
let response = null;
Expand Down