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