diff --git a/moonsource-qidian.koplugin/moonsource_qidian/client.lua b/moonsource-qidian.koplugin/moonsource_qidian/client.lua index 877203a..46cfcc5 100644 --- a/moonsource-qidian.koplugin/moonsource_qidian/client.lua +++ b/moonsource-qidian.koplugin/moonsource_qidian/client.lua @@ -103,9 +103,13 @@ end ---@param path string ---@param cookie string|nil ---@param cb fun(html: string|nil, err: string|nil) +---@param soft boolean|nil 公开页面:带 Cookie 被 302/403 拦时不判登录失效,改用匿名再取一次 ---@return HttpJob -local function webGet(path, cookie, cb, cache_ttl) - return Request.get(WEB .. path, { +local webGet +function webGet(path, cookie, cb, cache_ttl, soft) + local job + local handle = { cancel = function() if job then job.cancel() end end } + job = Request.get(WEB .. path, { headers = headers(cookie), timeout = require("http.timeout").net(1), cache_ttl = cache_ttl, @@ -113,7 +117,11 @@ local function webGet(path, cookie, cb, cache_ttl) if not raw then local code = res and tonumber(res.code) if cookie and cookie ~= "" and (code == 302 or code == 403) then - cb(nil, loginGone(cookie or "")) + if soft then + job = webGet(path, nil, cb, cache_ttl, true) + else + cb(nil, loginGone(cookie or "")) + end else cb(nil, err and tostring(err) or _("请求失败")) end @@ -121,6 +129,7 @@ local function webGet(path, cookie, cb, cache_ttl) end cb(raw) end) + return handle end ---@param wire table|nil @@ -429,7 +438,7 @@ function Client.detailAsync(book_id, cb) end cb(info) - end) + end, nil, true) end ---@param book_id string diff --git a/moonsource-qidian.koplugin/moonsource_qidian/source.lua b/moonsource-qidian.koplugin/moonsource_qidian/source.lua index d45e7b8..8e81624 100644 --- a/moonsource-qidian.koplugin/moonsource_qidian/source.lua +++ b/moonsource-qidian.koplugin/moonsource_qidian/source.lua @@ -45,27 +45,46 @@ function Source:configured() end --- 书架行没有简介。本地没有简介或封面的书逐本补拉详情,拉不到就退回书架行。 +local MISSING_TTL = 7 * 24 * 3600 + +--- 起点页面返回 404(下架/删除)的书:一段时间内不再逐本重试,否则每次同步书架都白打一轮请求。 +local function missingSet() + local cfg = require("utils.settings").getSource("qidian") + cfg.detail_missing = type(cfg.detail_missing) == "table" and cfg.detail_missing or {} + return cfg.detail_missing +end + ---@param rows table[] ---@param cb fun(rows: table[]) ---@return { cancel: fun() } local function fillDetails(rows, cb) local BookDB = require("book.books_db") - local cancelled, job, index = false, nil, 0 + local cancelled, job, index, missingDirty = false, nil, 0, false local function nextRow() if cancelled then return end index = index + 1 local row = rows[index] - if not row then cb(rows); return end + if not row then + if missingDirty then require("utils.settings").saveSource("qidian") end + cb(rows) + return + end row.local_book = BookDB.get(Qidian.meta().id, tostring(row.bid)) local book = row.local_book local intro = book and book.intro local cover = book and book.cover if intro and intro ~= "" and cover and cover ~= "" then nextRow(); return end + local since = tonumber(missingSet()[tostring(row.bid)]) + if since and os.time() - since < MISSING_TTL then nextRow(); return end job = Client.detailAsync(row.bid, function(info, err) if cancelled then return end if info then row.info = info else + if tostring(err):find("404", 1, true) then + missingSet()[tostring(row.bid)] = os.time() + missingDirty = true + end require("utils.log").warn("book qidian detail failed", row.bid, err) end nextRow() diff --git a/tests/qidian/qidian_spec.lua b/tests/qidian/qidian_spec.lua index 689a513..9317337 100644 --- a/tests/qidian/qidian_spec.lua +++ b/tests/qidian/qidian_spec.lua @@ -349,3 +349,25 @@ do Assert.is_nil(got) Assert.eq(err, "起点书友圈响应无效") end + +-- 详情页是公开页面:带 Cookie 被 302/403 拦时不能判登录失效,改匿名再取一次。 +do + expired = nil + cfg.cookie = "ywkey=abc; _csrfToken=tok" + requests, replies = {}, { + { body = nil, err = "HTTP 403", res = { code = 403 } }, + { body = page({ bookInfo = { bookId = 9, bookName = "书" } }) }, + } + local info + Client.detailAsync("9", function(v) info = v end) + Assert.eq(#requests, 2) + Assert.is_true(requests[2].opts.headers.Cookie == nil, "重试不带账号 Cookie") + Assert.eq(info.bookName, "书") + Assert.is_nil(expired, "详情被拦不标登录失效") + -- 404(下架)原样报错,不当登录问题 + requests, replies = {}, { { body = nil, err = "HTTP 404", res = { code = 404 } } } + local err + Client.detailAsync("9", function(_, e) err = e end) + Assert.eq(err, "HTTP 404") + Assert.is_nil(expired) +end