From 25f4265099923c2d2f86620ce356b1c03f344f01 Mon Sep 17 00:00:00 2001 From: Tarun Sharma Date: Fri, 12 Nov 2021 16:34:29 +0530 Subject: [PATCH] refactor --- src/app.js | 61 ++++++++++++++++++++++++++++++++------------------- src/helper.js | 2 ++ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/app.js b/src/app.js index e377fd2..6b49c4c 100644 --- a/src/app.js +++ b/src/app.js @@ -2,42 +2,45 @@ const path = require("path") const axios = require("axios").default const fs = require("fs") const util = require("util") +const core = require("@actions/core") const {Worker, workerData} = require("worker_threads") const worker = new Worker(`${path.join(__dirname, 'worker.js')}`) const {SolutionDetails} = require(`${path.join(__dirname, 'SolutionDetails.js')}`) -const all_problems = require(`${path.join(__dirname, 'problemstat.json')}`); const {SOLUTION_LOCATION, COOKIE_VAL, INTERVAL, - URL} = require("./helper") + URL, + PROBLEMS_URL} = require("./helper") -if (COOKIE_VAL === null || COOKIE_VAL === undefined || COOKIE_VAL.length === 0) { - throw 'Set COOKIE_SECRET in repo secrets' -} +/*if (COOKIE_VAL === null || COOKIE_VAL === undefined || COOKIE_VAL.length === 0) { + throw 'Set COOKIE_SECRET in repo secrets' +}*/ const readFileDir = util.promisify(fs.readdir) +//global map so other function can access this let aldyPresentSol = {} -function mapFileWithId() { - return new Promise(async (resolve, reject) => { - const subPresent = await readFileDir(`${SOLUTION_LOCATION}`); - subPresent.map(val => { - if (val.indexOf("_") >= 0) { - let f = val.split("_")[0] - aldyPresentSol[f] = 1 - } - }) - return resolve(); + +const mapFileWithId = async (SOLUTION_LOCATION) => { + console.log(SOLUTION_LOCATION) + const subPresent = await readFileDir(SOLUTION_LOCATION); + subPresent.map(val => { + if (val.indexOf("_") >= 0) { + // TODO better algo than "_" check + let f = val.split("_")[0] + aldyPresentSol[f] = 1 + } }) } + SolutionDetails.prototype.IsPresent = function () { if (aldyPresentSol[this.id] === 1) { return true; @@ -64,7 +67,7 @@ worker.on('exit', () => { console.log("done this Work") }) -let solutionPromise = (question) => new Promise((resolve, reject) => { +const solutionPromiseToWorkerThrd = (question, COOKIE_VAL) => new Promise((resolve, reject) => { axios({ method: 'GET', baseURL: `${URL}${question.question__title_slug}`, @@ -79,6 +82,7 @@ let solutionPromise = (question) => new Promise((resolve, reject) => { .catch(err => { console.log("err", err.message) core.error('there is something wrong in here!') + reject(err) }) .then(() => { clearTimeout(sleep) @@ -88,11 +92,23 @@ let solutionPromise = (question) => new Promise((resolve, reject) => { const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); -async function OneTimeFetch() { +const prepareProblem = async (PROBLEMS_URL) => { + const returnProblems = await axios({ + method: 'GET', + baseURL: PROBLEMS_URL, + headers: { + cookie: COOKIE_VAL + } + }) + return returnProblems.data +} + +async function OneTimeFetch(PROBLEMS_URL, INTERVAL, prepareProblem, COOKIE_VAL) { try { + const all_problems = await prepareProblem(PROBLEMS_URL) for (let i = 0; i < all_problems.stat_status_pairs.length; i++) { const question = all_problems.stat_status_pairs[i] - await solutionPromise(question.stat) + await solutionPromiseToWorkerThrd(question.stat, COOKIE_VAL) await sleep(INTERVAL) } worker.postMessage({workerData: 'EXIT'}) @@ -103,7 +119,7 @@ async function OneTimeFetch() { } } -async function DailyFetch() { +async function DailyFetch(URL, COOKIE_VAL) { try { const r_recentSubmittedSols = await axios({ method: 'GET', @@ -138,12 +154,13 @@ const FileWriteHdl = async bVal => { ; (async () => { console.time() - await mapFileWithId() + console.log(process.cwd()) + await mapFileWithId(SOLUTION_LOCATION) if (Object.keys(aldyPresentSol).length >= 1) { - DailyFetch() + DailyFetch(URL, COOKIE_VAL) } else { - OneTimeFetch() + OneTimeFetch(PROBLEMS_URL, INTERVAL, prepareProblem, COOKIE_VAL) } })() diff --git a/src/helper.js b/src/helper.js index 5509493..02c06c1 100644 --- a/src/helper.js +++ b/src/helper.js @@ -10,5 +10,7 @@ exports.INTERVAL = 3000; exports.URL = "https://leetcode.com/api/submissions/" +exports.PROBLEMS_URL = "https://leetcode.com/api/problems/all" + exports.COOKIE_VAL = core.getInput('cookieVal')