Skip to content

Commit

Permalink
Use local RMP scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
TyHil committed Oct 24, 2024
1 parent 92c8338 commit eaba6e1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 63 deletions.
87 changes: 43 additions & 44 deletions src/data/fetchFromRmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,49 @@ function getGraphQlUrlProp(name: string, schoolID: string) {
};
}

export interface RmpRequest {
profFirst: string;
profLast: string;
schoolId: string;
schoolName: string;
}
export function requestProfessorFromRmp({
profFirst,
profLast,
schoolId,
schoolName,
}: RmpRequest): Promise<RMPInterface> {
profFirst = profFirst.split(' ')[0];
const name = profFirst + ' ' + profLast;
export type RMPInterface = {
avgDifficulty: number;
avgRating: number;
courseCodes: {
courseCount: number;
courseName: string;
}[];
department: string;
firstName: string;
lastName: string;
legacyId: number;
numRatings: number;
ratingsDistribution: {
r1: number;
r2: number;
r3: number;
r4: number;
r5: number;
total: number;
};
school: {
id: string;
};
teacherRatingTags: {
tagCount: number;
tagName: string;
}[];
wouldTakeAgainPercent: number;
};

type Data = {
message: string;
data?: RMPInterface;
};

export default function fetchFromRmp(
profFirst: string,
profLast: string,
schoolId: string,
schoolName: string,
): Promise<Data> {
const singleProfFirst = profFirst.split(' ')[0];
const name = singleProfFirst + ' ' + profLast;
// create fetch object for professor
const graphQlUrlProp = getGraphQlUrlProp(name, schoolId);
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -74,33 +103,3 @@ export function requestProfessorFromRmp({
});
});
}

export type RMPInterface = {
avgDifficulty: number;
avgRating: number;
courseCodes: {
courseCount: number;
courseName: string;
}[];
department: string;
firstName: string;
lastName: string;
legacyId: number;
numRatings: number;
ratingsDistribution: {
r1: number;
r2: number;
r3: number;
r4: number;
r5: number;
total: number;
};
school: {
id: string;
};
teacherRatingTags: {
tagCount: number;
tagName: string;
}[];
wouldTakeAgainPercent: number;
};
31 changes: 12 additions & 19 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import Landing from '~components/Landing';
import ProfessorOverview from '~components/ProfessorOverview';
import SearchResultsTable from '~components/SearchResultsTable';
import TopMenu from '~components/TopMenu';
import { TRENDS_URL } from '~data/config';
import type { RMPInterface } from '~data/fetchFromRmp';
import { SCHOOL_ID, SCHOOL_NAME, TRENDS_URL } from '~data/config';
import fetchFromRmp, { type RMPInterface } from '~data/fetchFromRmp';
import fetchWithCache, {
cacheIndexNebula,
cacheIndexRmp,
expireTime,
} from '~data/fetchWithCache';
import type SearchQuery from '~utils/SearchQuery';
Expand Down Expand Up @@ -123,23 +122,14 @@ function fetchGradesData(course: SearchQuery): Promise<GradesType> {

//Fetch RMP data from RMP
function fetchRmpData(professor: SearchQuery): Promise<RMPInterface> {
return fetchWithCache(
TRENDS_URL +
'api/ratemyprofessorScraper?profFirst=' +
encodeURIComponent(String(professor.profFirst)) +
'&profLast=' +
encodeURIComponent(String(professor.profLast)),
{
method: 'GET',
headers: {
Accept: 'application/json',
},
},
cacheIndexRmp,
expireTime,
return fetchFromRmp(
professor.profFirst,
professor.profLast,
SCHOOL_ID,
SCHOOL_NAME,
).then((response) => {
if (response.message !== 'success') {
throw new Error(response.message);
throw new Error(response);
}
return response.data;
});
Expand Down Expand Up @@ -265,7 +255,10 @@ const Index = () => {
.catch((error) => {
//Set loading status to error
addToRmp(searchQueryLabel(professor), { state: 'error' });
console.error('RMP data for ' + searchQueryLabel(professor), error);
console.error(
'RMP data for ' + searchQueryLabel(professor),
error.message,
);
});
}

Expand Down

0 comments on commit eaba6e1

Please sign in to comment.