this script writes your leetcode progess to a gist
retrive a github token
create a gist and retrive the id
update .env file
npm install then node update.js
| LEETCODE_USER=leetcode username | |
| GITHUB_TOKEN=github token | |
| GIST_ID=gist id | |
| GIST_FILE=progress.txt |
this script writes your leetcode progess to a gist
retrive a github token
create a gist and retrive the id
update .env file
npm install then node update.js
| { | |
| "name": "leetcode-progress-gist", | |
| "version": "0.1.0", | |
| "description": "update my leetcode progress to gist", | |
| "main": "update.js", | |
| "keywords": [ | |
| "leetcode" | |
| ], | |
| "author": "Chuang Yu", | |
| "license": "MIT", | |
| "dependencies": { | |
| "axios": "^0.19.2", | |
| "cheerio": "^1.0.0-rc.3", | |
| "dotenv": "^8.2.0" | |
| } | |
| } |
| "use strict"; | |
| require("dotenv").config(); | |
| const axios = require("axios"); | |
| const cheerio = require("cheerio"); | |
| const USER_PAGE = `https://leetcode.com/${process.env.LEETCODE_USER}/`; | |
| const TOKEN = process.env.GITHUB_TOKEN; | |
| const GIST_ID = process.env.GIST_ID; | |
| const GIST_FILE = process.env.GIST_FILE; | |
| const API = `https://api.github.com/gists/${GIST_ID}`; | |
| const HEADERS = { Authorization: `token ${TOKEN}` }; | |
| const MAX_LEN = 56; | |
| main(); | |
| async function main() { | |
| const pageRes = await axios.get(USER_PAGE); | |
| const html = pageRes.data; | |
| const $ = cheerio.load(html); | |
| // extract info | |
| const panels = $("body .panel.panel-default"); | |
| const progressPanel = panels[2]; | |
| const mostRecentSubmissionsPanel = panels[7]; | |
| const listGroup = $("ul.list-group", progressPanel); | |
| const badges = $("li.list-group-item .badge", listGroup); | |
| const solvedQuestions = $(badges[0]) | |
| .text() | |
| .split("/"); | |
| const mostRecentSubmissionNode = $("ul a b", mostRecentSubmissionsPanel)[0]; | |
| const mostRecentSubmission = $(mostRecentSubmissionNode).text(); | |
| const info = { | |
| solved: solvedQuestions[0].trim(), | |
| total: solvedQuestions[1].trim(), | |
| latest: mostRecentSubmission | |
| }; | |
| const gistContent = []; | |
| const solved = info.solved + "/" + info.total; | |
| gistContent.push( | |
| "progress : ", | |
| genProgressBar(info.solved, info.total, MAX_LEN - 13), | |
| "\r\n" | |
| ); | |
| gistContent.push("solved : ", solved.padStart(MAX_LEN - 13), "\r\n"); | |
| gistContent.push("last solved: ", info.latest.padStart(MAX_LEN - 13)); | |
| // update gist | |
| const newGist = { files: {} }; | |
| newGist.files[GIST_FILE] = { content: gistContent.join("") }; | |
| const updateRes = await axios.patch(API, newGist, { headers: HEADERS }); | |
| if (updateRes.status === 200) console.log("Updated"); | |
| function genProgressBar(done, total, len = MAX_LEN) { | |
| const empty = "-"; | |
| const filled = "▇"; | |
| const numOfFilled = Math.floor(len * (done / total)); | |
| return ( | |
| new Array(numOfFilled).fill(filled).join("") + | |
| new Array(len - numOfFilled).fill(empty).join("") | |
| ); | |
| } | |
| } |
@seognil waka-box is using crontab but i don't think having one more configuration file for this is necessary.
if you want to schedule the update you can for example do 30 3 * * * node update.js (update at 3:30am everyday)
Cool stuff!
Any idea to make it automatic? like this https://github.com/matchai/waka-box
Or maybe an npm package as a commit hook for local leetcode coding.