feat(792-binary-search): add py3 solution
This commit is contained in:
55
.scripts/addnew
Executable file
55
.scripts/addnew
Executable file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env zx
|
||||
|
||||
import Turndown from "turndown";
|
||||
import { client, gql } from "./graphql.mjs";
|
||||
|
||||
const langExt = { python3: "py", nodejs: "js", cpp: "cpp" };
|
||||
|
||||
const link = await question("Enter leetcode problem link: ");
|
||||
const titleSlug = path.basename(new URL(link).pathname);
|
||||
|
||||
const language = await question("Enter language: ", {
|
||||
choices: Object.keys(langExt),
|
||||
});
|
||||
|
||||
const { question: q } = await client.request(
|
||||
gql`
|
||||
query questionData($titleSlug: String!) {
|
||||
question(titleSlug: $titleSlug) {
|
||||
questionId
|
||||
questionFrontendId
|
||||
boundTopicId
|
||||
title
|
||||
titleSlug
|
||||
content
|
||||
}
|
||||
}
|
||||
`,
|
||||
{
|
||||
titleSlug,
|
||||
}
|
||||
);
|
||||
|
||||
const questionIdPadded = q.questionId.padStart(4, "0");
|
||||
const questionDir = `${questionIdPadded}_${q.titleSlug}`;
|
||||
const solutionDir = `${questionDir}/${language}`;
|
||||
const solutionFilePath = `${solutionDir}/solution.${langExt[language]}`;
|
||||
|
||||
const td = new Turndown({});
|
||||
td.addRule("pre-code", {
|
||||
filter: "pre",
|
||||
replacement(content) {
|
||||
return "```\n" + content + "```";
|
||||
},
|
||||
});
|
||||
const mdBody = td.turndown(q.content);
|
||||
|
||||
await $`mkdir -p ${solutionDir}`;
|
||||
await $`touch ${solutionFilePath}`;
|
||||
|
||||
fs.writeFileSync(`${questionDir}/README.md`, mdBody, { encoding: "utf-8" });
|
||||
|
||||
// TODO: Wait for solution to be pasted and saved
|
||||
// TODO: If above is successful (prompt for a key), then git add . and git commit
|
||||
|
||||
console.log("Done!");
|
||||
23
.scripts/graphql.mjs
Normal file
23
.scripts/graphql.mjs
Normal file
@@ -0,0 +1,23 @@
|
||||
import { request, GraphQLClient, gql } from "graphql-request";
|
||||
|
||||
const customFetch = require("fetch-cookie")(fetch);
|
||||
export const client = new GraphQLClient("https://leetcode.com/graphql", {
|
||||
headers: {
|
||||
Referer: "https://leetcode.com",
|
||||
},
|
||||
fetch: customFetch,
|
||||
});
|
||||
|
||||
const lastFetch = await customFetch("https://leetcode.com", {
|
||||
redirect: "follow",
|
||||
headers: {
|
||||
Referer: "https://leetcode.com",
|
||||
},
|
||||
});
|
||||
|
||||
client.setHeader(
|
||||
"x-csrftoken",
|
||||
/csrftoken=([^;]+)/.exec(lastFetch.headers.get("set-cookie"))[1]
|
||||
);
|
||||
|
||||
export { gql };
|
||||
Reference in New Issue
Block a user