每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。 Co-authored-by: Copilot <[email protected]>
394 lines
16 KiB
Lua
394 lines
16 KiB
Lua
--[[--
|
|
起点中文网数据源。
|
|
|
|
详情 / 目录 / 章节走手机站页面(client.lua)。扫码、短信与浏览器 Cookie 经统一校验保存(setting.lua);
|
|
登录后书架双向同步(source.shelf):拉取、本地加入上行、本地删除上行。
|
|
进度只到章:开书从书架行的 readCid 拉,关书经 updReadStatus 推。笔记与统计仅本地记录。
|
|
VIP 章未购买时打开一页临时提示,目录标注已购 / 未购(打开过才知道)。
|
|
|
|
@module moonsource_qidian
|
|
--]]
|
|
|
|
local Browse = require("source.browse")
|
|
local Client = require("moonsource_qidian.client")
|
|
local Mapper = require("moonsource_qidian.mapper")
|
|
local ReviewCache = require("book.review_cache")
|
|
local Shelf = require("source.shelf")
|
|
local SourceBase = require("source.base")
|
|
local Toc = require("source.toc")
|
|
local _ = require("l10n").get("moonsource_qidian")
|
|
|
|
local Qidian = {}
|
|
|
|
-- 章节页不小,预取与整本缓存逐章间隔请求。
|
|
local FETCH_INTERVAL = 1
|
|
|
|
---@return BookSourceMeta
|
|
function Qidian.meta()
|
|
return require("moonsource_qidian.loadmeta")()
|
|
end
|
|
|
|
---@class QidianSource : SourceBase
|
|
---@field category_info table[]|nil 官网男频分类目录
|
|
local Source = setmetatable({ shelf_add = true }, { __index = SourceBase })
|
|
Source.__index = Source
|
|
|
|
---@return QidianSource
|
|
function Qidian.new()
|
|
local meta = Qidian.meta()
|
|
return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, Source)
|
|
end
|
|
|
|
---@return boolean
|
|
function Source:configured()
|
|
return Client.cookie() ~= nil
|
|
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 function nextRow()
|
|
if cancelled then return end
|
|
index = index + 1
|
|
local row = rows[index]
|
|
if not row then 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
|
|
job = Client.detailAsync(row.bid, function(info, err)
|
|
if cancelled then return end
|
|
if info then
|
|
row.info = info
|
|
else
|
|
require("utils.log").warn("book qidian detail failed", row.bid, err)
|
|
end
|
|
nextRow()
|
|
end)
|
|
end
|
|
nextRow()
|
|
return { cancel = function()
|
|
cancelled = true
|
|
if job then job.cancel() end
|
|
end }
|
|
end
|
|
|
|
--- 书架远端:Shelf.syncAsync 要的三个冒号方法。
|
|
local Remote = {}
|
|
|
|
function Remote:shelfSyncAsync(cb)
|
|
local cancelled, job, fill = false, nil, nil
|
|
job = Client.shelfAsync(Client.cookie(), function(rows, err)
|
|
if cancelled then return end
|
|
if not rows then cb(nil, err); return end
|
|
fill = fillDetails(rows, cb)
|
|
end)
|
|
return { cancel = function()
|
|
cancelled = true
|
|
job.cancel()
|
|
if fill then fill.cancel() end
|
|
end }
|
|
end
|
|
|
|
function Remote:addToShelfAsync(stable_id, cb)
|
|
return Client.addAsync(stable_id, cb)
|
|
end
|
|
|
|
function Remote:removeFromShelfAsync(stable_id, cb)
|
|
return Client.removeAsync(stable_id, cb)
|
|
end
|
|
|
|
function Source:syncBooksAsync(opts, cb)
|
|
if not self:configured() then return SourceBase.skipped(cb, "qidian not logged in") end
|
|
return Shelf.syncAsync(self.id, Remote, Mapper.shelfList, opts, cb)
|
|
end
|
|
|
|
--- 书架行的 readCid 才是用户看到的进度。getReadStatus 在查询章等于已读章时会把章号省掉。
|
|
function Source:getProgressAsync(identity, cb)
|
|
if not self:configured() then return SourceBase.defer(cb, nil, _("未登录起点")) end
|
|
local cancelled, job = false, nil
|
|
job = Client.shelfAsync(Client.cookie(), function(rows, err)
|
|
if cancelled then return end
|
|
if not rows then cb(nil, err); return end
|
|
local row
|
|
for _idx, item in ipairs(rows) do
|
|
if tostring(item.bid) == tostring(identity.stable_id) then row = item; break end
|
|
end
|
|
local read = Mapper.readChapter(row)
|
|
if not read then cb(nil, nil, { empty = true }); return end
|
|
job = self:loadTocAsync(identity, function(toc, reason)
|
|
if cancelled then return end
|
|
if not toc or #toc == 0 then cb(nil, reason or _("起点目录为空")); return end
|
|
local idx = Toc.index(self.id, identity.stable_id, read.uid)
|
|
if not idx then cb(nil, nil, { empty = true }); return end
|
|
cb({
|
|
chapter_idx = idx,
|
|
chapter_title = read.title,
|
|
fraction = (idx - 1) / #toc,
|
|
extra = { chapter_uid = read.uid, chapter_idx = idx },
|
|
})
|
|
end)
|
|
end)
|
|
return { cancel = function()
|
|
cancelled = true
|
|
if job and job.cancel then job.cancel() end
|
|
end }
|
|
end
|
|
|
|
function Source:putProgressAsync(identity, pos, cb)
|
|
if not self:configured() then return SourceBase.defer(cb, nil, _("未登录起点")) end
|
|
local cancelled, job = false, nil
|
|
local function send(chapter)
|
|
if not chapter.updated then
|
|
cb(nil, _("起点章节时间缺失"))
|
|
return
|
|
end
|
|
job = Client.updateReadAsync(identity.stable_id, chapter.uid, chapter.title, chapter.vip, chapter.updated,
|
|
function(ok, err)
|
|
if cancelled then return end
|
|
cb(ok and true or nil, err)
|
|
end)
|
|
end
|
|
job = self:loadTocAsync(identity, function(toc, err)
|
|
if cancelled then return end
|
|
if not toc then cb(nil, err); return end
|
|
local idx = tonumber(pos.chapter_idx) or tonumber(identity.chapter_idx)
|
|
local chapter = idx and toc[idx]
|
|
if not chapter or not chapter.uid then
|
|
cb(nil, _("缺少起点章节位置"))
|
|
return
|
|
end
|
|
if chapter.updated then send(chapter); return end
|
|
-- 旧目录缓存没有章节更新时间。不带时间上传会把阅读状态写坏,先补拉目录。
|
|
job = Client.catalogAsync(identity.stable_id, function(data, reason)
|
|
if cancelled then return end
|
|
local fresh = data and Mapper.toc(data)
|
|
if fresh and #fresh > 0 then Toc.put(self.id, identity.stable_id, fresh) end
|
|
local hit
|
|
for _ch, item in ipairs(fresh or {}) do
|
|
if item.uid == chapter.uid then hit = item; break end
|
|
end
|
|
if not hit then cb(nil, reason or _("缺少起点章节位置")); return end
|
|
if not hit.updated then cb(nil, _("起点章节时间缺失")); return end
|
|
send(hit)
|
|
end)
|
|
end)
|
|
return { cancel = function()
|
|
cancelled = true
|
|
if job and job.cancel then job.cancel() end
|
|
end }
|
|
end
|
|
|
|
--- 书城搜索:回包形状同 listStoreAsync。
|
|
---@param keyword string
|
|
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
|
|
---@return { cancel: fun() }|nil
|
|
function Source:searchAsync(keyword, cb)
|
|
return Browse.pages(function(page, done)
|
|
return Client.searchAsync(keyword, function(data, err)
|
|
if not data then done(nil, err); return end
|
|
local info = data.bookInfo
|
|
done(Mapper.search(data), nil, info.isLast ~= 1 and info.isLast ~= true)
|
|
end, page)
|
|
end, 200, {}, cb)
|
|
end
|
|
|
|
local RANKS = {
|
|
{ key = "yuepiao", title = _("月票榜") },
|
|
{ key = "hotsales", title = _("畅销榜") },
|
|
{ key = "readindex", title = _("阅读指数榜") },
|
|
{ key = "newfans", title = _("新人榜") },
|
|
{ key = "update", title = _("更新榜") },
|
|
{ key = "newbook", title = _("新书榜") },
|
|
{ key = "newauthor", title = _("新作者榜") },
|
|
}
|
|
local CATS = {
|
|
{ id = "-1", title = _("全站") }, { id = "21", title = _("玄幻") }, { id = "1", title = _("奇幻") },
|
|
{ id = "2", title = _("武侠") }, { id = "22", title = _("仙侠") }, { id = "4", title = _("都市") },
|
|
{ id = "15", title = _("现实") }, { id = "6", title = _("军事") }, { id = "5", title = _("历史") },
|
|
{ id = "7", title = _("游戏") }, { id = "8", title = _("体育") }, { id = "9", title = _("科幻") },
|
|
{ id = "10", title = _("悬疑灵异") }, { id = "20109", title = _("诸天无限") }, { id = "12", title = _("轻小说") },
|
|
}
|
|
|
|
--- 默认分类书库;兼容「榜单键:分类号」旧节点,分类和榜单均聚合后续页。
|
|
---@param opts { node?: string, limit?: integer }
|
|
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
|
|
---@return { cancel: fun() }
|
|
function Source:browseAsync(opts, cb)
|
|
if not opts.node or opts.node:sub(1, 9) == "category?" then
|
|
return require("moonsource_qidian.browse").run(self, opts, cb)
|
|
end
|
|
local key, cat = (opts.node or RANKS[1].key .. ":-1"):match("^(%w+):(-?%d+)$")
|
|
if not key then return SourceBase.defer(cb, nil, _("起点分类条件无效")) end
|
|
local ranks, cats = {}, {}
|
|
for _idx, rank in ipairs(RANKS) do ranks[#ranks + 1] = { id = rank.key .. ":" .. cat, title = rank.title } end
|
|
for _idx, row in ipairs(CATS) do cats[#cats + 1] = { id = key .. ":" .. row.id, title = row.title } end
|
|
local node = key .. ":" .. cat
|
|
table.insert(ranks, 1, { id = "category?catId=21&gender=male", title = _("分类书库") })
|
|
local groups = { Browse.group(ranks, node), Browse.group(cats, node) }
|
|
return Browse.pages(function(page, done)
|
|
return Client.rankPageAsync(key, cat, page, function(data, err)
|
|
if not data then done(nil, err); return end
|
|
done(Mapper.rank(data), nil, data.isLast ~= 1 and data.isLast ~= true)
|
|
end)
|
|
end, opts.limit or 60, groups, cb)
|
|
end
|
|
|
|
function Source:getDetailAsync(identity, cb)
|
|
return SourceBase.defer(cb, identity.book or require("book.books_db").get(self.id, identity.stable_id))
|
|
end
|
|
|
|
---@param identity BookIdentity
|
|
---@param cb fun(page: table|nil, err: string|nil)
|
|
---@return HttpJob
|
|
function Source:bookReviewsAsync(identity, cb)
|
|
if not require("ui/network/manager"):isConnected() then
|
|
return SourceBase.defer(cb, nil, _("网络不可用,请先连接 Wi-Fi"))
|
|
end
|
|
return Client.bookCircleAsync(identity.stable_id, function(data, err)
|
|
if not data then cb(nil, err); return end
|
|
cb(require("moonsource_qidian.reviews").bookPage(data))
|
|
end)
|
|
end
|
|
|
|
--- 强制拉目录并写回;失败由调用方继续用旧缓存。
|
|
---@param identity BookIdentity
|
|
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
|
---@return { cancel: fun() }
|
|
local function fetchTocAsync(self, identity, cb)
|
|
local cancelled, job = false, nil
|
|
job = Client.catalogAsync(identity.stable_id, function(data, err)
|
|
if cancelled then return end
|
|
local toc = data and Mapper.toc(data)
|
|
if not toc then cb(nil, err); return end
|
|
if #toc == 0 then cb(nil, _("起点目录为空")); return end
|
|
if not Toc.put(self.id, identity.stable_id, toc) then cb(nil, _("章节目录保存失败")); return end
|
|
cb(toc)
|
|
end)
|
|
return { cancel = function()
|
|
cancelled = true
|
|
job.cancel()
|
|
end }
|
|
end
|
|
|
|
---@param identity BookIdentity
|
|
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
|
---@param force boolean|nil 真=跳过缓存强制拉
|
|
---@return { cancel: fun() }
|
|
function Source:loadTocAsync(identity, cb, force)
|
|
if not force then
|
|
local cached = Toc.read(self.id, identity.stable_id)
|
|
if cached and #cached > 0 then
|
|
return SourceBase.defer(cb, cached)
|
|
end
|
|
end
|
|
return fetchTocAsync(self, identity, cb)
|
|
end
|
|
|
|
Source.refreshTocAsync = fetchTocAsync
|
|
|
|
function Source:openBookAsync(identity, opts, cb)
|
|
local Chapter = require("source.chapter")
|
|
local open = opts and opts.chapter_idx and Chapter.openAsync or Chapter.openWithUi
|
|
local locate = require("book.review_locate")
|
|
local reviews = self:paragraphReviewsEnabled()
|
|
return open(self, identity, identity.book, opts, {
|
|
loadToc = function(ref, done) return self:loadTocAsync(ref, done) end,
|
|
fetchContent = Chapter.vipFetcher(self, Client.contentAsync, true, reviews and locate.prepareChapterEnd or nil),
|
|
reuse = reviews and locate.reuseChapterEnd or nil,
|
|
}, cb)
|
|
end
|
|
|
|
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
|
local Chapter = require("source.chapter")
|
|
local locate = require("book.review_locate")
|
|
local reviews = self:paragraphReviewsEnabled()
|
|
return Chapter.prefetchAsync(identity, identity.book, toc, from_idx, count, {
|
|
fetchContent = Chapter.vipFetcher(self, Client.contentAsync, false, reviews and locate.prepareChapterEnd or nil),
|
|
reuse = reviews and locate.reuseChapterEnd or nil,
|
|
interval_seconds = FETCH_INTERVAL,
|
|
}, cb)
|
|
end
|
|
|
|
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
|
|
local Chapter = require("source.chapter")
|
|
local prepare = self:paragraphReviewsEnabled() and require("book.review_locate").prepareChapterEnd or nil
|
|
return Chapter.cacheAllAsync(self, identity, Chapter.vipFetcher(self, Client.contentAsync, false, prepare), on_progress, cb,
|
|
FETCH_INTERVAL, range)
|
|
end
|
|
|
|
--- 阅读页是否显示他人段评(设置页开关,默认关)。
|
|
---@return boolean
|
|
function Source:paragraphReviewsEnabled()
|
|
return require("source.review_setting").enabled(self.id)
|
|
end
|
|
|
|
--- 本章有评论的段落(未定位)。
|
|
---@param identity BookIdentity
|
|
---@param cb fun(hotspots: QidianReviewHotspot[]|nil, err: string|nil)
|
|
---@return { cancel: fun() }
|
|
local function hotspotsAsync(self, identity, cb)
|
|
local withUid = require("source.with_chapter_uid")
|
|
return ReviewCache.hotspotsAsync(identity, function(done)
|
|
return withUid.run(self, identity, _("缺少起点章节位置"), function(uid, finish)
|
|
return require("moonsource_qidian.reviews").hotspotsAsync(identity.stable_id, uid, finish)
|
|
end, done)
|
|
end, cb)
|
|
end
|
|
|
|
function Source:paragraphHotspotsAsync(identity, cb)
|
|
return hotspotsAsync(self, identity, function(list, err)
|
|
if not list then cb(nil, err); return end
|
|
local paragraphs = {}
|
|
for _idx, spot in ipairs(list) do
|
|
if tonumber(spot.range) > 0 then paragraphs[#paragraphs + 1] = spot end
|
|
end
|
|
cb(paragraphs)
|
|
end)
|
|
end
|
|
|
|
function Source:chapterReviewHotspotsAsync(identity, cb)
|
|
return hotspotsAsync(self, identity, function(list, err)
|
|
if not list then cb(nil, err); return end
|
|
local ending = {}
|
|
for _idx, spot in ipairs(list) do
|
|
if spot.range == "-1" then
|
|
ending[#ending + 1] = { range = spot.range, text = _("本章讨论"), total = spot.total,
|
|
badge = tostring(spot.total), heading = _("章节评论"),
|
|
anchor = require("book.review_locate").CHAPTER_ANCHOR }
|
|
end
|
|
end
|
|
cb(ending)
|
|
end)
|
|
end
|
|
|
|
--- 段落按段序定位到章节 HTML(子进程里跑,不碰实例状态)。
|
|
---@param hotspots QidianReviewHotspot[]
|
|
---@param html string
|
|
---@return table[]
|
|
function Source:locateHotspots(hotspots, html)
|
|
return require("moonsource_qidian.reviews").locate(hotspots, html)
|
|
end
|
|
|
|
--- 某段的一页评论;range 是段落序号,max_idx 是已取页数(首页 0)。
|
|
---@param identity BookIdentity
|
|
---@param range string
|
|
---@param max_idx integer
|
|
---@param cb fun(page: WechatReviewPage|nil, err: string|nil)
|
|
---@return { cancel: fun() }
|
|
function Source:paragraphReviewPageAsync(identity, range, max_idx, cb)
|
|
local withUid = require("source.with_chapter_uid")
|
|
return ReviewCache.pageAsync(identity, range, max_idx, function(done)
|
|
return withUid.run(self, identity, _("缺少起点章节位置"), function(uid, finish)
|
|
return require("moonsource_qidian.reviews").pageAsync(identity.stable_id, uid, tonumber(range), max_idx, finish)
|
|
end, done)
|
|
end, cb)
|
|
end
|
|
|
|
return Qidian
|