8000 to github · ME-ON1/Leetcode-SyncInAction@42f2c29 · GitHub
[go: up one dir, main page]

Skip to content

Commit 42f2c29

Browse files
committed
to github
0 parents  commit 42f2c29

File tree

9 files changed

+45588
-0
lines changed

9 files changed

+45588
-0
lines changed

.github/src/SolutionDetails.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const fs = require("fs")
2+
3+
const util = require("util")
4+
5+
const readFileDir = util.promisify(fs.readdir)
6+
const codeWrite = util.promisify(fs.writeFile)
7+
8+
9+
function SolutionDetails({id, lang , runtime, memory, code, title_slug})
10+
{
11+
this.id = id
12+
this.lang = lang
13+
this.memory = memory
14+
this.runtime = runtime
15+
this.code = code
16+
this.title_slug = title_slug
17+
this.fmtData = ""
18+
this.ext = this._getExtension(this.lang)
19+
}
20+
21+
22+
SolutionDetails.prototype.fmtHdl = async function(){
23+
this.fmtData += `id = ${this.id} \n` ;
24+
this.fmtData += `lang = ${this.lang} \n`
25+
this.fmtData += `runtime = ${this.runtime} \n`
26+
this.fmtData += `memory = ${this.memory}\n`
27+
this.fmtData += `title_slug = ${this.title_slug}\n`
28+
this.fmtData += `code =\n\n ${this.code}`
29+
await this._fileWriteHdl()
30+
}
31+
32+
SolutionDetails.prototype._fileWriteHdl = async function() {
33+
try {
34+
await codeWrite(`../../${this.id}_${this.title_slug}.${this.ext}`,this.fmtData)
35+
console.log("file written")
36+
}
37+
catch(er)
38+
{
39+
console.log(er.message ,er )
40+
}
41+
}
42+
43+
SolutionDetails.prototype._getExtension = function(lang) {
44+
45+
switch(lang )
46+
{
47+
case 'cpp' : this.ext = "cxx"
48+
break;
49+
case 'javascript' : this.ext = "js"
50+
break;
51+
case 'python' : this.ext = "py"
52+
break ;
53+
case 'java' : this.ext = "java"
54+
break;
55+
case 'golang' : this.ext = "go"
56+
break;
57+
case 'rust' : this.ext = "rs"
58+
break ;
59+
case 'c' : this.ext = "c"
60+
break;
61+
case 'swift' : this.ext = "swift"
62+
break ;
63+
case 'c#' : this.ext = "cs"
64+
break ;
65+
case 'ruby' : this.ext = "rb"
66+
break ;
67+
case 'scala' : this.ext = "sc"
68+
break;
69+
case 'kotlin' : this.ext = "kt"
70+
break ;
71+
case 'typescript' : this.ext = 'ts'
72+
break;
73+
case 'php' : this.ext = 'php'
74+
break;
75+
case 'erlang' : this.ext = 'erl'
76+
break;
77+
case 'racket' : this.ext = 'rkt'
78+
break ;
79+
case 'elixir' : this.ext = 'ex'
80+
break;
81+
default : this.ext = "md"
82+
break ;
83+
}
84+
85+
return this.ext
86+
}
87+
88+
89+
module.exports = {
90+
SolutionDetails,
91+
}

.github/src/app.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
})

.github/src/fetchSolution.js

Whitespace-only changes.

.github/src/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "leetcodesync",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"axios": "^0.21.1",
14+
"dotenv": "^10.0.0"
15+
}
16+
}

0 commit comments

Comments
 (0)
0