|
| 1 | +const path = require("path") |
| 2 | + |
| 3 | +require("dotenv").config() |
| 4 | + |
| 5 | +const axios = require("axios").default |
| 6 | + |
| 7 | +const fs = require("fs") |
| 8 | + |
| 9 | +const util = require("util") |
| 10 | + |
| 11 | +const INTERVAL = 3000; |
| 12 | +const {Worker , workerData } = require("worker_threads") |
| 13 | + |
| 14 | +const URL = "https://leetcode.com/api/submissions/" |
| 15 | +const PROBLEM_URL = "https://leetcode.com/api/problems/all" |
| 16 | + |
| 17 | +const all_problems = require("./problemstat.json"); |
| 18 | + |
| 19 | +const cookieVal = process.env.COOKIE_SECRET ; |
| 20 | + |
| 21 | +if(cookieVal === null || cookieVal === undefined || cookieVal.length === 0) |
| 22 | +{ |
| 23 | + throw 'Set COOKIE_SECRET in repo secrets' |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +const {SolutionDetails} = require("./SolutionDetails.js") |
| 28 | + |
| 29 | +const readFileDir = util.promisify(fs.readdir) |
| 30 | + |
| 31 | +let aldyPresentSol = {} |
| 32 | + |
| 33 | +function mapFileWithId(){ |
| 34 | + return new Promise( async (resolve , reject ) => { |
| 35 | + const subPresent = await readFileDir("../../") ; |
| 36 | + subPresent.map(val => { |
| 37 | + if(val.indexOf("_") >= 0) |
| 38 | + { |
| 39 | + let f = val.split("_")[0] |
| 40 | + aldyPresentSol[f] = 1 |
| 41 | + } |
| 42 | + }) |
| 43 | + return resolve(); |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +SolutionDetails.prototype.IsPresent = function(){ |
| 48 | + if(aldyPresentSol[this.id] === 1 ) |
| 49 | + { |
| 50 | + return true; |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +const worker = new Worker('./worker.js' ) |
| 59 | + |
| 60 | + |
| 61 | +worker.on('message', ()=>{ |
| 62 | + console.log("done writing") |
| 63 | +}) |
| 64 | + |
| 65 | +worker.on('messageerror' , (err)=>[ |
| 66 | + console.log("messageerror ", err) |
| 67 | +]) |
| 68 | + |
| 69 | +worker.on('error', (err)=>{ |
| 70 | + console.log(err ," messsage ") |
| 71 | +}) |
| 72 | + |
| 73 | +worker.on('exit', ()=>{ |
| 74 | + console.log("done this Work") |
| 75 | +}) |
| 76 | + |
| 77 | +let solutionPromise = (question) => new Promise((resolve, reject) => { |
| 78 | + axios({ |
| 79 | + method : 'GET', |
| 80 | + baseURL : `${URL}${question.question__title_slug}`, |
| 81 | + headers : { |
| 82 | + cookie : cookieVal |
| 83 | + } |
| 84 | + }) |
| 85 | + .then(async (res)=>{ |
| 86 | + //console.log(res.data) |
| 87 | + console.log(res, "reaching herre") |
| 88 | + worker.postMessage({workerData : res.data} ) |
| 89 | + resolve() |
| 90 | + }) |
| 91 | + .catch(err => { |
| 92 | + console.log("err",err.message) |
| 93 | + }) |
| 94 | + .then(()=>{ |
| 95 | + clearTimeout(sleep) |
| 96 | + resolve() |
| 97 | + }) |
| 98 | +}) |
| 99 | + |
| 100 | +const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); |
| 101 | + |
| 102 | +async function OneTimeFetch(){ |
| 103 | + try { |
| 104 | + for(let i = 0 ; i < all_problems.stat_status_pairs.length ; i++ ) |
| 105 | + { |
| 106 | + const question = all_problems.stat_status_pairs[i] |
| 107 | + await solutionPromise(question.stat) |
| 108 | + await sleep(INTERVAL ) |
| 109 | + } |
| 110 | + worker.postMessage({workerData : 'EXIT'} ) |
| 111 | + } |
| 112 | + catch(err) |
| 113 | + { |
| 114 | + process.exit(err) ; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +async function DailyFetch (){ |
| 119 | + console.log("is it running") |
| 120 | + try { |
| 121 | + const r_recentSubmittedSols = await axios({ |
| 122 | + method : 'GET', |
| 123 | + baseURL : URL, |
| 124 | + headers : { |
| 125 | + cookie : cookieVal |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + const bVal = r_recentSubmittedSols.data.submissions_dump; |
| 130 | + await FileWriteHdl(bVal) |
| 131 | + console.log("reachign here") |
| 132 | + process.exit() |
| 133 | + } catch (err) |
| 134 | + { |
| 135 | + console.log(err.message ) |
| 136 | + process.exit(err); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +const FileWriteHdl = async bVal => { |
| 141 | + |
| 142 | + for(let i = 0 ; i < bVal.length ; i++ ) |
| 143 | + { |
| 144 | + if(bVal[i].status_display === 'Accepted') |
| 145 | + { |
| 146 | + const sol_obj = new SolutionDetails(bVal[i]); |
| 147 | + if(!sol_obj.IsPresent()) |
| 148 | + { |
| 149 | + console.log("running") |
| 150 | + await sol_obj.fmtHdl() |
| 151 | + aldyPresentSol[this.id] = 1 ; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +;(async ()=>{ |
| 158 | + console.time() |
| 159 | + await mapFileWithId() |
| 160 | + if(Object.keys(aldyPresentSol).length >= 1) |
| 161 | + { |
| 162 | + DailyFetch() |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + OneTimeFetch() |
| 167 | + } |
| 168 | +})() |
| 169 | + |
| 170 | +process.on('exit', (err)=>{ |
| 171 | + console.timeEnd() |
| 172 | + if(err) |
| 173 | + { |
| 174 | + console.log("err", err.message) |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + console.log("exited peacefully!!") |
| 179 | + } |
| 180 | +}) |
0 commit comments