✨ (kmoe): 新增 Kmoe 漫画书源,按卷下载 EPUB 阅读

书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。
支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致,
会话 UA 随 Cookie 一起保存。
This commit is contained in:
Garfield committed 2026-11-27 13:00:01 +08:00
commit 1dd1cccb68
24 files changed
+2522

No files matched your search

+173
View File
@@ -0,0 +1,173 @@
--[[--
moonsource_kmoe.client 离线用例:跳转归类、公开接口不带账号 Cookie、登录取 Cookie、账号校验、收藏确认。
@module tests.moonsource_kmoe.client_spec
--]]
---@diagnostic disable: need-check-nil, undefined-field, inject-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
Stubs.install()
package.loaded["json"] = { decode = require("support.json_stub").decode }
local calls, script, expired = {}, {}, {}
local function header(res, name)
local v = res and res.headers and res.headers[name]
return type(v) == "table" and v[1] or v
end
package.preload["http.request"] = function()
local function send(method, url, body, opts, cb)
local jar = opts and opts.jar
calls[#calls + 1] = { method = method, url = url, body = body, cookie = jar and jar:header(url) or nil,
ua = opts and opts.headers and opts.headers["User-Agent"] }
local step = table.remove(script, 1) or { code = 200, body = "" }
if step.code ~= 200 then
cb(nil, "HTTP " .. step.code, { code = step.code, headers = step.headers or {} })
else
cb(step.body, nil, { code = 200, headers = step.headers or {} })
end
return { cancel = function() end }
end
return {
get = function(url, opts, cb) return send("GET", url, nil, opts, cb) end,
post = function(url, body, opts, cb) return send("POST", url, body, opts, cb) end,
header = header,
headerAll = function(res, name)
local v = res and res.headers and res.headers[name]
return type(v) == "table" and v or { v }
end,
}
end
package.preload["source.auth"] = function()
return { expire = function(id, msg) expired[#expired + 1] = id; return msg end }
end
local Client = require("moonsource_kmoe.client")
local function reset(steps)
calls, script, expired = {}, steps, {}
end
-- 书单是公开接口:登录着也不带账号 Cookie;分类、排序、页码进查询串。
do
reset({ { code = 200, body = [[{"totalpage":1,"nowpage":1,"data":[]}]] } })
local books, more
Client.new("VOLSKEY=k"):listAsync({ s = "CAT*幽默", by = "score", ["end"] = "連載", lang = "chn" }, 2,
function(b, _e, m) books, more = b, m end)
Assert.len(books, 0)
Assert.is_false(more)
Assert.is_nil(calls[1].cookie)
local url = calls[1].url
Assert.matches(url, "^https://mox%.moe/data_list%.php%?")
Assert.is_true(url:find("s=CAT%2A%E5%B9%BD%E9%BB%98", 1, true) ~= nil, url)
Assert.is_true(url:find("by=score", 1, true) ~= nil)
Assert.is_true(url:find("lang=chn", 1, true) ~= nil)
Assert.is_true(url:find("&p=2", 1, true) ~= nil)
end
-- 详情带登录 Cookie;没做手机验证 → 手机验证说明;登录失效 → 标记失效;未登录 → 请先登录。
do
reset({ { code = 302, headers = { Location = "https://mox.moe/m/regphone.php" } } })
local url, err
Client.new("VOLSKEY=k"):downUrlAsync("/getdownurl.php?b=1&v=2&mobi=2&vip=0&json=1", function(u, e) url, err = u, e end)
Assert.is_nil(url)
Assert.matches(err, "手機驗證")
Assert.eq(calls[1].cookie, "VOLSKEY=k")
reset({ { code = 302, headers = { Location = "https://mox.moe/login.php" } } })
Client.new("VOLSKEY=k"):detailAsync("abc", function(_d, e) err = e end)
Assert.matches(err, "重新登录")
Assert.eq(expired[1], "kmoe")
Assert.eq(calls[1].url, "https://mox.moe/m/c/abc.htm")
reset({})
Client.new(nil):downUrlAsync("/getdownurl.php", function(_u, e) err = e end)
Stubs.flush()
Assert.matches(err, "请先登录")
Assert.len(calls, 0, "没登录不发下载请求")
Assert.len(expired, 0)
reset({ { code = 200, body = [[{"url":"/dl/x.epub","name":"x.epub"}]] } })
Client.new("VOLSKEY=k"):downUrlAsync("/getdownurl.php", function(u) url = u end)
Assert.eq(url, "https://mox.moe/dl/x.epub")
end
-- 会话 UA:带了就逐字沿用,没带用默认电脑 UA;取下载地址 403 = UA 和登录时不一致。
do
reset({ { code = 200, body = "" }, { code = 200, body = "" } })
Client.new("VOLSKEY=k", "Browser/157"):detailAsync("abc", function() end)
Client.new("VOLSKEY=k"):detailAsync("abc", function() end)
Assert.eq(calls[1].ua, "Browser/157")
Assert.eq(calls[2].ua, Client.ua)
Assert.matches(Client.ua, "Macintosh")
reset({ { code = 403 } })
local url, err
Client.new("VOLSKEY=k"):downUrlAsync("/getdownurl.php", function(u, e) url, err = u, e end)
Assert.is_nil(url)
Assert.matches(err, "浏览器和现在不一致")
end
-- 卷列表:详情页不是登录态就不请求 data_book(匿名只会拿到空列表)。
do
reset({})
local err
Client.new("VOLSKEY=k"):volumesAsync({ logged_in = false, hash = "h", bookid = "1" }, function(_c, e) err = e end)
Stubs.flush()
Assert.len(calls, 0)
Assert.matches(err, "重新登录")
end
-- 密码登录:表单字段 email / passwd;m100 + Set-Cookie 才成功;失败带出站点原话。
do
reset({ { code = 200, body = [[{"ret":0,"msg":"","msgid":"m100"}]],
headers = { ["Set-Cookie"] = { "VOLSKEY=abc; path=/; domain=.mox.moe", "VLIBSID=s1; path=/" } } } })
local cookie, err
Client.loginAsync("[email protected]", "p&w", function(c, e) cookie, err = c, e end)
Assert.is_nil(err)
Assert.is_true(cookie:find("VOLSKEY=abc", 1, true) ~= nil)
Assert.is_true(cookie:find("VLIBSID=s1", 1, true) ~= nil)
Assert.eq(calls[1].method, "POST")
Assert.eq(calls[1].url, "https://mox.moe/login_act.php")
Assert.eq(calls[1].body, "email=a%40b.c&passwd=p%26w")
Assert.is_nil(calls[1].cookie)
reset({ { code = 200, body = [[{"ret":1,"msg":"郵箱帳號或密碼錯誤。","msgid":"e400"}]] } })
Client.loginAsync("[email protected]", "x", function(c, e) cookie, err = c, e end)
Assert.is_nil(cookie)
Assert.eq(err, "郵箱帳號或密碼錯誤。")
end
-- 账号校验:「我的」页里的 /u/<uin>/ 是账号;跳登录页是 Cookie 无效。
do
reset({ { code = 200, body = '<a href="https://mox.moe/u/123456/">主頁</a>' } })
local ok, err, account
Client.accountAsync("VOLSKEY=k", function(o, e, a) ok, err, account = o, e, a end)
Assert.is_true(ok)
Assert.eq(account.id, "123456")
Assert.eq(calls[1].cookie, "VOLSKEY=k")
reset({ { code = 302, headers = { Location = "https://mox.moe/login.php" } } })
Client.accountAsync("VOLSKEY=bad", function(o, e) ok, err = o, e end)
Assert.is_false(ok)
Assert.matches(err, "登录")
end
-- 收藏:先读详情拿数字书号,回包里有 c101 才算成功。
do
local detail = [[<b class='font_size_l'>有鬼</b><script>var uin = "1"; var bookid = "34410";</script>]]
reset({ { code = 200, body = detail }, { code = 200, body = '<script>parent.display_codeinfo( "c101", 1 );</script>' } })
local ok, err
Client.new("VOLSKEY=k"):addToShelfAsync("de34d9", function(o, e) ok, err = o, e end)
Assert.is_true(ok)
Assert.eq(calls[2].url, "https://mox.moe/book_follow.php")
Assert.eq(calls[2].body, "follow_act=1&follow_bookid=34410&follow_type=0")
reset({ { code = 200, body = detail }, { code = 200, body = '<script>parent.display_codeinfo( "e999", 1 );</script>' } })
Client.new("VOLSKEY=k"):removeFromShelfAsync("de34d9", function(o, e) ok, err = o, e end)
Assert.is_nil(ok)
Assert.not_nil(err)
Assert.is_true(calls[2].body:find("follow_act=0", 1, true) ~= nil)
end
+133
View File
@@ -0,0 +1,133 @@
--[[--
moonsource_kmoe.mapper 离线用例:书单 JSON、详情页 HTML、卷列表、下载地址、书架页。样本结构取自 mox.moe 实际回包。
@module tests.moonsource_kmoe.mapper_spec
--]]
---@diagnostic disable: need-check-nil, undefined-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
Stubs.install()
package.loaded["json"] = { decode = require("support.json_stub").decode }
local Mapper = require("moonsource_kmoe.mapper")
-- 书单:搜索结果名字带 <b> 高亮;nowpage < totalpage 才还有下一页。
do
local body = [[{"uin":"","total":2,"totalpage":2,"nowcount":1,"nowpage":1,"data":[
{"status":"\u5b8c\u7d50","url_book":"https:\/\/mox.moe\/c\/f1bc90.htm","url_cover":"https:\/\/kmimg.moex.ink\/cover\/x.jpg?sign=a",
"newvol":"\u5377 31","name":"<b>\u881f\u7b46<\/b>\u5c0f\u65b0","author":"\u81fc\u4e95\u5100\u4eba","score":"9.1"},
{"url_book":"https:\/\/mox.moe\/about.htm","name":"x"}]}]]
local books, err, more = Mapper.list(body)
Assert.is_nil(err)
Assert.len(books, 1, "认不出书 key 的行丢掉")
Assert.eq(books[1].stable_id, "f1bc90")
Assert.eq(books[1].source_id, "kmoe")
Assert.eq(books[1].title, "蠟筆小新")
Assert.eq(books[1].authors, "臼井儀人")
Assert.eq(books[1].cover, "https://kmimg.moex.ink/cover/x.jpg?sign=a")
Assert.is_true(more)
local _b, _e, last = Mapper.list([[{"totalpage":3,"nowpage":3,"data":[]}]])
Assert.is_false(last)
local none, bad = Mapper.list("<html>")
Assert.is_nil(none)
Assert.not_nil(bad, "非 JSON 是响应无效,不是空结果")
end
local DETAIL = [[
<img class="cover_l border_gray" src="https://kmimg.moex.ink/cover/l.jpg!cover_l?sign=b" alt="蠟筆小新" border="0" /><br />
<b class='font_size_l' style="line-height:30px;">蠟筆小新</b><br>
<span class="font_size_m" style="line-height:26px;">
作者:<font style="display:"><a href="https://mox.moe/m/idx.php?s=%E8%87%BC" class="color_link">臼井儀人</a></font>
<font style="display:"> <a href="https://mox.moe/m/idx.php?s=x" class="color_link">UY Team</a></font>
<font style="display:none"> <a href="https://mox.moe/m/idx.php?s=" class="color_link"></a></font>
<font style="display:none"> ...</font>
<br />
狀態:完結
<br />
分類:<font style="display:">幽默</font>
&nbsp; <font style="display:">治癒</font>
<br />
<script>
var uin = "@UIN@";
var bookid = "35063";
var can_do_fav = "1";
document.getElementById("div_desc_content").innerHTML = "五歲的小孩,<br />引發 \"笑料\"&nbsp;Crayon";
window.iframe_action2.location.href = "/book_data.php?h=17957395811f1bc9";
</script>]]
-- 详情:只取可见作者 / 分类;简介去标签解实体;uin 非空才算登录态页面。
do
local d = Mapper.detail("f1bc90", DETAIL:gsub("@UIN@", ""))
Assert.eq(d.bookid, "35063")
Assert.eq(d.hash, "17957395811f1bc9")
Assert.is_false(d.logged_in)
Assert.eq(d.book.stable_id, "f1bc90")
Assert.eq(d.book.title, "蠟筆小新")
Assert.eq(d.book.authors, "臼井儀人, UY Team")
Assert.eq(d.book.category, "幽默 治癒")
Assert.eq(d.book.cover, "https://kmimg.moex.ink/cover/l.jpg!cover_l?sign=b")
Assert.eq(d.book.intro, "五歲的小孩,\n引發 \"笑料\" Crayon")
Assert.is_true(Mapper.detail("f1bc90", (DETAIL:gsub("@UIN@", "123"))).logged_in)
Assert.is_nil(Mapper.detail("f1bc90", "<html>維護中</html>"), "认不出书号就是解析失败")
end
-- 卷列表:保序,卷号做 uid,EPUB 大小 0 标为制作中;空回包 / 空列表都是错误。
do
local body = [==[{"msgid":0,"bookkey":"de34d9","voldata":[
["1001",0,0,"單行本",1,"卷 01",120,240,"60.1","70.2","0","70.3","","2024-01-01"],
["3086",0,0,"話",2,"話 086-090",90,180,"20","25","0","0","","2026-11-23"]]}]==]
local chapters, err = Mapper.chapters(body, "34410")
Assert.is_nil(err)
Assert.len(chapters, 2)
Assert.eq(chapters[1].idx, 1)
Assert.eq(chapters[1].uid, "1001")
Assert.eq(chapters[1].title, "卷 01")
Assert.eq(chapters[1].origin, "/getdownurl.php?b=34410&v=1001&mobi=2&vip=0&json=1")
Assert.is_nil(chapters[1].building)
Assert.is_true(chapters[2].building)
local none, empty_err = Mapper.chapters("", "1")
Assert.is_nil(none)
Assert.not_nil(empty_err, "空回包不能当成没有卷")
local _n, no_vol = Mapper.chapters([[{"voldata":[]}]], "1")
Assert.not_nil(no_vol)
end
-- 下载地址:有 url 返回;没有就把站点消息带出来。
do
Assert.eq(Mapper.downUrl([[{"url":"https:\/\/dl.example\/a.epub","name":"a.epub"}]]), "https://dl.example/a.epub")
local url, err = Mapper.downUrl([[{"msg":"額度不足"}]])
Assert.is_nil(url)
Assert.eq(err, "額度不足")
Assert.is_nil((Mapper.downUrl("<html>")))
end
local function row(key, bookid, title, fav)
return ([[<tr style="height:36px;"> <td width='10px'></td> <td width='350px' style='text-align:left'><a href='https://mox.moe/c/%s.htm' target='_blank'>%s</a></td> <td width='190px' style='text-align:left'>コノシマルカ</td> <td> <img src="https://img.mxomo.com/moe/icon-tick.png" style="width:20px;display:none"> <select id="sel_pushtype_%s"></select> </td> <td> <img src="https://img.mxomo.com/moe/icon-tick-fav.png" style="width:70px;height:20px;vertical-align:middle;display:%s"> </td> </tr>]])
:format(key, title, bookid, fav)
end
-- 书架:只收打了收藏的行;页脚的 /c/10001.htm 不在表格行里;出现分页链接时标不完整。
do
local html = '<a href="https://mox.moe/myfollow.php?f=7">⬆︎</a><table>'
.. row("de34d9", "34410", "有鬼拍不到", "") .. row("abc123", "1", "只訂閱", "none")
.. '</table><a href="https://mox.moe/c/10001.htm">說明</a>'
local rows, err, paged = Mapper.shelf(html)
Assert.is_nil(err)
Assert.len(rows, 1)
Assert.eq(rows[1].book.stable_id, "de34d9")
Assert.eq(rows[1].book.title, "有鬼拍不到")
Assert.eq(rows[1].book.authors, "コノシマルカ")
Assert.eq(rows[1].bookid, "34410")
Assert.is_false(paged)
local _r, _e, more = Mapper.shelf(html .. '<a href="https://mox.moe/myfollow.php?f=7&p=2">2</a>')
Assert.is_true(more)
local empty = Mapper.shelf('<a href="https://mox.moe/myfollow.php?f=7">⬆︎</a>')
Assert.len(empty, 0, "空收藏是空列表")
local none, bad = Mapper.shelf("<html>維護中</html>")
Assert.is_nil(none)
Assert.not_nil(bad, "认不出的页面不能当空书架")
end
+151
View File
@@ -0,0 +1,151 @@
--[[--
moonsource_kmoe.source / chapter 离线用例:书城节点与筛选组、搜索聚合、卷下载(制作中、已有文件、非 EPUB)、缓存统计。
@module tests.moonsource_kmoe.source_spec
--]]
---@diagnostic disable: need-check-nil, undefined-field, inject-field, duplicate-set-field
local Assert = require("support.assert")
local Stubs = require("support.stubs")
Stubs.install()
local WORK = os.tmpname()
os.remove(WORK)
os.execute("mkdir -p '" .. WORK .. "'")
package.preload["utils.paths"] = function()
return {
bookWorkDir = function(stable_id) return WORK .. "/" .. stable_id end,
ensureBookWork = function(stable_id) os.execute("mkdir -p '" .. WORK .. "/" .. stable_id .. "'") end,
}
end
package.preload["utils.settings"] = function()
return { getSource = function() return { cookie = "VOLSKEY=k" } end, saveSource = function() end }
end
local queries, downloads, down_body = {}, {}, nil
package.preload["http.request"] = function()
return {
download = function(opts, dest, cb)
downloads[#downloads + 1] = { url = opts.url, dest = dest }
local f = io.open(dest, "wb")
f:write(down_body)
f:close()
cb(true, nil, { code = 200 })
return { cancel = function() end }
end,
}
end
local fake = { ua = "UA", BASE = "https://mox.moe" }
package.preload["moonsource_kmoe.client"] = function()
return { new = function() return fake end }
end
function fake.loggedIn() return true end
function fake.listAsync(_self, query, page, cb)
queries[#queries + 1] = { query = query, page = page }
cb({ { source_id = "kmoe", stable_id = query.s .. page } }, nil, page < 2)
return { cancel = function() end }
end
function fake.downUrlAsync(_self, origin, cb)
cb("https://dl.example/" .. origin)
return { cancel = function() end }
end
local Kmoe = require("moonsource_kmoe.source")
local Chapter = require("moonsource_kmoe.chapter")
local src = Kmoe.new()
-- 缺省节点:综合排序、不限状态 / 语言 / 分类;四组 chip 各有名字。
do
local res
src:browseAsync({ limit = 1 }, function(r) res = r end)
Assert.eq(queries[1].query.by, "sortpoint")
Assert.eq(queries[1].query.s, "")
Assert.len(res.groups, 4)
Assert.eq(res.groups[1].active, "sortpoint,,,")
Assert.eq(res.groups[1].title, "排序")
Assert.eq(res.groups[4].title, "分类")
Assert.eq(res.groups[2].chips[2].id, "sortpoint,連載,,", "改状态保留其他三段")
Assert.eq(res.groups[4].chips[2].id, "sortpoint,,,幽默")
end
-- 选中分类:查询串 CAT*<分类>,别的段原样;认不出的段回到缺省。
do
queries = {}
local res
src:browseAsync({ node = "score,完結,jpn,幽默", limit = 1 }, function(r) res = r end)
Assert.eq(queries[1].query.s, "CAT*幽默")
Assert.eq(queries[1].query.by, "score")
Assert.eq(queries[1].query["end"], "完結")
Assert.eq(queries[1].query.lang, "jpn")
Assert.eq(res.groups[4].active, "score,完結,jpn,幽默")
queries = {}
src:browseAsync({ node = "evil,x,y,不存在", limit = 1 }, function(r) res = r end)
Assert.eq(queries[1].query.by, "sortpoint")
Assert.eq(queries[1].query.s, "")
Assert.eq(res.groups[1].active, "sortpoint,,,")
end
-- 搜索:按页聚合到源说没有更多为止。
do
queries = {}
local res
src:searchAsync("小新", function(r) res = r end)
Assert.len(queries, 2)
Assert.eq(queries[1].query.s, "小新")
Assert.len(res.data, 2)
end
local identity = { source_id = "kmoe", stable_id = "de34d9" }
-- 制作中的卷不请求下载。
do
local path, err
Chapter.materializeAsync(fake, identity, { uid = "1", origin = "o", building = true }, nil,
function(p, e) path, err = p, e end)
Stubs.flush()
Assert.is_nil(path)
Assert.matches(err, "制作中")
Assert.len(downloads, 0)
end
-- 下到的不是 zip 就删掉报错;是 EPUB 就交出路径,下次直接复用不再下载。
do
down_body = "<html>額度不足</html>"
local path, err
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p, e) path, err = p, e end)
Assert.is_nil(path)
Assert.matches(err, "EPUB")
Assert.is_nil(io.open(WORK .. "/de34d9/3086.epub", "rb"), "坏文件不留")
down_body = "PK\3\4epub"
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p, e) path, err = p, e end)
Assert.eq(path, WORK .. "/de34d9/3086.epub")
Assert.eq(downloads[#downloads].url, "https://dl.example/o")
local n = #downloads
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p) path = p end)
Stubs.flush()
Assert.eq(path, WORK .. "/de34d9/3086.epub")
Assert.eq(#downloads, n, "已有文件不再下载")
end
-- 缓存一段:单卷失败不阻断,统计已缓存 / 失败。
do
down_body = "PK\3\4epub"
local toc = {
{ uid = "a", origin = "o" }, { uid = "b", origin = "o", building = true }, { uid = "c", origin = "o" },
}
local result
Chapter.cacheAsync(fake, identity, toc, 0, 3, nil, function(cached, total, failed, err)
result = { cached, total, failed, err }
end)
Stubs.flush()
Assert.eq(result[1], 2)
Assert.eq(result[2], 3)
Assert.eq(result[3], 1)
Assert.matches(result[4], "制作中")
end
os.execute("rm -rf '" .. WORK .. "'")