|
| 1 | +import axios from "axios"; |
| 2 | + |
| 3 | +export default async function handler(req: any, res: any) { |
| 4 | + if (req.method !== "GET") { |
| 5 | + return res.status(405).json({ error: "Method Not Allowed" }); |
| 6 | + } |
| 7 | + const { username } = req.query; |
| 8 | + |
| 9 | + const url = "https://leetcode.com/graphql/"; |
| 10 | + const headers = { |
| 11 | + Accept: "*/*", |
| 12 | + "Accept-Encoding": "gzip, deflate, br, zstd", |
| 13 | + "Accept-Language": "en-US,en;q=0.7", |
| 14 | + Authorization: "YourAuthorizationTokenHere", // Replace with your actual Authorization token |
| 15 | + "Content-Type": "application/json", |
| 16 | + Cookie: "568e64618f15ed106010276ef0ec7b16", // Replace with your actual Cookie value |
| 17 | + Origin: "https://leetcode.com", |
| 18 | + Referer: `https://leetcode.com/u/${username}/`, // Dynamically set referer based on username |
| 19 | + "User-Agent": |
| 20 | + "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36", |
| 21 | + "X-Csrftoken": |
| 22 | + "irClyjQ7Qqcp8n65ZppYePQv6wo4mvzcuValmSaXlMeum9ZvYi0QYKmoVDieQKb7", |
| 23 | + }; |
| 24 | + const payload1 = { |
| 25 | + operationName: "userSessionProgress", |
| 26 | + query: ` |
| 27 | + query userSessionProgress($username: String!) { |
| 28 | + allQuestionsCount { |
| 29 | + difficulty |
| 30 | + count |
| 31 | + } |
| 32 | + matchedUser(username: $username) { |
| 33 | + submitStats { |
| 34 | + acSubmissionNum { |
| 35 | + difficulty |
| 36 | + count |
| 37 | + submissions |
| 38 | + } |
| 39 | + totalSubmissionNum { |
| 40 | + difficulty |
| 41 | + count |
| 42 | + submissions |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + `, |
| 48 | + variables: { |
| 49 | + username: username, |
| 50 | + }, |
| 51 | + }; |
| 52 | + const payload2 = { |
| 53 | + operationName: "userPublicProfile", |
| 54 | + query: ` |
| 55 | + query userPublicProfile($username: String!) { |
| 56 | + matchedUser(username: $username) { |
| 57 | + contestBadge { |
| 58 | + name |
| 59 | + expired |
| 60 | + hoverText |
| 61 | + icon |
| 62 | + } |
| 63 | + username |
| 64 | + githubUrl |
| 65 | + twitterUrl |
| 66 | + linkedinUrl |
| 67 | + profile { |
| 68 | + ranking |
| 69 | + userAvatar |
| 70 | + realName |
| 71 | + aboutMe |
| 72 | + school |
| 73 | + websites |
| 74 | + countryName |
| 75 | + company |
| 76 | + jobTitle |
| 77 | + skillTags |
| 78 | + postViewCount |
| 79 | + postViewCountDiff |
| 80 | + reputation |
| 81 | + reputationDiff |
| 82 | + solutionCount |
| 83 | + solutionCountDiff |
| 84 | + categoryDiscussCount |
| 85 | + categoryDiscussCountDiff |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + `, |
| 90 | + variables: { |
| 91 | + username: username, |
| 92 | + }, |
| 93 | + }; |
| 94 | + |
| 95 | + try { |
| 96 | + const response1 = await axios.post(url, payload1, { headers }); |
| 97 | + const response2 = await axios.post(url, payload2, { headers }); |
| 98 | + |
| 99 | + const data1 = response1.data; |
| 100 | + const data2 = response2.data; |
| 101 | + |
| 102 | + // Merge the data from both responses |
| 103 | + const combinedData = { |
| 104 | + userSessionProgress: data1.data, |
| 105 | + userPublicProfile: data2.data, |
| 106 | + }; |
| 107 | + |
| 108 | + const total = combinedData.userSessionProgress.allQuestionsCount[0].count; |
| 109 | + const easyTotal = |
| 110 | + combinedData.userSessionProgress.allQuestionsCount[1].count; |
| 111 | + const mediumTotal = |
| 112 | + combinedData.userSessionProgress.allQuestionsCount[2].count; |
| 113 | + const hardTotal = |
| 114 | + combinedData.userSessionProgress.allQuestionsCount[3].count; |
| 115 | + |
| 116 | + const totalSolved = |
| 117 | + combinedData.userSessionProgress.matchedUser.submitStats |
| 118 | + .acSubmissionNum[0].count; |
| 119 | + const easySolved = |
| 120 | + combinedData.userSessionProgress.matchedUser.submitStats |
| 121 | + .acSubmissionNum[1].count; |
| 122 | + const mediumSolved = |
| 123 | + combinedData.userSessionProgress.matchedUser.submitStats |
| 124 | + .acSubmissionNum[2].count; |
| 125 | + const hardSolved = |
| 126 | + combinedData.userSessionProgress.matchedUser.submitStats |
| 127 | + .acSubmissionNum[3].count; |
| 128 | + |
| 129 | + const profileData = { |
| 130 | + username: combinedData.userPublicProfile.matchedUser.username, |
| 131 | + rank: combinedData.userPublicProfile.matchedUser.profile.ranking, |
| 132 | + image: combinedData.userPublicProfile.matchedUser.profile.userAvatar, |
| 133 | + fullName: combinedData.userPublicProfile.matchedUser.profile.realName, |
| 134 | + }; |
| 135 | + const aboutData = { |
| 136 | + github: { |
| 137 | + link: combinedData.userPublicProfile.matchedUser.githubUrl, |
| 138 | + text: "Github", |
| 139 | + }, |
| 140 | + website: { |
| 141 | + link: combinedData.userPublicProfile.matchedUser.profile.websites[0], |
| 142 | + text: "Website", |
| 143 | + }, |
| 144 | + linkedin: { |
| 145 | + link: combinedData.userPublicProfile.matchedUser.linkedinUrl, |
| 146 | + text: "LinkedIn", |
| 147 | + }, |
| 148 | + twitter: { |
| 149 | + link: combinedData.userPublicProfile.matchedUser.twitterUrl, |
| 150 | + text: "Twitter", |
| 151 | + }, |
| 152 | + }; |
| 153 | + |
| 154 | + res.status(200).json({ |
| 155 | + profileData, |
| 156 | + aboutData, |
| 157 | + total, |
| 158 | + easyTotal, |
| 159 | + mediumTotal, |
| 160 | + hardTotal, |
| 161 | + totalSolved, |
| 162 | + easySolved, |
| 163 | + mediumSolved, |
| 164 | + hardSolved, |
| 165 | + }); |
| 166 | + } catch (error) { |
| 167 | + console.error(error); |
| 168 | + res.status(500).json({ error: "Internal Server Error" }); |
| 169 | + } |
| 170 | +} |
0 commit comments