31 |
32 |
1. All Contests
33 |
34 |
35 |
36 | Return type: JSON
37 | value: Array of all contests
38 |
39 |
40 |
41 |
42 |
43 | | Field |
44 | Value |
45 |
46 |
47 | | site |
48 | Name of the site / platform |
49 |
50 |
51 |
52 | | title |
53 | Name of the contest |
54 |
55 |
56 | | startTime |
57 | Start time of the contest |
58 |
59 |
60 |
61 | | duration |
62 | Duration of the contest |
63 |
64 |
65 | | endTime |
66 | End time of the contest |
67 |
68 |
69 | | url |
70 | URL of the contest |
71 |
72 |
73 |
74 |
75 |
Response
76 |
77 | [
78 | {
79 | "site": "codeforces",
80 | "title": "Codeforces Round #842 (Div. 2)",
81 | "startTime": 1672929300000,
82 | "duration": 7200000,
83 | "endTime": 1672936500000,
84 | "url": "https://codeforces.com/contest/1768"
85 | },
86 | {
87 | "site": "leetcode",
88 | "title": "Biweekly Contest 95",
89 | "startTime": 1673101800000,
90 | "duration": 324000000,
91 | "endTime": 1673425800000,
92 | "url": "https://leetcode.com/contest/biweekly-contest-95"
93 | },
94 | {
95 | "site": "leetcode",
96 | "title": "Weekly Contest 327",
97 | "startTime": 1673145000000,
98 | "duration": 324000000,
99 | "endTime": 1673469000000,
100 | "url": "https://leetcode.com/contest/weekly-contest-327"
101 | }
102 | ]
103 |
104 |
105 |
106 |
107 |
108 |
2. Codeforces Contests
109 |
110 |
111 |
112 | Returns : Array of all codeforces contests
113 |
114 |
115 |
116 |
117 |
118 | | Field |
119 | Value |
120 |
121 |
122 | | site |
123 | Name of the site / platform |
124 |
125 |
126 |
127 | | title |
128 | Name of the contest |
129 |
130 |
131 | | startTime |
132 | Start time of the contest |
133 |
134 |
135 |
136 | | duration |
137 | Duration of the contest |
138 |
139 |
140 | | endTime |
141 | End time of the contest |
142 |
143 |
144 | | url |
145 | URL of the contest |
146 |
147 |
148 |
149 |
150 |
Response
151 |
152 | [
153 | {
154 | "site": "codeforces",
155 | "title": "Codeforces Round #842 (Div. 2)",
156 | "startTime": 1672929300000,
157 | "duration": 7200000,
158 | "endTime": 1672936500000,
159 | "url": "https://codeforces.com/contests/1768"
160 | },
161 | {
162 | "site": "codeforces",
163 | "title": "Educational Codeforces Round 141 (Rated for Div. 2)",
164 | "startTime": 1673188500000,
165 | "duration": 7200000,
166 | "endTime": 1673195700000,
167 | "url": "https://codeforces.com/contests/1783"
168 | }
169 | ]
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
--------------------------------------------------------------------------------
/public/index.js:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ravibabuvadde/competeapi/c42dcf52d3d94fe7d0baecb971998431248e29a4/public/index.js
--------------------------------------------------------------------------------
/public/js/script.js:
--------------------------------------------------------------------------------
1 | const codes = document.querySelectorAll('.json-code');
2 | codes.forEach((code) => {
3 | code.innerText = JSON.stringify(JSON.parse(code.innerText), null, 4);
4 | });
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const app = express();
3 | const cors = require('cors');
4 | const fs = require('fs');
5 |
6 | app.use(cors());
7 |
8 | app.use(express.static('public'));
9 |
10 | app.get('/', (req, res) => {
11 | const html = fs.readFileSync('public/home.html', 'utf-8');
12 | res.send(html);
13 | })
14 |
15 | app.use('/user', require('./user/index'));
16 | app.use('/contests', require('./contests/index'));
17 |
18 | app.listen(80,() => {
19 | console.log('Example app listening...!');
20 | });
21 |
22 | // Export the Express API
23 | module.exports = app;
24 |
--------------------------------------------------------------------------------
/user/codechef.js:
--------------------------------------------------------------------------------
1 | const cheerio = require('cheerio');
2 | const express = require('express');
3 | const router = express.Router();
4 | const axios = require('axios');
5 |
6 | router.get('/', (req, res) => {
7 | res.send('Hello World! from leetcode');
8 | });
9 |
10 | router.get('/:username', async (req, res) => {
11 | let $;
12 | let userinfo = await axios.get('https://www.codechef.com/users/'+req.params.username,{
13 | headers: {
14 | redirect: 'manual'
15 | }
16 | })
17 | .then((data)=>{$ = cheerio.load(data.data);})
18 | .catch((error) => {
19 | console.log(error);
20 | })
21 |
22 |
23 | let script = $('main script').last().text();
24 | let all_rating = script.match(/var all_rating = (.*);/);
25 | if(all_rating === null){
26 | all_rating = [{},{}]
27 | all_rating[1] = JSON.stringify(all_rating[1]);
28 | }
29 |
30 |
31 | let userdata = {
32 | username: '',
33 | rating: $('.rating').first().text(),
34 | rating_number: parseInt($('.rating-number').text()),
35 | country: $('.user-country-name').text(),
36 | user_type: '',
37 | institution: '',
38 | organisation: '',
39 | global_rank: $('.rating-ranks ul li').first().find('strong').text(),
40 | country_rank: $('.rating-ranks ul li').last().find('strong').text(),
41 | max_rank: parseInt($('.rating-header small').text().match(/\d+/)),
42 | all_rating: JSON.parse(all_rating[1]),
43 | }
44 | $('.user-details li').each((i,item)=>{
45 | if($(item).find('label').text() == 'Student/Professional:'){
46 | userdata.user_type = $(item).find('span').text();
47 | }else if($(item).find('label').text() == 'Institution:'){
48 | userdata.institution = $(item).find('span').text();
49 | }else if($(item).find('label').text() == 'Organisation:'){
50 | userdata.organisation = $(item).find('span').text();
51 |
52 | }else if($(item).find('label').text() == 'Username:'){
53 | userdata.username = $(item).find('span').last().text();
54 | }
55 | });
56 |
57 | if(userdata.username == ''){
58 | res.json({error: 'User not found'});
59 | }else{
60 | res.json(userdata);
61 | }
62 |
63 |
64 |
65 | });
66 |
67 | module.exports = router;
68 |
--------------------------------------------------------------------------------
/user/codeforces.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const router = express.Router();
3 | const axios = require('axios');
4 |
5 | router.get('/', (req, res) => {
6 | res.send('Hello World! from leetcode');
7 | });
8 |
9 | router.get('/:username', async (req, res) => {
10 | let userdata = []
11 | let userinfo = await axios.get('https://codeforces.com/api/user.info?handles='+req.params.username)
12 | .then((response)=> { (response.data.status == 'OK') ? userdata.push(response.data.result[0]) : userdata.push({ error: 'User not found' }) })
13 | .catch((error) => {
14 | userdata.push({ error: 'User not found' })
15 | });
16 | let userratings = await axios.get('https://codeforces.com/api/user.rating?handle='+req.params.username)
17 | .then((response)=> (response.data.status == 'OK') ? userdata.push({ratings:response.data.result}):"")
18 | .catch((error) => {
19 | });
20 | res.json(userdata);
21 | });
22 |
23 | module.exports = router;
24 |
--------------------------------------------------------------------------------
/user/index.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const router = express.Router();
3 |
4 | router.get('/', (req, res) => {
5 | res.send('Hello World! from user');
6 | });
7 |
8 | router.use('/leetcode', require('./leetcode'));
9 | router.use('/codeforces', require('./codeforces'));
10 | router.use('/codechef', require('./codechef'));
11 |
12 | module.exports = router;
13 |
--------------------------------------------------------------------------------
/user/leetcode.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const router = express.Router();
3 | const axios = require('axios');
4 |
5 | router.get('/', (req, res) => {
6 | res.send('Hello World! from leetcode');
7 | });
8 |
9 | router.get('/:username', (req, res) => {
10 | axios.post('https://leetcode.com/graphql',{
11 | headers: {
12 | 'Content-Type': 'application/json',
13 | },
14 | query: `{
15 | matchedUser(username: ` + '"' + req.params.username + '"' + `) {
16 | username
17 | profile{
18 | userAvatar
19 | }
20 | languageProblemCount{
21 | languageName
22 | problemsSolved
23 | }
24 | submitStats: submitStatsGlobal {
25 | acSubmissionNum {
26 | difficulty
27 | count
28 | submissions
29 | }
30 | }
31 | badges{
32 | name
33 | }
34 | userCalendar{
35 | streak
36 | totalActiveDays
37 | }
38 |
39 | }
40 | userContestRanking(username: ` + '"' + req.params.username + '"' + `) {
41 | attendedContestsCount
42 | globalRanking
43 | rating
44 | topPercentage
45 | totalParticipants
46 | }
47 |
48 | }`
49 | })
50 | .then((response) => {
51 | res.send(response.data);
52 | })
53 | .catch((error) => {
54 | res.send(error);
55 | });
56 |
57 | });
58 |
59 | module.exports = router;
60 |
--------------------------------------------------------------------------------