forked from aylei/leetcode-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchList.js
86 lines (76 loc) · 1.77 KB
/
FetchList.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// https://codetop.cc/api/questions/?page=${N}&search=&ordering=-frequency
let genUrl = (N) => {
return `https://codetop.cc/api/questions/?page=${N}&search=&ordering=-frequency`
}
let pageRange = {min: 1, max: 50}
let fetchPage = (N) => {
let url = genUrl(N)
console.log(`Fetching ${url}...`);
return fetch(url).then(r => r.json())
}
let fetchAllPages = (range) => {
let queryPeriod = 1000
let promises = []
for (let i = range.min; i <= range.max; i++) {
promises.push(fetchPage(i))
}
return Promise.all(promises)
}
let fetchAllQuestions = () => {
return fetchAllPages(pageRange).then(pages => {
let questions = []
for (let page of pages) {
console.log("Page: ", page);
questions = questions.concat(page.list)
}
return questions
})
}
/**
* Response format:
* {
* count,
* list {
* id,
* value,
* leetcode? {
* id,
* title
* }
* }
* }
*/
// Generate a list of {leet_id, value, title}
let genList = (questions) => {
let list = []
for (let q of questions) {
let leetcode = q.leetcode
if (leetcode) {
let id = leetcode.id
let title = leetcode.title
let value = q.value
list.push({id, value, title})
}
}
return list
}
// Sort the list by value
let sortList = (list) => {
return list.sort((a, b) => {
return b.value - a.value
})
}
// Print to console
let printList = (list) => {
let out = ""
for (let {id, value, title} of list) {
out += `${id},${value},${title}\n`
}
console.log(out)
}
// Main
fetchAllQuestions().then(questions => {
let list = genList(questions)
let sortedList = sortList(list)
printList(sortedList)
})