forked from yuria-n/leetcode-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (157 loc) · 4.39 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const dotenv = require("dotenv");
const { fetchLikeAxios } = require("./fetchLikeAxios");
dotenv.config();
const LEETCODE_BASE_URL = "https://leetcode.com/";
const LEETCODE_ALL_QUESTION_URL = `${LEETCODE_BASE_URL}api/problems/all/`;
const LEETCODE_RECOMMENDED_LIST_URL = `${LEETCODE_BASE_URL}list/api/get_list/xo2bgr0r/`;
// difficulty defaults to "Medium"
const { SLACK_WEBHOOK_URL, DIFFICULTY = 2 } = process.env;
// the response returns difficulty as levels 1(easy), 2(medium), 3(hard)
const DIFFICULTIES = ["", "Easy", "Medium", "Hard"];
// Main function
(async () => {
try {
/** @type {RecommendedResponse} */
const { questions } = await getData(LEETCODE_RECOMMENDED_LIST_URL);
const ids = generateListIds(questions);
/** @type {AllResponse} */
const allData = await getData(LEETCODE_ALL_QUESTION_URL);
const freeQuestions = getFreeQuestions(allData);
// we have to match the IDs from the previous API to ones from the "get all" which contains the
// actual question information (links, difficuly, etc)
const recommended = getListQuestions(allData, ids);
const { difficulty: d, stat } = pickQuestion([...freeQuestions, ...recommended]);
const text = formatText(
stat.frontend_question_id,
stat.question__title,
stat.question__title_slug,
DIFFICULTIES[d.level]
);
postQuestion(text);
} catch (error) {
console.log(error);
}
})();
/**
*
* @param {string} url
* @template A
* @returns {Promise<A>}
*/
async function getData(url) {
const response = await fetchLikeAxios(url);
const data = await response.json();
return data;
}
/**
*
* @param {AllResponse} data
* @returns {StatPair[]}
*/
function getFreeQuestions(data) {
return data.stat_status_pairs.filter(
({ difficulty, paid_only }) => difficulty.level <= DIFFICULTY && !paid_only
);
}
/**
*
* @param {Question[]} data
* @returns {Set<number>}
*/
function generateListIds(data) {
const ids = new Set();
data.forEach(({ id }) => {
ids.add(id);
});
return ids;
}
/**
*
* @param {AllResponse} data
* @param {Set<number>} ids
* @returns {StatPair[]}
*/
function getListQuestions(data, ids) {
return data.stat_status_pairs.filter(({ stat }) => ids.has(stat.frontend_question_id));
}
// Pick a qestion to post
/**
*
* @param {StatPair[]} data
* @returns {StatPair}
*/
function pickQuestion(data) {
const i = Math.floor(Math.random() * data.length);
return data[i];
}
/**
*
* @param {number} num
* @param {string} title
* @param {string} dir
* @param {string} difficulty
* @returns
*/
function formatText(num, title, dir, difficulty) {
const link = `${LEETCODE_BASE_URL}problems/${dir}/`;
return `${num}. ${title} - ${difficulty}\n${link}`;
}
/**
* Post the generated message to Slack
* @param {string} text
*/
async function postQuestion(text) {
if (SLACK_WEBHOOK_URL) {
const response = await fetchLikeAxios(SLACK_WEBHOOK_URL, {
method: "post",
body: JSON.stringify({ text }),
});
console.info(response.status, text);
} else {
console.error("the SLACK_WEBHOOK_URL environment is not set ");
}
}
/**
* @typedef {object} RecommendedResponse
* @property {string} id_hash
* @property {string} name
* @property {string} description
* @property {Question[]} questions
* @property {boolean} is_public_favorite
* @property {number} view_count
* @property {string} creator
* @property {string} current_user
* @property {boolean} is_watched
*
*/
/**
* @typedef {object} Question
* @property {number} id
* @property {string} title
* @property {string} title_slug
*/
/**
* @typedef {object} AllResponse
* @property {Stat[]} stat_status_pairs
*/
/**
* @typedef {object} StatPair
* @property {object} stat
* @property {number} stat.question_id
* @property {boolean} stat.question__article__live
* @property {string} stat.question__article__slug
* @property {boolean} stat.question__article__has_video_solution
* @property {string} stat.question__title
* @property {string} stat.question__title_slug
* @property {boolean} stat.question__hide
* @property {number} stat.total_acs
* @property {number} stat.total_submitted
* @property {number} stat.frontend_question_id
* @property {boolean} stat.is_new_question
* @property {object} difficulty
* @property { number } difficulty.level
* @property {boolean} paid_only
* @property {boolean} is_favor
* @property {number} frequency
* @property {number} progress
*/