-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcodeScraper.js
58 lines (51 loc) · 1.91 KB
/
leetcodeScraper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const axios = require('axios');
// TODO: CLEAN THIS DAMN FILE IT IS UGLYYYYYYYYYYYYY MAKE IT A SINGLE FUNCTION
const ENDPOINT = "https://leetcode.com/api/problems/algorithms/"
const ENDPOINT_LINK = "https://leetcode.com/problems/"
const getRandomProblem = async (difficulty) => {
return await axios.get(ENDPOINT).then(resp => {
// extracting questions count and number of questions from total questions.
let questions = resp.data.stat_status_pairs
// check for only free questions.
questions.filter(question => question.paid_only == false)
// TODO: check whether question is solved or not
let questions_count = questions.length
let index,question;
do {
index = Math.floor(Math.random() * questions_count);
question = questions[index]
if(difficulty===-1) break;
} while(difficulty!=question.difficulty.level)
// choosing a random index from the lot
return {
id: question.stat.question_id,
title: question.stat.question__title,
link: `${ENDPOINT_LINK}${question.stat.question__title_slug}`,
difficulty: question.difficulty.level
}
})
.catch(err => {console.log(err);return null;})
}
const getRandomProblems = async (count, difficulties) => {
let i=0;
let output = [];
let questions = (await axios.get(ENDPOINT)).data.stat_status_pairs.filter(question => question.paid_only == false);
let questions_count = questions.length;
while (i!=count){
let index = Math.floor(Math.random() * questions_count);
let question = questions[index]
if (difficulties.length > i && question.difficulty.level != difficulties[i]) continue;
output.push({
id: question.stat.question_id,
title: question.stat.question__title,
link: `${ENDPOINT_LINK}${question.stat.question__title_slug}`,
difficulty: question.difficulty.level
})
i+=1;
}
return output;
}
module.exports = {
getRandomProblem,
getRandomProblems
};