--[[-- 掌阅网页响应 → 领域对象。纯函数,不发请求。 @module moonsource_ireader.mapper --]] local Text = require("utils.text") local Mapper = {} local SOURCE_ID = "ext.ireader" -- 官网 FZYouH_509R_V1.10.ttf 的二十个私用字形;两个连续区间,中间 A–F 没有字形。 local CONTENT_CHARS = {} for first, chars in pairs({ [0xF0000] = "的一是了在有他我这们", [0xF0010] = "来去说就事你对也还但", }) do for idx, ch in ipairs(Text.chars(chars)) do CONTENT_CHARS[Text.utf8char(first + idx - 1)] = ch end end --- 官网用补充私用区 A 代替常用字;还原为普通 Unicode,不要求用户安装官网字体。 ---@param html string ---@return string|nil 未知私用字码不能当作有效正文缓存 function Mapper.content(html) local unknown = false local decoded = html:gsub("\243[\176-\191][\128-\191][\128-\191]", function(ch) local plain = CONTENT_CHARS[ch] if not plain then unknown = true end return plain end) return not unknown and decoded or nil end --- 只读取官网书评首屏。 ---@class IReaderReviewPage ---@field items { author: string, text: string, time: string, grade: number|nil }[] ---@param html string ---@return IReaderReviewPage|nil function Mapper.reviews(html) local list = html:match('
]*class="con_dec"[^>]*>(.-)
') local time = card:match(']*class="sns_time"[^>]*>(.-)') if not author or not body or not time then return nil end items[#items + 1] = { author = Text.stripHtml(author), text = Text.stripHtml(body), time = Text.stripHtml(time), grade = tonumber(card:match('class="cur r(%d+)"')), } end if #items == 0 and Text.trim(list) ~= "" then return nil end return { items = items } end --- 目录缓存格式。首章携带,变了就当缓存失效。 Mapper.TOC_VERSION = 1 --- 扫码回包 body 是带引号的 JSON 字符串,网页用 replace 去掉引号。两种都认。 ---@param body any ---@return string|nil function Mapper.qrCode(body) if type(body) ~= "string" or body == "" then return nil end local ok, inner = pcall(require("json").decode, body) if ok and type(inner) == "string" and inner ~= "" then return inner end local stripped = body:gsub('"', "") if stripped == "" then return nil end return stripped end --- 轮询回包。等待是 30059(网页常量 WAIT_LOGIN),成功 body 里 name 是账号、session_id 是 zysid。 ---@param wire table|nil ---@return table|nil user ---@return "wait"|"fail"|nil kind function Mapper.login(wire) if type(wire) ~= "table" then return nil, "fail" end local code = tonumber(wire.code) if code == 30059 then return nil, "wait" end local body = wire.body if code == 0 and type(body) == "table" and type(body.name) == "string" and body.name ~= "" and type(body.session_id) == "string" and body.session_id ~= "" then local nick = type(body.nick) == "string" and body.nick ~= "" and body.nick or body.name return { usr = body.name, zysid = body.session_id, name = nick, avatar = type(body.avatar) == "string" and body.avatar or nil, } end return nil, "fail" end ---@param id string|number ---@param title string|nil ---@param author string|nil ---@param intro string|nil ---@param cover string|nil ---@return Book local function book(id, title, author, intro, cover) local function clean(value) if type(value) ~= "string" then return "" end return Text.trim(Text.xmlDecode(value)) end local picture = type(cover) == "string" and cover ~= "" and Text.xmlDecode(cover) or nil return { source_id = SOURCE_ID, stable_id = tostring(id), title = clean(title), authors = clean(author), intro = clean(intro), cover = picture, } end --- 书架行。文件夹和本机上传的书网页读不了,不进月读书架。 ---@param wire table|nil ---@return Book[] function Mapper.shelf(wire) local out = {} local rows = type(wire) == "table" and wire.data or nil if type(rows) ~= "table" then return out end for _idx, row in ipairs(rows) do if type(row) == "table" and row.book_id and row.dtype ~= 1 and row.is_local_book ~= true then local item = book(row.book_id, row.book_name, row.author, nil, row.icon or row.cover) if item.title ~= "" then out[#out + 1] = item end end end return out end --- 搜索结果页。卡片是 data-id + data-src + name / author / brief。 ---@param html string|nil ---@return Book[] function Mapper.search(html) local out, seen = {}, {} if type(html) ~= "string" then return out end local pos = 1 while true do local _start, stop, id = html:find('data%-id="(%d+)"', pos) if not _start then break end if not seen[id] then seen[id] = true local window = html:sub(stop, math.min(#html, stop + 1500)) local cover = window:match('data%-src="([^"]+)"') local title = window:match('class="name[^"]*"[^>]*>([^<]*)') local author = window:match('class="author[^"]*"[^>]*>([^<]*)') local intro = window:match('class="brief"[^>]*>([^<]*)') if title and Text.trim(title) ~= "" then out[#out + 1] = book(id, title, author, intro, cover) end end pos = stop end return out end --- 频道页 data-point 里的 cli_res_id / cli_res_name。同一本书滑块里会重复,只留第一次。 ---@param html string|nil ---@return Book[] function Mapper.channel(html) local out, seen = {}, {} if type(html) ~= "string" then return out end local pattern = '"cli_res_id"%s*:%s*(%d+)%s*,%s*"cli_res_name"%s*:%s*"([^"]*)"' local pos = 1 while true do local _start, stop, id, title = html:find(pattern, pos) if not _start then break end if not seen[id] then seen[id] = true local cover = html:sub(stop, math.min(#html, stop + 500)):match('src="(https://[^"]+)"') out[#out + 1] = book(id, title, nil, nil, cover) end pos = stop end return out end ---@param html string ---@return Book[] function Mapper.catalog(html) local out, seen = {}, {} for id, card in html:gmatch('