seanprashad / leetcode-patterns

A pattern-based approach for learning technical interview questions
https://seanprashad.com/leetcode-patterns/
GNU General Public License v3.0
10.21k stars 1.77k forks source link

Integrate with leetcode to populate completed problems #155

Open veeral-patel opened 2 years ago

veeral-patel commented 2 years ago

Another idea is integrating with Leetcode's API, if one exists, to mark already completed problems on LC as done.

tobias-wilfert commented 2 years ago

The closest thing to an API Leetcode has is this: https://leetcode.com/api/problems/algorithms/ Which allows you to retrieve a JSON containing data about the problems (including if the logged-in user solved them).

In the meantime You can grab the names of the questions you solved and then run the following code in the console on the website, this will check the boxes of all the questions you have already solved on Leetcode:

var solved = ['Contains Duplicate' ... ];
var tr = document.getElementsByTagName("tr");

// Skip the first 2 rows as they are not question rows
for (var i = 2; i < tr.length; i++) {
    // Check if in solved
    if(solved.includes(tr[i].children[1].children[0].innerText) === true){
        tr[i].children[0].children[0].children[0].click(); // Simulate a toggle
    }
}
seanprashad commented 2 years ago

Relates to #179

seanprashad commented 2 years ago

With the introduction of #210, we should be able to accomplish this. I was thinking that we could retrieve all completed questions for a user and populate the checkboxes as such.

https://leetcode.com/discuss/general-discussion/1297705/is-there-public-api-endpoints-available-for-leetcode might have a few leads for us!

mlcivilengineer commented 1 year ago

The closest thing to an API Leetcode has is this: https://leetcode.com/api/problems/algorithms/ Which allows you to retrieve a JSON containing data about the problems (including if the logged-in user solved them).

In the meantime You can grab the names of the questions you solved and then run the following code in the console on the website, this will check the boxes of all the questions you have already solved on Leetcode:

var solved = ['Contains Duplicate' ... ];
var tr = document.getElementsByTagName("tr");

// Skip the first 2 rows as they are not question rows
for (var i = 2; i < tr.length; i++) {
    // Check if in solved
    if(solved.includes(tr[i].children[1].children[0].innerText) === true){
        tr[i].children[0].children[0].children[0].click(); // Simulate a toggle
    }
}

if you go to the site https://leetcode.com/api/problems/algorithms/ and paste the following code on the browser console while logged in on leetcode on another tab you can retrieve a list of all the names of the completed algorithms. Just copy the list and substitute the var solved with it and execute the code on the console and it will work :)

fetch('https://leetcode.com/api/problems/algorithms/')
  .then(response => response.json())
  .then(data => {
    const questionList = [];

    const statStatusPairs = data.stat_status_pairs;
    for (const pair of statStatusPairs) {
      if (pair.status === 'ac') {
        const questionTitle = pair.stat.question__title;
        questionList.push(questionTitle);
      }
    }

    console.log(questionList);
  })
  .catch(error => console.error('Error:', error));

I tried using the following code directly so I could skip copying the list and substituting the solved var, but was not able to make it work because of cors policy. As I am not a javascript dev nor a web developer, I gave up. If anyone knows how to circunvent the cors policy, feel free to modify the code:

function get_solved_questions() {
  return fetch('https://leetcode.com/api/problems/algorithms/')
    .then(response => response.json())
    .then(data => {
      const questionList = [];

      const statStatusPairs = data.stat_status_pairs;
      for (const pair of statStatusPairs) {
        if (pair.status === 'ac') {
          const questionTitle = pair.stat.question__title;
          questionList.push(questionTitle);
        }
      }

      return questionList;
    })
    .catch(error => {
      console.error('Error:', error);
      return [];
    });
}

var tr = document.getElementsByTagName("tr");

get_solved_questions()
  .then(solved => {
    // Skip the first 2 rows as they are not question rows
    for (var i = 2; i < tr.length; i++) {
      // Check if in solved
      if (solved.includes(tr[i].children[1].children[0].innerText)) {
        tr[i].children[0].children[0].children[0].click(); // Simulate a toggle
      }
    }
  });