--[[--
晋江文学城数据映射(纯函数):App JSON 与手机网页 HTML → 领域对象。
@module koplugin.book.moonsource_jjwxc.mapper
--]]
local Text = require("utils.text")
local Vip = require("moonsource_jjwxc.vip")
local Mapper = {}
local SOURCE_ID = "jjwxc"
--- 带 ver 参数的封面链接服务端强制 gzip(Content-Encoding 与图片下载器不兼容,落盘变成无法识别的文件);
--- 去掉 ver 就是同一张图的原始 PNG。
---@param url string|nil
---@return string|nil
local function cover(url)
if type(url) ~= "string" or url == "" then return nil end
return (url:gsub("&ver=[^&]*", ""))
end
--- novelbasicinfo → Book。简介是实体转义过的 HTML。
---@param info table
---@return Book
function Mapper.book(info)
local words = tonumber((tostring(info.novelSize or ""):gsub(",", "")))
local status = type(info.novelStep) == "string" and Text.trim(info.novelStep) or ""
local tags = type(info.novelTags) == "string" and Text.trim(info.novelTags)
or type(info.novelTag) == "string" and Text.trim(info.novelTag) or ""
if tags ~= "" then tags = (tags:gsub("[,%s;;]+", " · ")) end
return {
source_id = SOURCE_ID,
stable_id = tostring(info.novelId),
title = info.novelName,
authors = info.authorName,
category = info.novelClass,
intro = info.novelIntro and Text.stripHtml(info.novelIntro),
cover = cover(info.novelCover),
meta = {
words = words,
status = status ~= "" and status or nil,
tags = tags ~= "" and tags or nil,
source = {
[SOURCE_ID] = {
protagonist = type(info.protagonist) == "string" and info.protagonist or nil,
perspective = type(info.mainview) == "string" and info.mainview or nil,
},
},
},
}
end
--- 搜索 wire → Book[](按 novelid / novelname 取,只留有书号的)。
---@param wire table
---@return Book[]
function Mapper.search(wire)
local out = {}
for _idx, item in ipairs(type(wire.items) == "table" and wire.items or {}) do
if item.novelid then
out[#out + 1] = {
source_id = SOURCE_ID,
stable_id = tostring(item.novelid),
title = item.novelname,
authors = item.authorname,
intro = item.novelintroshort and Text.stripHtml(item.novelintroshort),
cover = cover(item.ebookurl),
}
end
end
return out
end
--- 手机榜单页 HTML → { title, books }[]:按「【xx排行】」分节,每节只有书名和书号(作者、封面等详情补)。
---@param html string
---@return { title: string, books: Book[] }[]
function Mapper.rankSections(html)
local sections = {}
for title, body in html:gmatch("【(.-)】(.-)") do
local books = {}
for id, name in body:gmatch(']*>([^<]*)') do
books[#books + 1] = { source_id = SOURCE_ID, stable_id = id, title = Text.xmlDecode(name) }
end
if #books > 0 then sections[#sections + 1] = { title = title, books = books } end
end
return sections
end
--- 书城首页 getFullPage(channel=yq)里 type=rankings 的「六排行」。
--- 没有频道列表的项(页面中间那条空位)丢掉。
---@param home table
---@return { title: string, channels: { id: string, title: string }[] }[]
function Mapper.storeRanks(home)
local ranks = {}
for _idx, section in ipairs(type(home) == "table" and home or {}) do
if section.type == "rankings" then
for _i, item in ipairs(type(section.data) == "table" and section.data or {}) do
local more = item.more
local list = type(more) == "table" and more.channellist
if type(list) == "table" and more.rankName then
local channels = {}
for _j, ch in ipairs(list) do
if type(ch.channelid) == "string" and ch.channelid:find("^[%w_]+$") and ch.channel then
channels[#channels + 1] = { id = ch.channelid, title = ch.channel }
end
end
if #channels > 0 then
ranks[#ranks + 1] = { title = more.rankName, channels = channels }
end
end
end
end
end
return ranks
end
--- 榜单分页(channelMore)里的书。封面、简介有就带上,没有书号的丢掉。
---@param wire table|nil
---@return Book[]
function Mapper.rankBooks(wire)
local data = type(wire) == "table" and wire.data
local rows = type(data) == "table" and data.data or {}
local books = {}
for _idx, item in ipairs(type(rows) == "table" and rows or {}) do
local id = item.novelId or item.novelid
if id then
books[#books + 1] = {
source_id = SOURCE_ID,
stable_id = tostring(id),
title = item.novelName or item.novelname,
authors = item.authorName or item.authorname,
category = item.novelClass,
intro = item.novelIntroShort and Text.stripHtml(item.novelIntroShort),
cover = cover(item.cover or item.ebookurl),
}
end
end
return books
end
--- 分类页表单与结果;缺分页信息不是空书库,而是响应格式错误。
---@param html string
---@return table|nil, string|nil
function Mapper.category(html)
local filters = {}
for label in html:gmatch("") do
local name = label:match("name%s*=%s*['\"]([%w_]+)['\"]")
local key, value
if name then key, value = name:match("^(%a+)(%d+)$") end
if key and value then
filters[key] = filters[key] or {}
filters[key][#filters[key] + 1] = { id = value, title = Text.stripHtml(label) }
end
end
for tag, body in html:gmatch("(") do
local key = tag:match("name%s*=%s*['\"]([%w_]+)['\"]")
if key then
local options = {}
for value, text in body:gmatch("