--[[-- 网易云阅读响应到书籍、目录和正文。 电脑端目录来自 getBook.do,分卷也保留为章节,避免改变已有章节序号。 正文的 x 字段指定 XOR / AES 解密顺序;没有 x 时为标准 Base64。 @module moonsource_netease.mapper --]] local Text = require("utils.text") local JSON = require("json") local _ = require("l10n").get("moonsource_netease") ---@class NeteaseChapter : BookChapter ---@field big_content_id string|nil ---@field leaf boolean|nil local Mapper = {} Mapper.TOC_VERSION = 2 local SOURCE_ID = "netease" function Mapper.authorized(html) return type(html) == "string" and html:find("
]*>(.-)
") or ""), }) if not item then return nil, nil, _("响应无效") end if not seen[item.stable_id] then seen[item.stable_id] = true books[#books + 1] = item end end end local next_page for encoded in html:gmatch('href=["\']([^"\']+)["\']') do local link = Text.xmlDecode(encoded) local target = tonumber(link:match("[?&]page=(%d+)")) if link:find("/shelf.do?", 1, true) and link:find("operation=query_current", 1, true) and target and target > page and (not next_page or target < next_page) then next_page = target end end return books, next_page end ---@param wire table|nil ---@return Book|nil function Mapper.detail(wire) if type(wire) ~= "table" or type(wire.portions) ~= "table" then return nil end return book({ sourceUuid = wire.id, title = wire.title, author = wire.author, description = wire.shareDescription, coverImage = wire.coverImg, }) end ---@param wire table|nil ---@return NeteaseChapter[]|nil function Mapper.chapters(wire) local rows = type(wire) == "table" and wire.portions or nil if type(rows) ~= "table" then return nil end local out = {} for _idx, row in ipairs(rows) do if type(row) == "table" and row.id ~= nil and tostring(row.id) ~= "" then out[#out + 1] = { idx = #out + 1, uid = tostring(row.id), title = clean(row.title), sec_id = tostring(row.id), big_content_id = row.bigContentId, leaf = row.leafNode, word_count = tonumber(row.charSize), vip = tonumber(row.needPay) == 1 and row.isPaid ~= true, } end end if out[1] then out[1].toc_version = Mapper.TOC_VERSION end return out end --- 电脑端以章节 UUID 定位;匿名响应带默认章节,但没有阅读时间。 ---@param wire table|nil ---@return integer|nil function Mapper.progressIndex(wire) if type(wire) ~= "table" or (tonumber(wire.curDate) or 0) <= 0 then return nil end for idx, row in ipairs(wire.portions or {}) do if row.id == wire.curArticleUuid then return idx end end end --- 电脑端章内百分比是 0..100,不是比例 0..1。 ---@param stable_id string ---@param toc BookChapter[]|nil ---@param idx number|nil ---@param fraction number|nil ---@param now_ms number ---@return table|nil function Mapper.readlog(stable_id, toc, idx, fraction, now_ms) if type(toc) ~= "table" or type(idx) ~= "number" then return nil end idx = math.floor(idx) local chapter = toc[idx] if type(chapter) ~= "table" or type(chapter.uid) ~= "string" or chapter.uid == "" then return nil end local page = tonumber(fraction) or 0 if page < 0 then page = 0 end if page > 1 then page = 1 end return { sourceUuid = stable_id, articleUuid = chapter.uid, curPagePercent = page * 100, date = now_ms, } end --- 书评。网页模板用的字段:username、text、posttime、grade、likes、replied.username。 --- 第二返回值是总页数,没有分页信息就是 nil。 ---@param wire table|nil ---@return { author: string, text: string, time: string|nil, grade: number|nil, likes: number|nil, reply_to: string|nil }[]|nil ---@return integer|nil function Mapper.comments(wire) if type(wire) ~= "table" or tonumber(wire.resultCode) ~= 0 or type(wire.data) ~= "table" then return nil end local out = {} for _idx, row in ipairs(wire.data) do if type(row) == "table" and type(row.text) == "string" then local item = { author = type(row.username) == "string" and row.username or "", text = row.text, time = type(row.posttime) == "string" and row.posttime or nil, grade = tonumber(row.stars), likes = tonumber(row.likes), } if type(row.replied) == "table" and type(row.replied.username) == "string" then item.reply_to = row.replied.username end out[#out + 1] = item end end return out, tonumber(wire.totalPage) end --- XOR 作用于 JavaScript UTF-16 码元,而不是 UTF-8 字节。 local function xor(text, key) local bxor = require("bit").bxor local Glyph = require("utils.glyph") return (text:gsub(Text.UTF8_CHAR, function(ch) local cp = Glyph.codepoint(ch) if cp <= 0xFFFF then return Text.utf8char(bxor(cp, key)) end cp = cp - 0x10000 local high = bxor(0xD800 + math.floor(cp / 0x400), key) local low = bxor(0xDC00 + cp % 0x400, key) return Text.utf8char(0x10000 + (high - 0xD800) * 0x400 + low - 0xDC00) end)) end --- 正文解码失败必须报错,不得把密文写进章节缓存。 ---@param wire table|nil ---@return string|nil function Mapper.content(wire) local cipher = type(wire) == "table" and wire.content or nil if type(cipher) ~= "string" or cipher == "" then return nil, _("正文为空") end local html = Text.base64Decode(cipher) if type(wire.x) == "string" and wire.x ~= "" then local ok, steps = pcall(JSON.decode, Text.base64Decode(wire.x)) if not ok or type(steps) ~= "table" or #steps < 1 or #steps > 2 then return nil, _("正文解析失败") end for _idx, step in ipairs(steps) do if type(step) ~= "table" then return nil, _("正文解析失败") end local decoded, value if step.m == 1 and tonumber(step.k) then decoded, value = pcall(xor, html, tonumber(step.k)) elseif step.m == 2 and type(step.k) == "string" and html:match("^%x+$") and #html % 32 == 0 then local Aes = require("crypto.aes") decoded, value = pcall(Aes.ecb_decrypt, Aes.from_hex(html), step.k) end if not decoded then return nil, _("正文解析失败") end html = value end end if html == "" then return nil, _("正文为空") end return html end return Mapper