') do
local title = item:match('class="book%-title"[^>]*>([^<]*)<')
if title then
local module = item:match('class="book%-module"[^>]*>(.-)') or ""
local cover = item:match('

]*>([^<]*)<') or "")),
cover = cover and (cover:gsub("^//", "https://")),
}
end
end
if #out == 0 then return nil, _("响应无效") end
return out
end
--- 目录缓存格式版本。
Mapper.TOC_VERSION = 1
--- 手机站目录页 → 目录。页面内嵌 SSR JSON,`"chapters":[` 之后是扁平对象:type 0 是卷,type 1 是章(price > 0 为 VIP)。
---@param html string
---@return BookChapter[]|nil
---@return string|nil
function Mapper.toc(html)
local toc = {}
local rows
if html:find('id="vite-plugin-ssr_pageContext"', 1, true) then
local data, err = Mapper.state(html)
if not data then return nil, err end
rows = data.chapters or data.chapterListData
else
local raw = html:match('"chapters":%s*(%b[])')
rows = raw and Mapper.decode('{"rows":' .. raw .. "}")
rows = rows and rows.rows
end
if type(rows) ~= "table" then return nil, _("响应无效") end
for _idx, row in ipairs(rows) do
if type(row) ~= "table" or (row.type ~= 0 and row.type ~= 1) then return nil, _("响应无效") end
if row.type == 1 then
if type(row.ccid) ~= "string" or not row.ccid:match("^%d+$") or type(row.title) ~= "string" then
return nil, _("响应无效")
end
toc[#toc + 1] = {
idx = #toc + 1,
uid = row.ccid,
title = row.title,
vip = (tonumber(row.price) or 0) > 0 or nil,
}
end
end
if toc[1] then toc[1].toc_version = Mapper.TOC_VERSION end
return toc
end
--- 章节接口回包 → 正文纯文本。没买的 VIP 章(价格 > 0 且 auth 不为真)只有 130 字预览,返回 nil, "unpaid"。
--- 正文段落以 \r\n 分隔、每段前有全角缩进。
---@param wire table
---@return string|nil text
---@return "unpaid"|"encrypted"|nil kind
---@return table|nil encrypted
function Mapper.content(wire)
local chapters = type(wire.data) == "table" and wire.data.chapterContents
local chapter = chapters and chapters[1]
if type(chapter) ~= "table" then return nil end
local price = tonumber(chapter.chapterPrice)
if not price then return nil end
if price > 0 and chapter.auth ~= true then return nil, "unpaid" end
if chapter.encrypt == true then return nil, "encrypted", chapter end
if type(chapter.chapterContent) ~= "string" then return nil end
local lines = {}
for line in chapter.chapterContent:gmatch("[^\r\n]+") do
line = Text.trim(line)
while line:sub(1, 3) == "\227\128\128" do line = Text.trim(line:sub(4)) end
if line ~= "" then lines[#lines + 1] = line end
end
if #lines == 0 then return nil end
return table.concat(lines, "\n")
end
--- 书架行 → Book[]。封面按书 id 拼(书架接口不给)。
---@param rows table[]
---@return Book[]
function Mapper.shelfList(rows)
local out = {}
for _idx, row in ipairs(rows) do
local id = tostring(row.bookid)
out[#out + 1] = {
source_id = SOURCE_ID,
stable_id = id,
title = Text.trim(row.title or ""),
authors = Text.trim(row.author or ""),
cover = "https://bookcover.yuewen.com/qdbimg/349573/c_" .. id .. "/180",
}
end
return out
end
--- 书架行 → 书 id → 最近读到的章 id(ccid 为 "0" 表示没读过)。
---@param rows table[]
---@return table
function Mapper.progress(rows)
local out = {}
for _idx, row in ipairs(rows) do
local ccid = tostring(row.ccid or "0")
if ccid ~= "0" then out[tostring(row.bookid)] = ccid end
end
return out
end
return Mapper