8000 Init: Setup leetcode-api-client project · PrashanthaTP/leetcode-client@2e1cb2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e1cb2e

Browse files
committed
Init: Setup leetcode-api-client project
0 parents  commit 2e1cb2e

File tree

5 files changed

+297
-0
lines changed

5 files changed

+297
-0
lines changed

.gitignore

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
.env.json
2+
# Logs
3+
logs
4+
*.log
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
lerna-debug.log*
9+
.pnpm-debug.log*
10+
11+
# Diagnostic reports (https://nodejs.org/api/report.html)
12+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
13+
14+
# Runtime data
15+
pids
16+
*.pid
17+
*.seed
18+
*.pid.lock
19+
20+
# Directory for instrumented libs generated by jscoverage/JSCover
21+
lib-cov
22+
23+
# Coverage directory used by tools like istanbul
24+
coverage
25+
*.lcov
26+
27+
# nyc test coverage
28+
.nyc_output
29+
30+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
31+
.grunt
32+
33+
# Bower dependency directory (https://bower.io/)
34+
bower_components
35+
36+
# node-waf configuration
37+
.lock-wscript
38+
39+
# Compiled binary addons (https://nodejs.org/api/addons.html)
40+
build/Release
41+
42+
# Dependency directories
43+
node_modules/
44+
jspm_packages/
45+
46+
# Snowpack dependency directory (https://snowpack.dev/)
47+
web_modules/
48+
49+
# TypeScript cache
50+
*.tsbuildinfo
51+
52+
# Optional npm cache directory
53+
.npm
54+
55+
# Optional eslint cache
56+
.eslintcache
57+
58+
# Optional stylelint cache
59+
.stylelintcache
60+
61+
# Microbundle cache
62+
.rpt2_cache/
63+
.rts2_cache_cjs/
64+
.rts2_cache_es/
65+
.rts2_cache_umd/
66+
67+
# Optional REPL history
68+
.node_repl_history
69+
70+
# Output of 'npm pack'
71+
*.tgz
72+
73+
# Yarn Integrity file
74+
.yarn-integrity
75+
76+
# dotenv environment variable files
77+
.env
78+
.env.development.local
79+
.env.test.local
80+
.env.production.local
81+
.env.local
82+
83+
# parcel-bundler cache (https://parceljs.org/)
84+
.cache
85+
.parcel-cache
86+
87+
# Next.js build output
88+
.next
89+
out
90+
91+
# Nuxt.js build / generate output
92+
.nuxt
93+
dist
94+
95+
# Gatsby files
96+
.cache/
97+
# Comment in the public line in if your project uses Gatsby and not Next.js
98+
# https://nextjs.org/blog/next-9-1#public-directory-support
99+
# public
100+
101+
# vuepress build output
102+
.vuepress/dist
103+
104+
# vuepress v2.x temp and cache directory
105+
.temp
106+
.cache
107+
108+
# Docusaurus cache and generated files
109+
.docusaurus
110+
111+
# Serverless directories
112+
.serverless/
113+
114+
# FuseBox cache
115+
.fusebox/
116+
117+
# DynamoDB L F438 ocal files
118+
.dynamodb/
119+
120+
# TernJS port file
121+
.tern-port
122+
123+
# Stores VSCode versions used for testing VSCode extensions
124+
.vscode-test
125+
126+
# yarn v2
127+
.yarn/cache
128+
.yarn/unplugged
129+
.yarn/build-state.yml
130+
.yarn/install-state.gz
131+
.pnp.*

config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {readFielSync} from "fs"
2+
const configRaw = readFileSync( ".env.json" )
3+
export const config = JSON.parse( configRaw )
4+
export default config;

index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
<script src="./leetcode.js"></script>
9+
</head>
10+
<body>
11+
<h1>Working!!!</h1>
12+
</body>
13+
</html>
14+
index.html
15+

index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import config from "./config"
2+
const url = "https://leetcode.com/graphql";
3+
const body2 = `{"operationName":"getContestRankingData","variables":{"username":"aditi"},"query":"query getContestRankingData($username: String!) {\
4+
userContestRanking(username: aditi) {\
5+
attendedContestsCount\
6+
rating\
7+
globalRanking\
8+
}\
9+
}\
10+
"}`;
11+
const test = () => {
12+
fetch(url, {
13+
method: "POST",
14+
headers: {
15+
accept: "*/*",
16+
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8,hi;q=0.7,ru;q=0.6",
17+
"cache-control": "no-cache",
18+
"content-type": "application/json",
19+
pragma: "no-cache",
20+
"sec-ch-ua-mobile": "?0",
21+
"sec-ch-ua":
22+
'" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
23+
"sec-fetch-dest": "empty",
24+
"sec-fetch-mode": "cors",
25+
"sec-fetch-site": "same-origin",
26+
"sec-gpc": "1",
27+
},
28+
referrerPolicy: "strict-origin-when-cross-origin",
29+
mode: "cors",
30+
body: body2,
31+
})
32+
.then((res) => res.json())
33+
.then((data) => {
34+
console.table(data);
35+
});
36+
}
37+
38+
// Just some constants
39+
const LEETCODE_API_ENDPOINT = "https://leetcode.com/graphql";
40+
const DAILY_CODING_CHALLENGE_QUERY = `
41+
query questionOfToday {
42+
activeDailyCodingChallengeQuestion {
43+
date
44+
userStatus
45+
link
46+
question {
47+
acRate
48+
difficulty
49+
freqBar
50+
frontendQuestionId: questionFrontendId
51+
isFavor
52+
paidOnly: isPaidOnly
53+
status
54+
title
55+
titleSlug
56+
hasVideoSolution
57+
hasSolution
58+
topicTags {
59+
name
60+
id
61+
slug
62+
}
63+
}
64+
}
65+
}`;
66+
67+
// We can pass the JSON response as an object to our createTodoistTask later.
68+
const fetchDailyCodingChallenge = async () => {
69+
console.log(`Fetching daily coding challenge from LeetCode API.`);
70+
document.cookie = config.COOKIE
71+
console.log("Cookie Set");
72+
console.table(document.cookie);
73+
const init = {
74+
method: "POST",
75+
headers: { "Content-Type": "application/json" },
76+
body: JSON.stringify({ query: DAILY_CODING_CHALLENGE_QUERY }),
77+
referrer: "https://leetcode.com",
78+
referrerPolicy: "strict-origin-when-cross-origin",
79+
from: {
80+
csrfmiddlewaretoken:config.csrfmiddlewaretoken,
81+
login: "",
82+
password: "",
83+
},
84+
};
85+
86+
const response = await fetch(LEETCODE_API_ENDPOINT, init);
87+
return response.json();
88+
};
89+
90+
console.log(document.cookie);
91+
console.log(fetchDailyCodingChallenge());

leetcode.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const baseurl = "https://leetcode.com";
2+
3+
const urls = new Map();
4+
urls.set("login", "accounts/login");
5+
6+
const request1 = async (options) => {
7+
const init = {
8+
method: options.method || "GET",
9+
followRedirect: false,
10+
headers: {
11+
Cookie: options.setCookie
12+
? `LEETCODE_SESSION=${process.env.LEETCODE_SESSION};csrftoken=${process.env.CSRFTOKEN}`
13+
: "",
14+
"X-Requested-With": "XMLHttpRequest",
15+
"X-CSRFToken": options.setCookie ? process.env.CSRFTOKEN : "",
16+
Referer: options.referer || "",
17+
},
18+
resolveWithFullResponse: options.resolveWithFullResponse || false,
19+
form: options.form || null,
20+
};
21+
if (init.method !== "GET") {
22+
init.body = JSON.stringify(options.body) || "";
23+
}
24+
console.table(init)
25+
26+
return await fetch(options.url, init);
27+
};
28+
const HttpRequest = async (options) => {
29+
return await request({
30+
method: options.method || "GET",
31+
uri: options.url,
32+
followRedirect: false,
33+
headers: {
34+
Cookie: Helper.credit
35+
? `LEETCODE_SESSION=${Helper.credit.session};csrftoken=${Helper.credit.csrfToken}`
36+
: "",
37+
"X-Requested-With": "XMLHttpRequest",
38+
"X-CSRFToken": Helper.credit ? Helper.credit.csrfToken : "",
39+
Referer: options.referer || Helper.uris.base,
40+
},
41+
resolveWithFullResponse: options.resolveWithFullResponse || false,
42+
form: options.form || null,
43+
// body: JSON.stringify(options.body) || "",
44+
});
45+
};
46+
47+
const main = async() => {
48+
const res = await request1( {
49+
url: `${ baseurl }/${ urls.get( "login" ) }`,
50+
setCookie: true,
51+
resolveWithFullResponse: true,
52+
referer:baseurl
53+
} );
54+
console.log(res)
55+
};
56+
main()

0 commit comments

Comments
 (0)
0