Skip to content

Commit b8088fc

Browse files
committed
fix: ignore users with no submissions in participants list, update: scheduling
1 parent 0469af6 commit b8088fc

File tree

3 files changed

+111
-104
lines changed

3 files changed

+111
-104
lines changed

server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ app.use('/',indexRouter)
2828
app.listen(process.env.PORT || 3000)
2929

3030
// const predict = require('./services/predict')
31-
// const schedualing = require('./services/scheduling')
31+
const schedualing = require('./services/scheduling')
32+
schedualing.fetchAllContests()
3233
// schedualing.fetchAllContests()
3334
// const users = require('./services/users')
35+
const contest = require('./services/contests')
36+

services/contests.js

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,133 @@
11
const fetch = require('node-fetch')
22

33
const Contest = require('../models/contest')
4-
let halfHour = 1000*60*30
4+
let halfHour = 1000 * 60 * 30
55

6-
const fetchContestRankings = async function(contestSlug) {
6+
const fetchContestRankings = async function (contestSlug) {
77

8-
let contest = await Contest.findById(contestSlug)
9-
if(contest===null || contest.rankings.length===0){
10-
11-
try {
12-
rankings = []
13-
console.log(`fetching ${contestSlug}`)
14-
let response = await fetch(`https://leetcode.com/contest/api/ranking/${contestSlug}/?pagination=1&region=global`);
15-
response = await response.json()
16-
let contest_id = response.total_rank[0].contest_id
17-
let num_User = response.user_num
18-
// TODO: remove hard coded lines
19-
let pages = 10//Math.floor(response.user_num/25)
20-
for(let i=1;i<=pages;i++){
21-
console.log("fetching page no.: "+ i)
22-
let res = await fetch(`https://leetcode.com/contest/api/ranking/${contestSlug}/?pagination=${i}&region=global`);
23-
res = await res.json()
24-
for(ranks of res.total_rank){
25-
let {username , user_slug , country_code , country_name ,data_region, rank} = ranks
26-
let ranking = {username, user_slug, country_code, country_name, data_region, rank}
27-
ranking["_id"] = username
28-
rankings.push(ranking)
29-
}
30-
}
31-
let newContest = new Contest({
32-
_id: contestSlug,
33-
contest_id: contest_id,
34-
lastUpdated: Date.now(),
35-
rankings: rankings,
36-
num_user: num_User
37-
})
38-
if(contest===null ){
39-
await newContest.save()
40-
console.log(`Created contest ${contestSlug}`)
8+
try {
9+
let contest = await Contest.findById(contestSlug)
10+
if (!contest) {
11+
return null
12+
}
4113

14+
rankings = []
15+
console.log(`fetching ${contestSlug} ...`)
16+
let response = await fetch(`https://leetcode.com/contest/api/ranking/${contestSlug}/?pagination=1&region=global`);
17+
response = await response.json()
18+
let contest_id = response.total_rank[0].contest_id
19+
let num_User = response.user_num
20+
// TODO: remove hard coded lines
21+
22+
let pages = Math.floor(response.user_num / 25)
23+
for (let i = 1; i <= pages; i++) {
24+
console.log("fetching page no.: " + i)
25+
let res = await fetch(`https://leetcode.com/contest/api/ranking/${contestSlug}/?pagination=${i}&region=global`);
26+
res = await res.json()
27+
// console.log(res)
28+
for (ranks of res.total_rank) {
29+
30+
let {
31+
username,
32+
user_slug,
33+
country_code,
34+
country_name,
35+
data_region,
36+
rank,
37+
score,
38+
finish_time,
39+
} = ranks
40+
41+
// no submission
42+
if (score === 0 && finish_time * 1000 == contest.startTime.getTime()) {
43+
break // can also break page loop
44+
}
45+
let ranking = {
46+
username,
47+
user_slug,
48+
country_code,
49+
country_name,
50+
data_region,
51+
rank
52+
}
53+
ranking["_id"] = username
54+
rankings.push(ranking)
4255
}
43-
else{
44-
let updatedContest = new Contest({
45-
_id: contestSlug,
46-
contest_id: contest_id,
47-
lastUpdated: Date.now(),
48-
rankings: rankings,
49-
startTime: contest.startTime,
50-
endTime: contest.endTime,
51-
num_user: num_User
52-
})
53-
await Contest.findByIdAndUpdate(contestSlug, updatedContest)
54-
console.log(`Updated Rankings in contest ${contestSlug}`)
55-
}
56-
return newContest
57-
}
58-
catch (error) {
59-
console.error(error);
60-
return null
6156
}
62-
}
63-
else{
64-
console.log("Aldready in db");
57+
58+
let updatedContest = new Contest({
59+
contest_id: contest_id,
60+
lastUpdated: Date.now(),
61+
rankings: rankings,
62+
num_user: num_User
63+
})
64+
65+
contest = await Contest.findByIdAndUpdate(contestSlug, updatedContest, {
66+
new: true
67+
})
68+
console.log(`Updated Rankings in ${contestSlug}`)
69+
6570
return contest
71+
} catch (error) {
72+
console.error(error);
73+
return null
6674
}
6775
}
68-
6976
const fetchContest = async () => {
7077

7178
try {
7279
let res = await fetch("https://leetcode.com/graphql", {
73-
"headers": {
74-
"accept": "*/*",
75-
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
76-
"content-type": "application/json",
77-
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"",
78-
"sec-ch-ua-mobile": "?0",
79-
"sec-fetch-dest": "empty",
80-
"sec-fetch-mode": "cors",
81-
"sec-fetch-site": "same-origin",
82-
"x-newrelic-id": "UAQDVFVRGwEAXVlbBAg="
83-
84-
},
85-
"referrer": "https://leetcode.com/contest/",
86-
"referrerPolicy": "strict-origin-when-cross-origin",
87-
"body": "{\"operationName\":null,\"variables\":{},\"query\":\"{\\n brightTitle\\n currentTimestamp\\n allContests {\\n containsPremium\\n title\\n cardImg\\n titleSlug\\n description\\n startTime\\n duration\\n originStartTime\\n isVirtual\\n company {\\n watermark\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}",
88-
"method": "POST",
89-
"mode": "cors"
80+
"headers": {
81+
"accept": "*/*",
82+
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
83+
"content-type": "application/json",
84+
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"",
85+
"sec-ch-ua-mobile": "?0",
86+
"sec-fetch-dest": "empty",
87+
"sec-fetch-mode": "cors",
88+
"sec-fetch-site": "same-origin",
89+
},
90+
"referrer": "https://leetcode.com/contest/",
91+
"referrerPolicy": "strict-origin-when-cross-origin",
92+
"body": "{\"operationName\":null,\"variables\":{},\"query\":\"{\\n brightTitle\\n currentTimestamp\\n allContests {\\n containsPremium\\n title\\n cardImg\\n titleSlug\\n description\\n startTime\\n duration\\n originStartTime\\n isVirtual\\n company {\\n watermark\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}",
93+
"method": "POST",
94+
"mode": "cors"
9095
});
9196
res = await res.json()
9297
//console.log(res.data.allContests[0])
9398
//let contestSlug = res.data.allContests[0].titleSlug
9499
//let startTime = res.data.allContests[0].startTime*1000
95100
//let endTime = startTime + res.data.allContests[0].duration*1000
96-
for(let i=0;i<res.data.allContests.length;i++)
97-
{
101+
for (let i = 0; i < res.data.allContests.length; i++) {
98102
//console.log(i)
99103
let contest = res.data.allContests[i];
100104
let isfound = await Contest.findById(contest.titleSlug)
101-
if(isfound){
102-
break;
103-
}
105+
if (isfound) {
106+
break
107+
}
104108
let newContest = new Contest({
105109
_id: contest.titleSlug,
106-
startTime: contest.startTime*1000,
107-
endTime: contest.startTime*1000 + contest.duration*1000,
110+
startTime: contest.startTime * 1000,
111+
endTime: contest.startTime * 1000 + contest.duration * 1000,
108112
lastUpdated: Date.now(),
109-
num_user : contest.num_user
113+
num_user: contest.num_user
110114
})
111115
let oldContest = await Contest.findById(contest.titleSlug)
112-
if(oldContest==null){
113-
await newContest.save()
114-
}
115-
else{
116-
Contest.findByIdAndUpdate(contest.titleSlug,newContest)
117-
}
116+
await Contest.findByIdAndUpdate(contest.titleSlug, newContest)
118117
}
119118
return res.data.allContests
120-
}
121-
catch(error){
119+
} catch (error) {
122120
console.log(error)
121+
return null
123122
}
124123
}
125-
const getContestRankings = async function(contestSlug){
124+
const getContestRankings = async function (contestSlug) {
126125
let contest = await Contest.findById(contestSlug)
127-
if(!contest || !contest.rankings || !contest.rankings.length){
126+
if (!contest || !contest.rankings || !contest.rankings.length) {
128127
contest = await fetchContestRankings(contestSlug)
129128
}
130129
return contest
131-
}
130+
}
132131

133132
// exports
134133
module.exports.fetchContest = fetchContest

services/scheduling.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@ const job = schedule.scheduleJob({hour: 00, minute: 00,},async function(){
1515
getContestRankings(contestList[i].titleSlug)
1616
});
1717
}
18+
let endTime = contestList[i].startTime*1000 + contestList[i].duration*1000
19+
if(Date.now()>endTime){
20+
await getContestRankings(contestList[i].titleSlug)
21+
}
1822
}
1923
});
2024

21-
// const fetchNow = async function(){
22-
// let contestList = await fetchContest()
23-
// for(let i=0;i<contestList.length;i++){
24-
// if(contestList[i].rankings && contestList[i].rankings.length>0)
25-
// break;
26-
// await getContestRankings(contestList[i].titleSlug)
27-
// }
28-
29-
// }
30-
// exports.fetchAllContests = fetchNow
25+
const fetchNow = async function(){
26+
let contestList = await fetchContest()
27+
if(!contestList)
28+
return
29+
let promises = contestList.map(async (contest)=>{
30+
let endTime = contest.startTime*1000 + contest.duration*1000
31+
if(Date.now()> endTime)
32+
await getContestRankings(contest.titleSlug)
33+
})
34+
}
35+
exports.fetchAllContests = fetchNow

0 commit comments

Comments
 (0)