Last active
July 3, 2022 13:37
-
-
Save DET171/80c54d3bc688a2c2b92daebbe9534340 to your computer and use it in GitHub Desktop.
For checking who to deport (GH Gist edition)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import got from 'got'; | |
import { JSDOM } from 'jsdom'; | |
import chalk from 'chalk'; | |
import table from 'chalk-table'; | |
import { usernames, problems } from './toFetch.js'; | |
const frames = [ | |
'⠋', | |
'⠙', | |
'⠹', | |
'⠸', | |
'⠼', | |
'⠴', | |
'⠦', | |
'⠧', | |
'⠇', | |
'⠏', | |
]; | |
const ESC = '\x1b'; // ASCII escape character | |
const CSI = ESC + '['; // control sequence introducer | |
const clearLastLine = () => { | |
process.stdout.write(CSI + 'A'); // moves cursor up one line | |
process.stdout.write(CSI + 'K'); // clears from cursor to line end | |
}; | |
const totalResults = usernames.length * problems.length; | |
let currentResult = 0; | |
let res = []; | |
process.stdout.write(chalk.blue(`${frames[0]} Fetching scores (0/${totalResults})...`)); | |
for (const username of usernames) { | |
for (const problem of problems) { | |
const body = (await got(`https://codebreaker.xyz/submissions?username=${username}&problem=${problem}`)).body; | |
const { document } = new JSDOM(body).window; | |
let scores = document.querySelectorAll('.pb-1.text-white.badge'); | |
scores = [...scores]; | |
// never do qn smh | |
if (scores.length === 0) { | |
res.push(`${username} ${problem} ${chalk.red.bold('0 UNATTEMPTED')}`); | |
} | |
// if there are multiple scores, take the highest | |
scores = scores.map(score => parseInt(score.textContent, 10)); | |
scores.sort((a, b) => b - a); | |
const score = scores[0]; | |
if (score === 100) { | |
res.push(`${username} ${problem} ${chalk.green.bold('100 COMPLETED')}`); | |
} | |
// maximum sadgery | |
else if (score === 0) { | |
res.push(`${username} ${problem} ${chalk.red.bold('0')} ${chalk.yellow.bold('ATTEMPTED')}`); | |
} | |
else if (score > 0 && score < 100) { | |
res.push(`${username} ${problem} ${chalk.yellow.bold(score)} ${chalk.yellow.bold('ATTEMPTED')}`); | |
} | |
currentResult++; | |
process.stdout.write(chalk.blue(`\r${frames[currentResult % frames.length]} Fetching scores (${currentResult}/${totalResults})...`)); | |
} | |
} | |
clearLastLine(); | |
res = res.map((str) => { | |
const [username, problem, score, status] = str.split(' '); | |
return { username, problem, score, status }; | |
}); | |
const resTable = table({ | |
leftPad: 2, | |
columns: [ | |
{ | |
field: 'username', | |
name: chalk.blue.bold('Username'), | |
}, | |
{ | |
field: 'problem', | |
name: chalk.blue.bold('Problem'), | |
}, | |
{ | |
field: 'score', | |
name: chalk.blue.bold('Score'), | |
}, | |
{ | |
field: 'status', | |
name: chalk.yellow.bold('Status'), | |
}, | |
], | |
}, [ | |
...res, | |
]); | |
console.log('\n' + resTable); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const usernames = [ | |
'DSHI', | |
'Tyx2019', | |
'acertainsomeone', | |
]; | |
export const problems = [ | |
'lis_easy', | |
'hamster1', | |
'bracketex', | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment