A Neovim plugin enabling you to solve LeetCode problems.
| 1 | local utils = require("leetcode.api.utils") |
| 2 | local queries = require("leetcode.api.queries") |
| 3 | local config = require("leetcode.config") |
| 4 | local urls = require("leetcode.api.urls") |
| 5 | local Spinner = require("leetcode.logger.spinner") |
| 6 | |
| 7 | ---@class lc.ProblemsApi |
| 8 | local Problems = {} |
| 9 | |
| 10 | ---@param cb? function |
| 11 | ---@param noti? boolean |
| 12 | --- |
| 13 | ---@return lc.cache.Question[], lc.err |
| 14 | |
| 15 | local endpoint = urls.problems:format("algorithms") |
| 16 | |
| 17 | local spinner |
| 18 | if noti then |
| 19 | spinner = Spinner:start("updating problemlist cache", "points") |
| 20 | end |
| 21 | |
| 22 | if cb then |
| 23 | utils.get(endpoint, { |
| 24 | callback = function(res, err) |
| 25 | if err then |
| 26 | if spinner then |
| 27 | spinner:error(err.msg) |
| 28 | end |
| 29 | return cb(nil, err) |
| 30 | end |
| 31 | |
| 32 | local problems = utils.normalize_problems(res.stat_status_pairs) |
| 33 | |
| 34 | if config.is_cn then |
| 35 | if spinner then |
| 36 | spinner:update("fetching title translations") |
| 37 | end |
| 38 | Problems.translated_titles(function(titles, terr) |
| 39 | if terr then |
| 40 | if spinner then |
| 41 | spinner:error(terr.msg) |
| 42 | end |
| 43 | return cb(nil, terr) |
| 44 | end |
| 45 | |
| 46 | problems = utils.translate_titles(problems, titles) |
| 47 | if spinner then |
| 48 | spinner:success("cache updated") |
| 49 | end |
| 50 | |
| 51 | cb(problems) |
| 52 | end) |
| 53 | else |
| 54 | if spinner then |
| 55 | spinner:success("cache updated") |
| 56 | end |
| 57 | |
| 58 | cb(problems) |
| 59 | end |
| 60 | end, |
| 61 | }) |
| 62 | else |
| 63 | local res, err = utils.get(endpoint) |
| 64 | if err then |
| 65 | if spinner then |
| 66 | spinner:error(err.msg) |
| 67 | end |
| 68 | return nil, err |
| 69 | end |
| 70 | |
| 71 | local problems = utils.normalize_problems(res.stat_status_pairs) |
| 72 | |
| 73 | if config.is_cn then |
| 74 | local titles, terr = Problems.translated_titles() |
| 75 | if terr then |
| 76 | if spinner then |
| 77 | spinner:error(terr.msg) |
| 78 | end |
| 79 | return nil, terr |
| 80 | end |
| 81 | |
| 82 | if spinner then |
| 83 | spinner:success("problems cache updated") |
| 84 | end |
| 85 | return utils.translate_titles(problems, titles) |
| 86 | else |
| 87 | if spinner then |
| 88 | spinner:success("problems cache updated") |
| 89 | end |
| 90 | return problems |
| 91 | end |
| 92 | end |
| 93 | end |
| 94 | |
| 95 | |
| 96 | local query = queries.qot |
| 97 | |
| 98 | utils.query(query, {}, { |
| 99 | callback = function(res, err) |
| 100 | if err then |
| 101 | return cb(nil, err) |
| 102 | end |
| 103 | |
| 104 | local tday_record = res.data["todayRecord"] |
| 105 | local question = config.is_cn and tday_record[1].question or tday_record.question |
| 106 | cb(question) |
| 107 | end, |
| 108 | }) |
| 109 | end |
| 110 | |
| 111 | |
| 112 | local query = queries.translations |
| 113 | |
| 114 | if cb then |
| 115 | utils.query(query, {}, { |
| 116 | callback = function(res, err) |
| 117 | if err then |
| 118 | return cb(nil, err) |
| 119 | end |
| 120 | cb(res.data.translations) |
| 121 | end, |
| 122 | }) |
| 123 | else |
| 124 | local res, err = utils.query(query, {}) |
| 125 | if err then |
| 126 | return nil, err |
| 127 | end |
| 128 | return res.data.translations |
| 129 | end |
| 130 | end |
| 131 | |
| 132 | return Problems |