-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathproblems.lua
More file actions
163 lines (139 loc) · 4.41 KB
/
Copy pathproblems.lua
File metadata and controls
163 lines (139 loc) · 4.41 KB
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
local utils = require("leetcode.api.utils")
local queries = require("leetcode.api.queries")
local config = require("leetcode.config")
local urls = require("leetcode.api.urls")
local Spinner = require("leetcode.logger.spinner")
local log = require("leetcode.logger")
---@class lc.ProblemsApi
local Problems = {}
---@param cb? function
---@param noti? boolean
---
---@return lc.cache.Question[], lc.err
function Problems.all(cb, noti)
local endpoint = urls.problems:format("algorithms")
local spinner
if noti then
spinner = Spinner:init("updating problemlist cache...", "points")
end
if cb then
utils.get(endpoint, {
callback = function(res, err)
if err then
if spinner then
spinner:stop(err.msg, false)
end
return cb(nil, err)
end
local problems = utils.normalize_problems(res.stat_status_pairs)
if config.is_cn then
if spinner then
spinner:update("fetching title translations")
end
Problems.translated_titles(function(titles, terr)
if terr then
if spinner then
spinner:stop(terr.msg, false)
end
return cb(nil, terr)
end
problems = utils.translate_titles(problems, titles)
if spinner then
spinner:stop("cache updated")
end
cb(problems)
end)
else
if spinner then
spinner:stop("cache updated")
end
cb(problems)
end
end,
})
else
local res, err = utils.get(endpoint)
if err then
if spinner then
spinner:stop(err.msg, false)
end
return nil, err
end
local problems = utils.normalize_problems(res.stat_status_pairs)
if config.is_cn then
local titles, terr = Problems.translated_titles()
if terr then
if spinner then
spinner:stop(terr.msg, false)
end
return nil, terr
end
if spinner then
spinner:stop("problems cache updated")
end
return utils.translate_titles(problems, titles)
else
if spinner then
spinner:stop("problems cache updated")
end
return problems
end
end
end
function Problems.question_of_today(cb)
local query = queries.qot
utils.query(query, {}, {
callback = function(res, err)
if err then
return cb(nil, err)
end
local tday_record = res.data["todayRecord"]
local question = config.is_cn and tday_record[1].question or tday_record.question
cb(question)
end,
})
end
function Problems.favorite_list(cb)
local query = queries.favorite_list
utils.query(query, {}, {
callback = function(res, err)
if err then
return cb(nil, err)
end
local data = res.data
cb(data.myCollectedFavoriteList.favorites)
end,
})
end
function Problems.favorite_question_list(favorite_slug, cb)
local query = queries.favorite_question_list
utils.query(query, { favoriteSlug = favorite_slug }, {
callback = function(res, err)
if err then
return cb(nil, err)
end
local normalized = utils.normalize_favorites(res.data.favoriteQuestionList.questions)
cb(normalized)
end,
})
end
function Problems.translated_titles(cb)
local query = queries.translations
if cb then
utils.query(query, {}, {
callback = function(res, err)
if err then
return cb(nil, err)
end
cb(res.data.translations)
end,
})
else
local res, err = utils.query(query, {})
if err then
return nil, err
end
return res.data.translations
end
end
return Problems