-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (122 loc) · 3.08 KB
/
index.js
File metadata and controls
129 lines (122 loc) · 3.08 KB
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
const newquery = `query getUserProfile($username: String!) {
allQuestionsCount {
difficulty
count
}
matchedUser(username: $username) {
username
githubUrl
twitterUrl
linkedinUrl
contributions {
points
questionCount
testcaseCount
}
profile {
realName
userAvatar
birthday
ranking
reputation
websites
countryName
company
school
skillTags
aboutMe
starRating
}
badges {
id
displayName
icon
creationDate
}
upcomingBadges {
name
icon
}
activeBadge {
id
displayName
icon
creationDate
}
submitStats {
totalSubmissionNum {
difficulty
count
submissions
}
acSubmissionNum {
difficulty
count
submissions
}
}
submissionCalendar
}
recentSubmissionList(username: $username, limit: 20) {
title
titleSlug
timestamp
statusDisplay
lang
}
}`;
const cors = require("cors");
require("dotenv").config();
// module.exports = query;
const userDetailsFetch = async (req, res, query) => {
let userName = req.params.username;
console.log(userName);
let limit = req.query.limit;
var data = await fetch("https://leetcode.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
Referer: "https://leetcode.com",
},
body: JSON.stringify({
query: query,
variables: {
username: userName, //username require
limit: limit, //only for submission
},
}),
});
data = await data.json();
return data.data;
};
const express = require("express");
const app = express();
app.use(
cors({
origin: "*", // Allow requests from any origin
methods: ["GET", "POST", "PUT", "DELETE"], // Allowed HTTP methods
allowedHeaders: ["Content-Type", "Authorization"], // Allowed request headers
exposedHeaders: ["Content-Length", "Authorization"], // Headers exposed to the client
credentials: true, // Allow credentials (cookies, authorization headers, etc.)
optionsSuccessStatus: 200, // Set the HTTP status code for successful OPTIONS requests
})
);
app.get("/:username", async (req, res) => {
try {
const data = await userDetailsFetch(req, res, newquery);
console.log(data);
for (let key in data.matchedUser) {
console.log(key);
if (key == "submitStats") console.log(data.matchedUser.submitStats);
}
res.send(data.matchedUser.submitStats);
} catch (error) {
console.error("Error occurred:", error);
// Handle the error as needed
res.status(500).send("Internal Server Error");
}
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(port);
});