Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ME-ON1 committed Nov 12, 2021
1 parent ffdc94f commit 25f4265
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`,
Expand All @@ -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)
Expand All @@ -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'})
Expand All @@ -103,7 +119,7 @@ async function OneTimeFetch() {
}
}

async function DailyFetch() {
async function DailyFetch(URL, COOKIE_VAL) {
try {
const r_recentSubmittedSols = await axios({
method: 'GET',
Expand Down Expand Up @@ -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)
}
})()

Expand Down
2 changes: 2 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 25f4265

Please sign in to comment.