Files
moonsource-kmoe/moonsource-kmoe.koplugin/moonsource_kmoe/mapper.lua
T
Garfield 1dd1cccb68 ✨ (kmoe): 新增 Kmoe 漫画书源,按卷下载 EPUB 阅读
书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。
支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致,
会话 UA 随 Cookie 一起保存。
2026-11-27 13:00:01 +08:00

180 lines
6.7 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--[[--
Kmoe 回包解析:书单 JSON、详情页 HTML、卷列表 JSON、书架页 HTML。纯函数,不发请求。
@module moonsource_kmoe.mapper
--]]
local JSON = require("json")
local Text = require("utils.text")
local _ = require("l10n").get("moonsource_kmoe")
local SOURCE_ID = "kmoe"
local Mapper = {}
---@param s string|nil
---@return string
local function plain(s)
return Text.stripHtml(s or "")
end
---@param body string|nil
---@return table|nil
local function decode(body)
if type(body) ~= "string" or body == "" then return nil end
local ok, wire = pcall(JSON.decode, body)
return ok and type(wire) == "table" and wire or nil
end
--- 书页链接里的书 key(`/c/<key>.htm`),即本源的 stable_id。
---@param url string|nil
---@return string|nil
function Mapper.key(url)
return type(url) == "string" and url:match("/c/(%w+)%.htm") or nil
end
--- `data_list.php` 回包 → 书列表。搜索结果的书名带 `<b>` 高亮,一并剥掉。
---@param body string|nil
---@return Book[]|nil books
---@return string|nil err
---@return boolean|nil more 还有下一页
function Mapper.list(body)
local wire = decode(body)
if not wire or type(wire.data) ~= "table" then return nil, _("Kmoe 书单响应无效") end
local books = {}
for _idx, row in ipairs(wire.data) do
local key = type(row) == "table" and Mapper.key(row.url_book)
if key then
books[#books + 1] = {
source_id = SOURCE_ID,
stable_id = key,
title = plain((tostring(row.name or ""):gsub("</?[bB]>", ""))),
authors = plain(row.author),
cover = row.url_cover,
}
end
end
return books, nil, (tonumber(wire.nowpage) or 0) < (tonumber(wire.totalpage) or 0)
end
--- 详情页里一段 `<font style="display:…">…</font>` 中可见的文字;隐藏的占位不算。第二个起的作者前面垫了全角空格。
---@param block string|nil
---@return string[]
local function visible(block)
local out = {}
for display, inner in (block or ""):gmatch('<font style="display:(%a*)">(.-)</font>') do
local text = Text.trim((plain(inner):gsub("\227\128\128", " ")))
if display == "" and text ~= "" then out[#out + 1] = text end
end
return out
end
---@class KmoeDetail
---@field book Book
---@field bookid string 数字书号;收藏、下载接口用它
---@field hash string|nil 卷列表 `data_book.php?h=` 的参数
---@field logged_in boolean 页面按登录态渲染(`var uin` 非空)
--- 手机版详情页 `/m/c/<key>.htm` → 书信息。认不出书号或书名返回 nil。
---@param key string
---@param html string|nil
---@return KmoeDetail|nil
function Mapper.detail(key, html)
html = html or ""
local bookid = html:match('var%s+bookid%s*=%s*"(%d+)"')
local title = html:match("<b class='font_size_l'[^>]*>(.-)</b>")
if not bookid or not title then return nil end
local intro = html:match('getElementById%("div_desc_content"%)%.innerHTML%s*=%s*"(.-)";')
return {
book = {
source_id = SOURCE_ID,
stable_id = key,
title = plain(title),
authors = table.concat(visible(html:match("作者:(.-)<br")), ", "),
category = table.concat(visible(html:match("分類:(.-)<br")), " "),
intro = intro and plain((intro:gsub("\\(.)", "%1"))) or nil,
cover = html:match('<img class="cover_l[^"]*" src="([^"]+)"'),
},
bookid = bookid,
hash = html:match("book_data%.php%?h=(%w+)"),
logged_in = (html:match('var%s+uin%s*=%s*"(%d*)"') or "") ~= "",
}
end
--- 卷下载请求路径(EPUB、普通线路),存在 toc 的 origin 里。
---@param bookid string
---@param volid string
---@return string
function Mapper.downPath(bookid, volid)
return "/getdownurl.php?b=" .. bookid .. "&v=" .. volid .. "&mobi=2&vip=0&json=1"
end
---@class KmoeChapter : BookChapter
---@field building boolean|nil EPUB 还在制作,暂时不能下载
--- `data_book.php` 回包 → 每卷一章。服务器已按 單行本 / 話 / 番外 分组排好,原样保序。
--- voldata 每行:[1] 卷号 [4] 类型 [6] 标题 [12] EPUB 大小(MB,0 表示还在制作)。
---@param body string|nil
---@param bookid string
---@return KmoeChapter[]|nil
---@return string|nil err
function Mapper.chapters(body, bookid)
local wire = decode(body)
if not wire then return nil, _("Kmoe 没有返回这部漫画的卷列表") end
if type(wire.voldata) ~= "table" or #wire.voldata == 0 then return nil, _("这部漫画还没有可下载的卷") end
local chapters = {}
for idx, vol in ipairs(wire.voldata) do
local volid = type(vol) == "table" and tostring(vol[1] or "")
if not volid or not volid:match("^%d+$") then return nil, _("Kmoe 卷列表响应无效") end
chapters[idx] = {
idx = idx,
uid = volid,
title = plain(vol[6]),
origin = Mapper.downPath(bookid, volid),
depth = 1,
building = (tonumber(vol[12]) or 0) <= 0 or nil,
}
end
return chapters
end
--- `getdownurl.php?json=1` 回包 → 文件地址。
---@param body string|nil
---@return string|nil url
---@return string|nil err
function Mapper.downUrl(body)
local wire = decode(body)
if not wire then return nil, _("Kmoe 下载地址响应无效") end
if type(wire.url) == "string" and wire.url ~= "" then return wire.url end
local msg = wire.msg or wire.message
return nil, type(msg) == "string" and msg ~= "" and plain(msg) or _("Kmoe 没有返回下载地址")
end
---@class KmoeShelfRow
---@field book Book
---@field bookid string
--- 「訂閱收藏」页 `/myfollow.php` → 收藏的书。只订阅不收藏的行不算书架。
---@param html string|nil
---@return KmoeShelfRow[]|nil rows
---@return string|nil err
---@return boolean paged 页面出现分页链接(这一页不是完整书架)
function Mapper.shelf(html)
html = html or ""
if not html:find("myfollow.php", 1, true) then return nil, _("Kmoe 书架页面无法识别"), false end
local rows = {}
for tr in html:gmatch('<tr style="height:36px;">(.-)</tr>') do
local key, title, author = tr:match("href='[^']*/c/(%w+)%.htm'[^>]*>(.-)</a></td>%s*<td[^>]*>(.-)</td>")
local bookid = tr:match("sel_pushtype_(%d+)")
local fav = tr:match('icon%-tick%-fav%.png"[^>]-display:(%a*)"')
if key and bookid and fav == "" then
rows[#rows + 1] = {
book = { source_id = SOURCE_ID, stable_id = key, title = plain(title), authors = plain(author) },
bookid = bookid,
}
end
end
return rows, nil, html:find("myfollow%.php%?[^\"']*p=%d") ~= nil
end
return Mapper