-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchUserProfile.js
175 lines (167 loc) · 5.52 KB
/
fetchUserProfile.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
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
import fetch from "node-fetch";
const query = `
query getUserProfile($username: String!) {
allQuestionsCount {
difficulty
count
}
matchedUser(username: $username) {
username
contributions {
points
}
profile {
reputation
ranking
realName
aboutMe
userAvatar
location
skillTags
websites
company
school
starRating
}
submissionCalendar
submitStats {
acSubmissionNum {
difficulty
count
submissions
}
totalSubmissionNum {
difficulty
count
submissions
}
}
badges {
id
displayName
icon
creationDate
}
}
recentSubmissionList(username: $username, limit: 20) {
title
titleSlug
timestamp
statusDisplay
lang
runtime
memory
url
__typename
}
userContestRanking(username: $username) {
attendedContestsCount
rating
globalRanking
totalParticipants
topPercentage
badge {
name
icon
}
}
}
`;
const formatData = (data) => {
let sendData = {
username: data.matchedUser.username,
totalSolved: data.matchedUser.submitStats.acSubmissionNum[0].count,
totalSubmissions: data.matchedUser.submitStats.totalSubmissionNum[0].count,
totalQuestions: data.allQuestionsCount[0].count,
easySolved: data.matchedUser.submitStats.acSubmissionNum[1].count,
totalEasy: data.allQuestionsCount[1].count,
mediumSolved: data.matchedUser.submitStats.acSubmissionNum[2].count,
totalMedium: data.allQuestionsCount[2].count,
hardSolved: data.matchedUser.submitStats.acSubmissionNum[3].count,
totalHard: data.allQuestionsCount[3].count,
ranking: data.matchedUser.profile.ranking,
contributionPoints: data.matchedUser.contributions.points,
reputation: data.matchedUser.profile.reputation,
submissionCalendar: JSON.parse(data.matchedUser.submissionCalendar),
recentSubmissions: data.recentSubmissionList,
profile: {
realName: data.matchedUser.profile.realName,
aboutMe: data.matchedUser.profile.aboutMe,
userAvatar: data.matchedUser.profile.userAvatar,
location: data.matchedUser.profile.location,
skillTags: data.matchedUser.profile.skillTags,
websites: data.matchedUser.profile.websites,
company: data.matchedUser.profile.company,
school: data.matchedUser.profile.school,
starRating: data.matchedUser.profile.starRating,
},
badges: data.matchedUser.badges,
contestRanking: data.userContestRanking,
};
return sendData;
};
export const fetchUserProfile = (req, res) => {
let user = req.params.username;
fetch("https://leetcode.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Referer: "https://leetcode.com",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
body: JSON.stringify({ query: query, variables: { username: user } }),
})
.then((result) => result.json())
.then((data) => {
if (data.errors) {
res.status(400).json({ error: "User not found or API error", details: data.errors });
} else {
res.json(formatData(data.data));
}
})
.catch((err) => {
console.error("Error", err);
res.status(500).json({ error: "Internal server error", details: err.message });
});
};
export const fetchMultipleUserProfiles = async (req, res) => {
const { usernames } = req.body;
if (!usernames || !Array.isArray(usernames)) {
return res.status(400).json({ error: "Usernames must be an array" });
}
try {
const profilePromises = usernames.map((username) =>
fetch("https://leetcode.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Referer: "https://leetcode.com",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
body: JSON.stringify({
query: query,
variables: { username: username },
}),
})
.then((response) => response.json())
.then((data) => {
if (data.errors) {
return { username, error: "User not found or API error", details: data.errors };
} else {
return { username, profile: formatData(data.data) };
}
})
.catch((err) => {
console.error("Error fetching profile for", username, err);
return { username, error: "Internal server error", details: err.message };
})
);
const profiles = await Promise.all(profilePromises);
res.json(profiles);
} catch (err) {
console.error("Error fetching multiple user profiles:", err);
res.status(500).json({ error: "Internal server error", details: err.message });
}
};