@@ -4,29 +4,25 @@ const Contest = require('../models/contest')
4
4
let halfHour = 1000 * 60 * 30
5
5
6
6
const fetchContestRankings = async function ( contestSlug ) {
7
-
8
7
try {
9
8
let contest = await Contest . findById ( contestSlug )
10
9
if ( ! contest ) {
10
+ console . error ( `Contest ${ contestSlug } not found in the db` )
11
11
return null
12
12
}
13
13
14
- rankings = [ ]
15
14
console . log ( `fetching ${ contestSlug } ...` )
16
- let response = await fetch ( `https://leetcode.com/contest/api/ranking/${ contestSlug } /?pagination=1®ion=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 )
15
+ rankings = [ ]
16
+ let resp = await fetch ( `https://leetcode.com/contest/api/ranking/${ contestSlug } /?pagination=1®ion=global` ) ;
17
+ resp = await resp . json ( )
18
+ let contest_id = resp . total_rank [ 0 ] . contest_id
19
+ let num_user = resp . user_num
20
+ let pages = Math . floor ( resp . user_num / 25 )
23
21
for ( let i = 1 ; i <= pages ; i ++ ) {
24
- console . log ( "fetching page no.: " + i )
22
+ console . log ( `Fetching rankings ( ${ contestSlug } ): page: ${ i } ` )
25
23
let res = await fetch ( `https://leetcode.com/contest/api/ranking/${ contestSlug } /?pagination=${ i } ®ion=global` ) ;
26
24
res = await res . json ( )
27
- // console.log(res)
28
25
for ( ranks of res . total_rank ) {
29
-
30
26
let {
31
27
username,
32
28
user_slug,
@@ -59,7 +55,8 @@ const fetchContestRankings = async function (contestSlug) {
59
55
contest_id : contest_id ,
60
56
lastUpdated : Date . now ( ) ,
61
57
rankings : rankings ,
62
- num_user : num_User
58
+ num_user : num_user ,
59
+ ratings_fetched : true
63
60
} )
64
61
65
62
contest = await Contest . findByIdAndUpdate ( contestSlug , updatedContest , {
@@ -68,8 +65,8 @@ const fetchContestRankings = async function (contestSlug) {
68
65
console . log ( `Updated Rankings in ${ contestSlug } ` )
69
66
70
67
return contest
71
- } catch ( error ) {
72
- console . error ( error ) ;
68
+ } catch ( err ) {
69
+ console . error ( err ) ;
73
70
return null
74
71
}
75
72
}
@@ -89,7 +86,19 @@ const fetchContest = async () => {
89
86
} ,
90
87
"referrer" : "https://leetcode.com/contest/" ,
91
88
"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\"}" ,
89
+ "body" : `{"operationName":null,"variables":{},"query":"{\
90
+ currentTimestamp\
91
+ allContests {\
92
+ containsPremium\
93
+ title\
94
+ titleSlug\
95
+ startTime\
96
+ duration\
97
+ originStartTime\
98
+ isVirtual\
99
+ }\
100
+ }\
101
+ "}` ,
93
102
"method" : "POST" ,
94
103
"mode" : "cors"
95
104
} ) ;
@@ -99,11 +108,10 @@ const fetchContest = async () => {
99
108
//let startTime = res.data.allContests[0].startTime*1000
100
109
//let endTime = startTime + res.data.allContests[0].duration*1000
101
110
for ( let i = 0 ; i < res . data . allContests . length ; i ++ ) {
102
- //console.log(i)
103
111
let contest = res . data . allContests [ i ] ;
104
- let isfound = await Contest . findById ( contest . titleSlug )
105
- if ( isfound ) {
106
- break
112
+ let dbContest = await Contest . findById ( contest . titleSlug )
113
+ if ( dbContest ) {
114
+ continue
107
115
}
108
116
let newContest = new Contest ( {
109
117
_id : contest . titleSlug ,
@@ -112,18 +120,20 @@ const fetchContest = async () => {
112
120
lastUpdated : Date . now ( ) ,
113
121
num_user : contest . num_user
114
122
} )
115
- let oldContest = await Contest . findById ( contest . titleSlug )
116
- await Contest . findByIdAndUpdate ( contest . titleSlug , newContest )
123
+ await newContest . save ( )
124
+ console . log ( `created new contest: ${ contest . titleSlug } ` )
117
125
}
118
126
return res . data . allContests
119
- } catch ( error ) {
120
- console . log ( error )
127
+ } catch ( err ) {
128
+ console . error ( err )
121
129
return null
122
130
}
123
131
}
124
132
const getContestRankings = async function ( contestSlug ) {
125
- let contest = await Contest . findById ( contestSlug )
126
- if ( ! contest || ! contest . rankings || ! contest . rankings . length ) {
133
+ let contest = await Contest . findById ( contestSlug , {
134
+ rankings : 0
135
+ } )
136
+ if ( ! contest || ! contest . ratings_fetched ) {
127
137
contest = await fetchContestRankings ( contestSlug )
128
138
}
129
139
return contest
0 commit comments