Skip to content

Commit 09b59be

Browse files
authored
Merge pull request #74 from Ayushman2004/feature/contests
Added new /contests and /contests/upcoming endpoints to fetch all the contests and future contests respectively.
2 parents bd3b316 + 926dede commit 09b59be

File tree

8 files changed

+128
-8
lines changed

8 files changed

+128
-8
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ During development, it's recommended to utilize the API locally. To do so, follo
8686
| _Skip & Filter & Limited Problems_ | `/problems?tags=tag1+tag2+tag3&limit=number&skip=number` | Get a list of a specified **_number_** of problems based on selected **_tags_** skipping a specified **number** of problems. | <a href="./public/demo/demo21.png" target="_blank">click here</a> |
8787
| _Official Solution_ | `/officialSolution?titleSlug=selected-question` | Get Get the official solution(leetcode) for a question | TODO |
8888

89+
### Contests
90+
91+
| Details | Endpoint | Description | Demo |
92+
| :--------------------------------- | :------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
93+
| _All Contests_ | `/contests` | Get all the contests | TODO |
94+
| _Upcoming Contests_ | `/contests/upcoming` | Get the upcoming contests. | TODO |
95+
96+
97+
8998
### Discussion
9099

91100
| Details | Endpoint | Description |
@@ -107,6 +116,7 @@ During development, it's recommended to utilize the API locally. To do so, follo
107116
| [@kvqn](https://www.github.com/kvqn) | PORT environment variable |
108117
| [@changchunlei](https://github.com/changchunlei) | New Endpoints - language stats, integrated user profile, contest and discussions |
109118
| [@merakesh99](https://github.com/merakesh99) | Hot reload issue solved |
119+
| [@Ayushman2004](https://github.com/Ayushman2004) | Added Endpoints: all-contests, upcoming-contests |
110120
| [@ajchili](https://github.com/ajchili) | Skip param to fetch problems |
111121
| [@theinit01](https://github.com/theinit01) | Temp fix for skip |
112122
| [@123xylem](https://github.com/123xylem) | Add Descriptions and Methods to API route documentation. |

package-lock.json

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

src/Controllers/fetchContests.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Response } from 'express';
2+
3+
export const fetchAllContests = async (
4+
res: Response,
5+
query: string
6+
) => {
7+
try {
8+
const response = await fetch('https://leetcode.com/graphql', {
9+
method: 'POST',
10+
headers: {
11+
'Content-Type': 'application/json',
12+
Referer: 'https://leetcode.com',
13+
},
14+
body: JSON.stringify({
15+
query: query
16+
}),
17+
});
18+
19+
const result = await response.json();
20+
if (!response.ok) {
21+
console.error(`HTTP error! status: ${response.status}`);
22+
}
23+
if (result.errors) {
24+
return res.send(result);
25+
}
26+
27+
return res.json(result.data);
28+
} catch (err) {
29+
console.error('Error: ', err);
30+
return res.send(err);
31+
}
32+
};
33+
34+
export const fetchUpcomingContests = async (
35+
res: Response,
36+
query: string
37+
) => {
38+
try {
39+
const response = await fetch('https://leetcode.com/graphql', {
40+
method: 'POST',
41+
headers: {
42+
'Content-Type': 'application/json',
43+
Referer: 'https://leetcode.com',
44+
},
45+
body: JSON.stringify({
46+
query: query
47+
}),
48+
});
49+
50+
const result = await response.json();
51+
if (!response.ok) {
52+
console.error(`HTTP error! status: ${response.status}`);
53+
}
54+
if (result.errors) {
55+
return res.send(result);
56+
}
57+
58+
const now = Math.floor(Date.now() / 1000);
59+
const allContests = result.data.allContests || [];
60+
61+
const upcomingContests = allContests.filter(
62+
(contest: any) => contest.startTime > now
63+
);
64+
65+
66+
return res.json({
67+
count: upcomingContests.length,
68+
contests: upcomingContests,
69+
});
70+
} catch (err) {
71+
console.error('Error: ', err);
72+
return res.send(err);
73+
}
74+
};
75+
76+

src/Controllers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export { default as fetchUserDetails } from './fetchUserDetails';
44
export { default as fetchTrendingTopics } from './fetchDiscussion';
55
export { default as fetchDataRawFormat } from './fetchDataRawFormat';
66
export { default as handleRequest } from './handleRequest';
7-
export { default as fetchUserProfile } from './fetchUserProfile';
7+
export { default as fetchUserProfile } from './fetchUserProfile';
8+
export { fetchAllContests, fetchUpcomingContests } from './fetchContests';

src/GQLQueries/allContests.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const allContestQuery = `
2+
query allContests {
3+
allContests {
4+
title
5+
titleSlug
6+
startTime
7+
duration
8+
originStartTime
9+
isVirtual
10+
containsPremium
11+
}
12+
}
13+
`

src/GQLQueries/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export { skillStatsQuery } from './skillStats';
1515
export { userQuestionProgressQuery } from './userQuestionProgress';
1616
export { discussTopicQuery } from './discussTopic';
1717
export { discussCommentsQuery } from './discussComments';
18-
export { userContestRankingInfoQuery } from './userContestRanking';
18+
export { userContestRankingInfoQuery } from './userContestRanking';
19+
export { allContestQuery } from './allContests';

src/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ app.get('/officialSolution', leetcode.officialSolution);
112112
//get list of problems
113113
app.get('/problems', leetcode.problems);
114114

115+
//get contests
116+
app.get('/contests', leetcode.allContests);
117+
app.get('/contests/upcoming', leetcode.upcomingContests);
115118

116119
// Construct options object on all user routes.
117120
app.use(

src/leetCode.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,25 @@ export const userProfileUserQuestionProgressV2_ = (req: Request, res: Response)
303303
export const userContestRankingInfo_ = (req: Request, res: Response) => {
304304
const { username } = req.params;
305305
controllers.handleRequest(res, gqlQueries.userContestRankingInfoQuery, { username });
306-
};
306+
};
307+
308+
//limiting is not supported in the contests unlike problems
309+
export const allContests = (
310+
_req: Request,
311+
res: Response
312+
) => {
313+
controllers.fetchAllContests(
314+
res,
315+
gqlQueries.allContestQuery
316+
);
317+
};
318+
319+
export const upcomingContests = (
320+
_req: Request,
321+
res: Response
322+
) => {
323+
controllers.fetchUpcomingContests(
324+
res,
325+
gqlQueries.allContestQuery
326+
);
327+
};

0 commit comments

Comments
 (0)