@@ -2,12 +2,12 @@ const axios = require('axios');
22const fetch = require ( 'node-fetch' )
33
44const Contest = require ( '../models/contest' )
5- let halfHour = 1000 // *60*30
5+ let halfHour = 1000 * 60 * 30
66
77const getContestRankings = async function ( contestSlug ) {
88
99 let contest = await Contest . findById ( contestSlug )
10- if ( contest === null || Date . now ( ) - contest . lastUpdated >= halfHour ) {
10+ if ( contest === null || contest . rankings . length === 0 ) {
1111
1212 try {
1313 rankings = [ ]
@@ -32,12 +32,22 @@ const getContestRankings = async function(contestSlug) {
3232 lastUpdated : Date . now ( ) ,
3333 rankings : rankings
3434 } )
35- if ( contest === null ) {
35+ if ( contest === null ) {
3636 await newContest . save ( )
37+ console . log ( `Created contest ${ contestSlug } ` )
38+
3739 }
3840 else {
39- await Contest . findByIdAndUpdate ( contestSlug , newContest )
40- console . log ( `Updated contest ${ contestSlug } ` )
41+ let updatedContest = new Contest ( {
42+ _id : contestSlug ,
43+ contest_id : contest_id ,
44+ lastUpdated : Date . now ( ) ,
45+ rankings : rankings ,
46+ startTime : contest . startTime ,
47+ endTime : contest . endTime ,
48+ } )
49+ await Contest . findByIdAndUpdate ( contestSlug , updatedContest )
50+ console . log ( `Updated Rankings in contest ${ contestSlug } ` )
4151 }
4252 }
4353 catch ( error ) {
@@ -49,5 +59,67 @@ const getContestRankings = async function(contestSlug) {
4959 }
5060
5161}
52- module . exports = getContestRankings
62+
63+
64+ const fetchContest = async ( ) => {
65+
66+ try {
67+ let res = await fetch ( "https://leetcode.com/graphql" , {
68+ "headers" : {
69+ "accept" : "*/*" ,
70+ "accept-language" : "en-GB,en-US;q=0.9,en;q=0.8" ,
71+ "content-type" : "application/json" ,
72+ "sec-ch-ua" : "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"" ,
73+ "sec-ch-ua-mobile" : "?0" ,
74+ "sec-fetch-dest" : "empty" ,
75+ "sec-fetch-mode" : "cors" ,
76+ "sec-fetch-site" : "same-origin" ,
77+ "x-newrelic-id" : "UAQDVFVRGwEAXVlbBAg="
78+
79+ } ,
80+ "referrer" : "https://leetcode.com/contest/" ,
81+ "referrerPolicy" : "strict-origin-when-cross-origin" ,
82+ "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\"}" ,
83+ "method" : "POST" ,
84+ "mode" : "cors"
85+ } ) ;
86+ res = await res . json ( )
87+ //console.log(res.data.allContests[0])
88+ let contestSlug = res . data . allContests [ 0 ] . titleSlug
89+ let startTime = res . data . allContests [ 0 ] . startTime
90+ let endTime = startTime + res . data . allContests [ 0 ] . duration
91+ for ( let i = 0 ; i < res . data . allContests . length ; i ++ )
92+ {
93+ let contest = res . data . allContests [ i ] ;
94+ let isfound = await Contest . findById ( contest . titleSlug )
95+ if ( isfound ) {
96+ break ;
97+ }
98+ let newContest = new Contest ( {
99+ _id : contest . titleSlug ,
100+ startTime : contest . startTime ,
101+ endTime : startTime + contest . duration * 1000 ,
102+ lastUpdated : Date . now ( ) ,
103+ } )
104+ await newContest . save ( )
105+ }
106+ return res . data . allContests
107+ }
108+ catch ( error ) {
109+ console . log ( error )
110+ }
111+
112+
113+
114+ }
115+
116+
117+
118+
119+
120+
121+
122+
123+ module . exports . getContestRankings = getContestRankings
124+ module . exports . fetchContest = fetchContest
53125
0 commit comments