Skip to content

Commit 5d56306

Browse files
committed
api part 1
1 parent 25c66e2 commit 5d56306

File tree

604 files changed

+98852
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

604 files changed

+98852
-0
lines changed

app.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const express = require('express')
2+
const app = express();
3+
const cors = require('cors')
4+
5+
app.use(cors({
6+
origin: '*'
7+
}))
8+
9+
let leet = require('./leetcode');
10+
app.get('/', (req, res) => {
11+
res.send(`<b>API URL:</b> https://localhost:3000/<b style="color:crimson;">yourLeetcodeUsername</b>`)
12+
});
13+
app.get('/:id', leet.leetcode);
14+
15+
app.listen(3000, () => {
16+
console.log(`App is running on port 3000`);
17+
});

leetcode.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//graphql query
2+
3+
4+
const fetch = require("node-fetch");
5+
6+
7+
const query = `
8+
query getUserProfile($username: String!) {
9+
allQuestionsCount {
10+
difficulty
11+
count
12+
}
13+
matchedUser(username: $username) {
14+
contributions {
15+
points
16+
}
17+
profile {
18+
reputation
19+
ranking
20+
}
21+
submissionCalendar
22+
submitStats {
23+
acSubmissionNum {
24+
difficulty
25+
count
26+
submissions
27+
}
28+
totalSubmissionNum {
29+
difficulty
30+
count
31+
submissions
32+
}
33+
}
34+
}
35+
}
36+
`;
37+
38+
39+
// format data
40+
const formatData = (data) => {
41+
let sendData = {
42+
totalSolved: data.matchedUser.submitStats.acSubmissionNum[0].count,
43+
totalSubmissions: data.matchedUser.submitStats.totalSubmissionNum,
44+
totalQuestions: data.allQuestionsCount[0].count,
45+
easySolved: data.matchedUser.submitStats.acSubmissionNum[1].count,
46+
totalEasy: data.allQuestionsCount[1].count,
47+
mediumSolved: data.matchedUser.submitStats.acSubmissionNum[2].count,
48+
totalMedium: data.allQuestionsCount[2].count,
49+
hardSolved: data.matchedUser.submitStats.acSubmissionNum[3].count,
50+
totalHard: data.allQuestionsCount[3].count,
51+
ranking: data.matchedUser.profile.ranking,
52+
contributionPoint: data.matchedUser.contributions.points,
53+
reputation: data.matchedUser.profile.reputation,
54+
submissionCalendar: JSON.parse(data.matchedUser.submissionCalendar)
55+
}
56+
return sendData;
57+
}
58+
59+
//fetching the data
60+
exports.leetcode = (req, res) => {
61+
let user = req.params.id;
62+
fetch('https://leetcode.com/graphql', {
63+
method: 'POST',
64+
headers: {
65+
'Content-Type': 'application/json',
66+
'Referer': 'https://leetcode.com'
67+
},
68+
body: JSON.stringify({query: query, variables: {username: user}}),
69+
70+
})
71+
.then(result => result.json())
72+
.then(data => {
73+
if(data.errors){
74+
res.send(data);
75+
}else {
76+
res.send(formatData(data.data));
77+
}
78+
})
79+
.catch(err=>{
80+
console.error('Error', err);
81+
res.send(err);
82+
});
83+
}

node_modules/.bin/mime

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime.cmd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime.ps1

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)