-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (118 loc) · 4.77 KB
/
server.js
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
const express = require('express');
const cors = require('cors');
const fetch = require('node-fetch');
const stringSimilarity = require('string-similarity'); // Fuzzy matching library
const fuzz = require('fuzzball');
const areNamesSimilar = require('./stringSimilarity.js');
const app = express();
app.use(cors());
app.get('/getRating', async (req, res) => {
try {
const professorName = req.query.name;
// Define the headers for the GraphQL request
const headers = {
'Authorization': 'Basic dGVzdDp0ZXN0', // Replace with your actual credentials
'Content-Type': 'application/json',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Connection': 'keep-alive',
'Cookie': 'ccpa-notice-viewed-02=true; cid=I3_j_yoLB9-20240912; userSchoolId=U2Nob29sLTEwNzI=; userSchoolLegacyId=1072; userSchoolName=University%20of%20California%20Berkeley',
'Origin': 'https://www.ratemyprofessors.com',
'Referer': 'https://www.ratemyprofessors.com/search/professors/1072?q=' + professorName,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
};
// Define the GraphQL query
const query = {
query: `
query TeacherSearchResultsPageQuery($query: TeacherSearchQuery!) {
search: newSearch {
teachers(query: $query, first: 8, after: "") {
edges {
node {
firstName
lastName
avgRating
id
numRatings
legacyId
wouldTakeAgainPercent
avgDifficulty
school {
name
id
}
}
}
}
}
}
`,
variables: {
query: {
text: professorName,
schoolID: "U2Nob29sLTEwNzI=", // UC Berkeley
fallback: true
},
schoolID: "U2Nob29sLTEwNzI=", // UC Berkeley
includeSchoolFilter: true
}
};
// Make the POST request to RateMyProfessors GraphQL API
console.log('fetching rating for', professorName)
const response = await fetch('https://www.ratemyprofessors.com/graphql', {
method: 'POST',
headers: headers,
body: JSON.stringify(query)
});
if (!response.ok) {
const errorMessage = `Failed to fetch data: ${response.status} ${response.statusText}`;
console.error(errorMessage);
return res.status(500).json({ error: errorMessage });
}
const data = await response.json();
//console.log(data.data.search.teachers.edges)
if (data && data.data && data.data.search && data.data.search.teachers && data.data.search.teachers.edges.length > 0) {
const listings = data.data.search.teachers.edges;
if (listings.length === 0) {
console.log(`No results found for professor: ${professorName}`);
return res.status(404).json({ message: 'No professor found.' });
}
const filteredListings = listings.filter(professor => {
const fullName = `${professor.node.firstName} ${professor.node.lastName}`;
const similarity = areNamesSimilar(fullName, professorName);
return similarity
//return isSimilar(fullName, professorName); // Use the fuzzy match function
});
if (filteredListings.length > 0) {
let professorMatches = [];
filteredListings.forEach(professor => {
professorMatches.push({
name: `${professor.node.firstName} ${professor.node.lastName}`,
id: `${professor.node.id}`,
urlId: `${professor.node.legacyId}`,
rating: professor.node.avgRating,
numRatings: professor.node.numRatings,
wouldTakeAgainPercent: professor.node.wouldTakeAgainPercent,
avgDifficulty: professor.node.avgDifficulty,
school: professor.node.school.name
});
});
console.log('done searching ratings for ', professorMatches )
res.json(professorMatches);
} else {
//console.log(`No matching professor found for: ${professorName}`);
return res.json([])
}
} else {
console.log(`No professor found for: ${professorName}`);
return res.json([])
}
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'An error occurred while fetching the rating' });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});