본문 바로가기

Data Structure & Algorithm/Baekjoon Review

[Programmers] 백준허브 이미 풀었던 문제 등록 자동화

반응형

백준허브에 프로그래머스에서 이미 풀었던 문제를 올리기 까다로워서,

 

자동화 프로그램을 만들어보았다.

 

사용법은 다음과 같다.

1. 프로그래머스 로그인

2. 상단 메뉴 [코딩테스트] 클릭

3. 필터링에서 "상태"에 "푼 문제" 체크

4. 개발자도구 열기 (윈도우는 F12, 맥북은 cmd + shift + i)

5. [콘솔] 탭 선택

6. 아래 코드 붙여넣고 엔터

7. 다될때까지 기다리기

 

async function waitForPageLoad(newPage) {
    return new Promise((resolve) => {
        const interval = setInterval(() => {
            if (newPage.document.readyState === 'complete') {
                clearInterval(interval);
                resolve();
            }
        }, 200);
    });
}

async function waitForProblemSolve(problem) {
    let title = problem.document.querySelector('.challenge-title').textContent;
    return new Promise((resolve) => {
        const interval = setInterval(() => {
            const progress = problem.document.getElementById('BaekjoonHub_progress_elem');
            if (progress && progress.className === 'markuploaded') {
                console.log(`${title} 해결!`);
                problem.close();
                clearInterval(interval);
                resolve();
            }
        }, 200);
    });
}

let nextPageExist = true;
let curPage = 0;
while (nextPageExist) {
    curPage += 1;
    let newPage = window.open(`https://school.programmers.co.kr/learn/challenges?order=recent&statuses=solved&page=${curPage}`);
    await waitForPageLoad(newPage);
    let nextButton = newPage.document.querySelector('button.next');
    if (nextButton === null) {
        console.log('다음 버튼이 없습니다.. 종료합니다..');
        nextPageExist = false;
    } else {
        nextPageExist = !nextButton.disabled;
    }
    const solvedProblems = newPage.document.querySelectorAll('.solved');
    console.log(solvedProblems);
    for (problem of solvedProblems) {
        const aTag = problem.nextElementSibling.querySelector('a'); 
        const hrefValue = aTag ? aTag.getAttribute('href') : null;
        const newProblem = window.open(hrefValue);
        await waitForPageLoad(newProblem);
        const button = newProblem.document.getElementById('submit-code');
        if (button) {
            button.click();
            await waitForProblemSolve(newProblem);
        }
    }
    newPage.close();
}
console.log("추가 완료");

 

원리는 간단하다.

1페이지부터 마지막 페이지까지 쭉 이동하면서 푼 문제들 들어가서 실행하는것.

 

혹시나 중간에 멈춘듯한 모습을 보이면 제일 처음 들어갔던 탭을 눌러보자.

 

그럼에도 안된다면, 가장 마지막까지 진행한 페이지를 curPage로 설정하고 다시 실행하기...ㅜ

이런식으로 실행하자.

 

programmers.js
0.00MB

반응형