@@ -4,29 +4,25 @@ const Contest = require('../models/contest')
44let halfHour = 1000 * 60 * 30
55
66const fetchContestRankings = async function ( contestSlug ) {
7-
87 try {
98 let contest = await Contest . findById ( contestSlug )
109 if ( ! contest ) {
10+ console . error ( `Contest ${ contestSlug } not found in the db` )
1111 return null
1212 }
1313
14- rankings = [ ]
1514 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 )
2321 for ( let i = 1 ; i <= pages ; i ++ ) {
24- console . log ( "fetching page no.: " + i )
22+ console . log ( `Fetching rankings ( ${ contestSlug } ): page: ${ i } ` )
2523 let res = await fetch ( `https://leetcode.com/contest/api/ranking/${ contestSlug } /?pagination=${ i } ®ion=global` ) ;
2624 res = await res . json ( )
27- // console.log(res)
2825 for ( ranks of res . total_rank ) {
29-
3026 let {
3127 username,
3228 user_slug,
@@ -59,7 +55,8 @@ const fetchContestRankings = async function (contestSlug) {
5955 contest_id : contest_id ,
6056 lastUpdated : Date . now ( ) ,
6157 rankings : rankings ,
62- num_user : num_User
58+ num_user : num_user ,
59+ ratings_fetched : true
6360 } )
6461
6562 contest = await Contest . findByIdAndUpdate ( contestSlug , updatedContest , {
@@ -68,8 +65,8 @@ const fetchContestRankings = async function (contestSlug) {
6865 console . log ( `Updated Rankings in ${ contestSlug } ` )
6966
7067 return contest
71- } catch ( error ) {
72- console . error ( error ) ;
68+ } catch ( err ) {
69+ console . error ( err ) ;
7370 return null
7471 }
7572}
@@ -89,7 +86,19 @@ const fetchContest = async () => {
8986 } ,
9087 "referrer" : "https://leetcode.com/contest/" ,
9188 "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+ "}` ,
93102 "method" : "POST" ,
94103 "mode" : "cors"
95104 } ) ;
@@ -99,11 +108,10 @@ const fetchContest = async () => {
99108 //let startTime = res.data.allContests[0].startTime*1000
100109 //let endTime = startTime + res.data.allContests[0].duration*1000
101110 for ( let i = 0 ; i < res . data . allContests . length ; i ++ ) {
102- //console.log(i)
103111 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
107115 }
108116 let newContest = new Contest ( {
109117 _id : contest . titleSlug ,
@@ -112,18 +120,20 @@ const fetchContest = async () => {
112120 lastUpdated : Date . now ( ) ,
113121 num_user : contest . num_user
114122 } )
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 } ` )
117125 }
118126 return res . data . allContests
119- } catch ( error ) {
120- console . log ( error )
127+ } catch ( err ) {
128+ console . error ( err )
121129 return null
122130 }
123131}
124132const 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 ) {
127137 contest = await fetchContestRankings ( contestSlug )
128138 }
129139 return contest
0 commit comments