✨ (kmoe): 新增 Kmoe 漫画书源,按卷下载 EPUB 阅读
书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。 支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致, 会话 UA 随 Cookie 一起保存。
This commit is contained in:
commit
1dd1cccb68
24 files changed
+2522
No files matched your search
@@ -0,0 +1,5 @@
|
||||
local _ = require("gettext")
|
||||
return {
|
||||
fullname = _("月读书源:Kmoe"),
|
||||
description = _("给月读增加「Kmoe」漫画书源。需要同时安装月读,与平台官方无关。"),
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
--[[--
|
||||
KOReader 只认带 main.lua 的插件目录;书源本身不需要任何插件钩子,月读读取旁边的 moon_source.lua。
|
||||
--]]
|
||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
|
||||
return WidgetContainer:extend{
|
||||
name = "moonsource_kmoe",
|
||||
is_doc_only = false,
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
--[[--
|
||||
月读第三方书源描述:Kmoe。契约见月读 examples/moonsource-demo.koplugin/moon_source.lua。
|
||||
--]]
|
||||
|
||||
local function lazy(module)
|
||||
return setmetatable({}, { __index = function(_, key) return require(module)[key] end })
|
||||
end
|
||||
|
||||
return {
|
||||
api = 1,
|
||||
provides = {
|
||||
source = true,
|
||||
},
|
||||
meta = function() return require("moonsource_kmoe.source").meta() end,
|
||||
new = function() return require("moonsource_kmoe.source").new() end,
|
||||
setting = lazy("moonsource_kmoe.setting"),
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
# Kmoe
|
||||
|
||||
| 能力 | 是否支持 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| 邮箱密码登录 | 支持 | 保存前打开「我的」页校验账号;不保存密码;用插件固定的电脑版 UA 登录 |
|
||||
| 浏览器授权 | 支持 | 需要能回传浏览器 UA 的新版 moon-cookie-storage 扩展;旧版扩展授权后只能浏览、同步书架,不能下载 |
|
||||
| 粘贴 Cookie | 支持 | 要同时填同一浏览器的 User-Agent,不填只能浏览、同步书架,不能下载 |
|
||||
| 扫码、验证码登录 | 不支持 | 网站登录页没有这两种方式 |
|
||||
| 搜索与书城 | 支持 | 排序、连载状态、语言、分类筛选,每页 21 部,默认聚合约 60 部;无需登录 |
|
||||
| 详情 | 支持 | 作者、分类、简介、封面;无需登录 |
|
||||
| 卷列表 | 支持 | 需要登录;一卷是一章,单行本、话、番外按网站顺序排列 |
|
||||
| 漫画阅读 | 支持 | 整卷下载 EPUB(普通线路);网站要求账号先验证手机号,并消耗账号下载额度;下载要求 UA 与登录时完全一致 |
|
||||
| 卷缓存与离线阅读 | 支持 | 只在打开或手动缓存时下载,不做阅读期预取;已下载的卷离线可读;批量缓存未实测 |
|
||||
| 书架拉取 | 支持 | 网站「收藏」即书架,只订阅不收藏的书不算;收藏页出现分页时不按缺席下架 |
|
||||
| 书架新增与移出 | 支持 | 改网站收藏,之后重新拉取书架确认 |
|
||||
| 阅读进度同步 | 不支持 | 网站没有阅读进度接口,只保存本地进度 |
|
||||
| 笔记与统计同步 | 不支持 | 仅本地保存 |
|
||||
| 书评 | 不支持 | 网站有整书书评,插件未接入 |
|
||||
| 章评、段评与回复 | 不支持 | 网站只有整书书评 |
|
||||
| 账号切换 | 已实现,未实测 | 按数字账号区分;换号时清掉旧账号的本地书架和阅读记录 |
|
||||
@@ -0,0 +1,15 @@
|
||||
--[[--
|
||||
书源元信息:id / 名称 / 类型,加上启用页展示用的说明与能力清单。
|
||||
|
||||
@module moonsource_kmoe._meta
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_kmoe")
|
||||
|
||||
return {
|
||||
id = "kmoe",
|
||||
name = _("Kmoe"),
|
||||
type = "chapter",
|
||||
description = _("Kmoe 漫画,按卷下载 EPUB 阅读"),
|
||||
abilities = { _("搜索与发现"), _("书架同步") },
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
--[[--
|
||||
Kmoe 一卷 = 一章:取下载地址,把整卷 EPUB 下到书目录,按卷号命名。
|
||||
|
||||
一卷几十 MB,而且占用账号的下载额度,所以只在打开或用户点缓存时下载,不做阅读期预取。
|
||||
|
||||
@module moonsource_kmoe.chapter
|
||||
--]]
|
||||
|
||||
local Paths = require("utils.paths")
|
||||
local _ = require("l10n").get("moonsource_kmoe")
|
||||
|
||||
local Chapter = {}
|
||||
|
||||
---@param identity BookIdentity
|
||||
---@param chapter BookChapter
|
||||
---@return string
|
||||
function Chapter.path(identity, chapter)
|
||||
return Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/" .. chapter.uid .. ".epub"
|
||||
end
|
||||
|
||||
--- 文件是完整的 EPUB(zip 头)。
|
||||
---@param path string
|
||||
---@return boolean
|
||||
local function isEpub(path)
|
||||
local f = io.open(path, "rb")
|
||||
if not f then return false end
|
||||
local head = f:read(4)
|
||||
f:close()
|
||||
return head == "PK\3\4"
|
||||
end
|
||||
|
||||
--- 已下载好的卷;没有则 nil。
|
||||
---@param identity BookIdentity
|
||||
---@param chapter BookChapter
|
||||
---@return string|nil
|
||||
function Chapter.existing(identity, chapter)
|
||||
local path = Chapter.path(identity, chapter)
|
||||
return isEpub(path) and path or nil
|
||||
end
|
||||
|
||||
--- 下载一卷;已有文件直接复用。
|
||||
---@param client KmoeClient
|
||||
---@param identity BookIdentity
|
||||
---@param chapter KmoeChapter
|
||||
---@param on_progress fun(bytes: number, total: number|nil)|nil
|
||||
---@param cb fun(path: string|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Chapter.materializeAsync(client, identity, chapter, on_progress, cb)
|
||||
local SourceBase = require("source.base")
|
||||
local path = Chapter.existing(identity, chapter)
|
||||
if path then return SourceBase.defer(cb, path) end
|
||||
if chapter.building then
|
||||
return SourceBase.defer(cb, nil, _("这一卷的 EPUB 还在制作中,过一阵再试"))
|
||||
end
|
||||
Paths.ensureBookWork(identity.stable_id, identity.source_id)
|
||||
path = Chapter.path(identity, chapter)
|
||||
local handle = {}
|
||||
handle.inner = client:downUrlAsync(chapter.origin, function(url, err)
|
||||
if handle.cancelled then return end
|
||||
if not url then cb(nil, err); return end
|
||||
handle.inner = require("http.request").download({
|
||||
url = url,
|
||||
headers = { ["User-Agent"] = client.ua, ["Referer"] = client.BASE .. "/" },
|
||||
timeout = require("http.timeout").net(20),
|
||||
max_bytes = 1024 * 1024 * 1024,
|
||||
resume = true,
|
||||
on_progress = on_progress,
|
||||
}, path, function(ok, download_err)
|
||||
if handle.cancelled then return end
|
||||
if not ok then cb(nil, download_err or _("下载失败")); return end
|
||||
if not isEpub(path) then
|
||||
os.remove(path)
|
||||
cb(nil, _("Kmoe 返回的不是 EPUB 文件"))
|
||||
return
|
||||
end
|
||||
cb(path)
|
||||
end)
|
||||
end)
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
--- 顺序下载 from_idx+1 起的 count 卷;单卷失败不阻断后面。
|
||||
---@param client KmoeClient
|
||||
---@param identity BookIdentity
|
||||
---@param toc KmoeChapter[]
|
||||
---@param from_idx integer
|
||||
---@param count integer
|
||||
---@param progress fun(done: integer, total: integer)|nil
|
||||
---@param cb fun(cached: integer, total: integer, failed: integer, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Chapter.cacheAsync(client, identity, toc, from_idx, count, progress, cb)
|
||||
local handle, cached, failed, last_err = {}, 0, 0, nil
|
||||
local last = math.min(#toc, from_idx + count)
|
||||
local total = math.max(0, last - from_idx)
|
||||
local idx = from_idx
|
||||
local function step()
|
||||
if handle.cancelled then return end
|
||||
idx = idx + 1
|
||||
if idx > last then cb(cached, total, failed, last_err); return end
|
||||
handle.inner = Chapter.materializeAsync(client, identity, toc[idx], nil, function(path, err)
|
||||
if handle.cancelled then return end
|
||||
if path then cached = cached + 1 else failed, last_err = failed + 1, err end
|
||||
if progress then progress(cached + failed, total) end
|
||||
step()
|
||||
end)
|
||||
end
|
||||
step()
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
return Chapter
|
||||
@@ -0,0 +1,257 @@
|
||||
--[[--
|
||||
Kmoe(mox.moe)HTTP 客户端。
|
||||
|
||||
书单 / 搜索走公开 JSON `data_list.php`,不带账号 Cookie;详情、卷列表、书架、收藏、下载带登录 Cookie。
|
||||
需要登录的页面在未登录时 302 到 login.php;没做手机验证的账号取下载地址会 302 到 regphone.php。
|
||||
请求一律不跟随跳转,跳转目标就是错误类型。
|
||||
|
||||
取下载地址要求 UA 与登录时逐字一致,否则 403(普通页面不查)。所以会话的 UA 跟 Cookie 一起保存:
|
||||
密码登录用这里的固定电脑版 UA;浏览器授权和粘贴 Cookie 用那个浏览器自己的 UA。
|
||||
|
||||
@module moonsource_kmoe.client
|
||||
--]]
|
||||
|
||||
local Mapper = require("moonsource_kmoe.mapper")
|
||||
local Request = require("http.request")
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_kmoe")
|
||||
|
||||
local SOURCE_ID = "kmoe"
|
||||
local BASE = "https://mox.moe"
|
||||
local UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
|
||||
---@class KmoeClient
|
||||
---@field jar CookieJar|nil 登录会话;未登录为 nil
|
||||
---@field ua string 这份会话登录时的 UA
|
||||
local Client = {}
|
||||
Client.__index = Client
|
||||
Client.BASE = BASE
|
||||
Client.ua = UA
|
||||
|
||||
---@param cookie string|nil 已保存的登录 Cookie
|
||||
---@param ua string|nil 登录时的 UA;nil 为密码登录用的默认 UA
|
||||
---@return KmoeClient
|
||||
function Client.new(cookie, ua)
|
||||
return setmetatable({ jar = require("http.cookie_jar").session(SOURCE_ID, cookie), ua = ua }, Client)
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Client:loggedIn()
|
||||
return self.jar ~= nil
|
||||
end
|
||||
|
||||
--- 需要登录却没登录(或登录已失效)时的错误。
|
||||
---@return string
|
||||
function Client:loginError()
|
||||
if not self.jar then return _("请先登录 Kmoe 账号") end
|
||||
return require("source.auth").expire(SOURCE_ID,
|
||||
_("Kmoe 登录已失效。打开「设置 → 书库 → 账号 → Kmoe」,重新登录。"))
|
||||
end
|
||||
|
||||
--- 站内请求。body 非 nil 时 POST 表单。成功 cb(body, nil, res)。
|
||||
---@param path string 站内路径(/ 开头)
|
||||
---@param opts { form: table|nil, auth: boolean|nil, jar: CookieJar|nil }
|
||||
---@param cb fun(body: string|nil, err: string|nil, res: table|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:request(path, opts, cb)
|
||||
local jar = opts.jar or (opts.auth and self.jar or nil)
|
||||
local headers = { ["User-Agent"] = self.ua, ["Referer"] = BASE .. "/" }
|
||||
local function done(body, err, res)
|
||||
if body then cb(body, nil, res); return end
|
||||
local code = res and res.code or 0
|
||||
local location = code >= 300 and code < 400 and Request.header(res, "Location") or ""
|
||||
if location:find("regphone", 1, true) then
|
||||
cb(nil, _("Kmoe 要求先验证手机号才能下载:在 Kmoe 网站「設置 → 帳號信息 → 手機驗證」完成验证,再回月读重新登录。"), res)
|
||||
elseif location:find("login", 1, true) then
|
||||
cb(nil, self:loginError(), res)
|
||||
elseif location ~= "" then
|
||||
cb(nil, _("Kmoe 返回了意外的跳转"), res)
|
||||
else
|
||||
cb(nil, err or _("网络请求失败"), res)
|
||||
end
|
||||
end
|
||||
local http = { headers = headers, jar = jar, timeout = require("http.timeout").net(1) }
|
||||
if opts.form then
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
return Request.post(BASE .. path, Text.formEncode(opts.form), http, done)
|
||||
end
|
||||
return Request.get(BASE .. path, http, done)
|
||||
end
|
||||
|
||||
---@class KmoeListQuery
|
||||
---@field s string 关键词,或 `CAT*<分类>`
|
||||
---@field ["end"] string|nil 完結 / 連載
|
||||
---@field lang string|nil chn / jpn / eng
|
||||
---@field by string|nil 排序
|
||||
|
||||
--- 书单一页(每页 21 部)。
|
||||
---@param query KmoeListQuery
|
||||
---@param page integer
|
||||
---@param cb fun(books: Book[]|nil, err: string|nil, more: boolean|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:listAsync(query, page, cb)
|
||||
local q = {
|
||||
k = "", s = query.s or "", ["end"] = query["end"] or "", lang = query.lang or "",
|
||||
blen = "", regn = "", by = query.by or "", color = "0", hd = "0", p = tostring(page),
|
||||
}
|
||||
return self:request("/data_list.php?" .. Text.formEncode(q), {}, function(body, err)
|
||||
if not body then cb(nil, err); return end
|
||||
cb(Mapper.list(body))
|
||||
end)
|
||||
end
|
||||
|
||||
--- 手机版详情页。登录时带 Cookie,页面里的 uin 用来判断登录是否还有效。
|
||||
---@param key string
|
||||
---@param cb fun(detail: KmoeDetail|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:detailAsync(key, cb)
|
||||
return self:request("/m/c/" .. key .. ".htm", { auth = true }, function(body, err)
|
||||
if not body then cb(nil, err); return end
|
||||
local detail = Mapper.detail(key, body)
|
||||
if not detail then cb(nil, _("Kmoe 漫画详情解析失败")); return end
|
||||
cb(detail)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 卷列表。匿名请求服务器回空列表,所以先看详情页是否按登录态渲染。
|
||||
---@param detail KmoeDetail
|
||||
---@param cb fun(chapters: BookChapter[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:volumesAsync(detail, cb)
|
||||
if not detail.logged_in then return require("source.base").defer(cb, nil, self:loginError()) end
|
||||
if not detail.hash then return require("source.base").defer(cb, nil, _("Kmoe 漫画详情解析失败")) end
|
||||
return self:request("/data_book.php?h=" .. detail.hash, { auth = true }, function(body, err)
|
||||
if not body then cb(nil, err); return end
|
||||
cb(Mapper.chapters(body, detail.bookid))
|
||||
end)
|
||||
end
|
||||
|
||||
--- 取一卷的文件地址。
|
||||
---@param origin string 目录里存的 getdownurl 路径
|
||||
---@param cb fun(url: string|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:downUrlAsync(origin, cb)
|
||||
if not self.jar then return require("source.base").defer(cb, nil, self:loginError()) end
|
||||
return self:request(origin, { auth = true }, function(body, err, res)
|
||||
if res and res.code == 403 then
|
||||
cb(nil, _("Kmoe 拒绝下载:登录时的浏览器和现在不一致。改用邮箱密码重新登录;浏览器授权要用新版扩展,粘贴 Cookie 要同时填浏览器 UA。"))
|
||||
return
|
||||
end
|
||||
if not body then cb(nil, err); return end
|
||||
local url, parse_err = Mapper.downUrl(body)
|
||||
cb(url and Text.absoluteUrl(BASE .. "/", url) or nil, parse_err)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 书架快照:「訂閱收藏」页里打了收藏的书。没有封面的新书顺序补拉详情页拿封面和简介,补拉失败只少封面。
|
||||
---@param cb fun(books: Book[]|nil, err: string|nil, meta: { incomplete: boolean }|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:shelfSyncAsync(cb)
|
||||
local handle = {}
|
||||
if not self.jar then return require("source.base").defer(cb, nil, self:loginError()) end
|
||||
handle.inner = self:request("/myfollow.php", { auth = true }, function(body, err)
|
||||
if handle.cancelled then return end
|
||||
if not body then cb(nil, err); return end
|
||||
local rows, parse_err, paged = Mapper.shelf(body)
|
||||
if not rows then cb(nil, parse_err); return end
|
||||
local BookDB = require("book.books_db")
|
||||
local books, pos = {}, 0
|
||||
for i, row in ipairs(rows) do books[i] = row.book end
|
||||
local function step()
|
||||
if handle.cancelled then return end
|
||||
pos = pos + 1
|
||||
local book = books[pos]
|
||||
if not book then cb(books, nil, { incomplete = paged }); return end
|
||||
local known = BookDB.get(SOURCE_ID, book.stable_id)
|
||||
if known and known.cover then return step() end
|
||||
handle.inner = self:detailAsync(book.stable_id, function(detail, detail_err)
|
||||
if detail then
|
||||
book.cover, book.intro, book.category = detail.book.cover, detail.book.intro, detail.book.category
|
||||
else
|
||||
require("utils.log").warn("kmoe shelf cover backfill failed", book.stable_id, detail_err)
|
||||
end
|
||||
step()
|
||||
end)
|
||||
end
|
||||
step()
|
||||
end)
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
--- 收藏 / 取消收藏。接口要数字书号,先读详情页;成功回包里有 `c101`。
|
||||
---@param key string
|
||||
---@param act string "1" 收藏 / "0" 取消
|
||||
---@param cb fun(ok: boolean|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:followAsync(key, act, cb)
|
||||
local handle = {}
|
||||
handle.inner = self:detailAsync(key, function(detail, err)
|
||||
if handle.cancelled then return end
|
||||
if not detail then cb(nil, err); return end
|
||||
if not detail.logged_in then cb(nil, self:loginError()); return end
|
||||
handle.inner = self:request("/book_follow.php", {
|
||||
auth = true,
|
||||
form = { follow_bookid = detail.bookid, follow_type = "0", follow_act = act },
|
||||
}, function(body, follow_err)
|
||||
if handle.cancelled then return end
|
||||
if not body then cb(nil, follow_err); return end
|
||||
if not body:find('"c101"', 1, true) then cb(nil, _("Kmoe 没有确认收藏操作")); return end
|
||||
cb(true)
|
||||
end)
|
||||
end)
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
function Client:addToShelfAsync(key, cb)
|
||||
return self:followAsync(key, "1", cb)
|
||||
end
|
||||
|
||||
function Client:removeFromShelfAsync(key, cb)
|
||||
return self:followAsync(key, "0", cb)
|
||||
end
|
||||
|
||||
--- 校验一份 Cookie:带它打开「我的」页,拿个人主页链接 `/u/<uin>/` 里的数字账号。
|
||||
---@param cookie string
|
||||
---@param cb fun(ok: boolean, err: string|nil, account: { id: string }|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.accountAsync(cookie, cb)
|
||||
local client = setmetatable({ jar = require("http.cookie_jar").new():seed(cookie) }, Client)
|
||||
return client:request("/my.php", { auth = true }, function(body, err, res)
|
||||
local uin = body and body:match("/u/(%d+)/")
|
||||
if uin then cb(true, nil, { id = uin }); return end
|
||||
local code = res and res.code or 0
|
||||
if body or (code >= 300 and code < 400) then cb(false, _("Kmoe 登录无效,请重新登录")); return end
|
||||
cb(false, err)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 账号密码登录,成功回登录 Cookie 串(还要经 accountAsync 校验)。密码不落盘。
|
||||
---@param email string
|
||||
---@param password string
|
||||
---@param cb fun(cookie: string|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.loginAsync(email, password, cb)
|
||||
local client = setmetatable({}, Client)
|
||||
return client:request("/login_act.php", { form = { email = email, passwd = password } }, function(body, err, res)
|
||||
if not body then cb(nil, err); return end
|
||||
local ok, wire = pcall(require("json").decode, body)
|
||||
if not ok or type(wire) ~= "table" then cb(nil, _("Kmoe 登录响应无效")); return end
|
||||
if wire.msgid ~= "m100" then
|
||||
cb(nil, type(wire.msg) == "string" and wire.msg ~= "" and wire.msg or _("登录失败"))
|
||||
return
|
||||
end
|
||||
local cookie = require("source.cookie_login").parse("", Request.headerAll(res, "Set-Cookie"))
|
||||
if cookie == "" then cb(nil, _("Kmoe 登录成功但没有返回 Cookie")); return end
|
||||
cb(cookie)
|
||||
end)
|
||||
end
|
||||
|
||||
return Client
|
||||
@@ -0,0 +1,62 @@
|
||||
return {
|
||||
["Kmoe"] = "Kmoe",
|
||||
["Kmoe 下载地址响应无效"] = "Invalid download link response from Kmoe",
|
||||
["Kmoe 书单响应无效"] = "Invalid list response from Kmoe",
|
||||
["Kmoe 书架页面无法识别"] = "Could not read the Kmoe bookshelf page",
|
||||
["Kmoe 卷列表响应无效"] = "Invalid volume list response from Kmoe",
|
||||
["Kmoe 没有确认收藏操作"] = "Kmoe did not confirm the favorite change",
|
||||
["Kmoe 没有返回下载地址"] = "Kmoe returned no download link",
|
||||
["Kmoe 没有返回这部漫画的卷列表"] = "Kmoe returned no volume list for this comic",
|
||||
["Kmoe 漫画详情解析失败"] = "Failed to parse Kmoe comic details",
|
||||
["Kmoe 漫画,按卷下载 EPUB 阅读"] = "Kmoe comics, read by downloading each volume as EPUB",
|
||||
["Kmoe 登录响应无效"] = "Invalid sign-in response from Kmoe",
|
||||
["Kmoe 登录已失效。打开「设置 → 书库 → 账号 → Kmoe」,重新登录。"] = "Your Kmoe sign-in has expired. Open Settings → Library → Account → Kmoe and sign in again.",
|
||||
["Kmoe 登录成功但没有返回 Cookie"] = "Kmoe sign-in succeeded but returned no cookie",
|
||||
["Kmoe 登录无效,请重新登录"] = "Kmoe sign-in is invalid. Please sign in again",
|
||||
["Kmoe 要求先验证手机号才能下载:在 Kmoe 网站「設置 → 帳號信息 → 手機驗證」完成验证,再回月读重新登录。"] = "Kmoe requires phone verification before downloading. Verify your phone on the Kmoe website (設置 → 帳號信息 → 手機驗證), then sign in again in Moon.",
|
||||
["Kmoe 账号"] = "Kmoe account",
|
||||
["Kmoe 返回了意外的跳转"] = "Kmoe returned an unexpected redirect",
|
||||
["Kmoe 返回的不是 EPUB 文件"] = "Kmoe did not return an EPUB file",
|
||||
["下载失败"] = "Download failed",
|
||||
["中文"] = "Chinese",
|
||||
["书架同步"] = "Shelf sync",
|
||||
["全部分类"] = "All categories",
|
||||
["全部状态"] = "Any status",
|
||||
["全部语言"] = "Any language",
|
||||
["分类"] = "Category",
|
||||
["完结"] = "Completed",
|
||||
["密码"] = "Password",
|
||||
["已登录"] = "Signed in",
|
||||
["排序"] = "Sort",
|
||||
["搜索与发现"] = "Search and discover",
|
||||
["新上架"] = "Newly added",
|
||||
["日文"] = "Japanese",
|
||||
["最近更新"] = "Recently updated",
|
||||
["未登录 · 点此登录"] = "Not signed in · Tap to sign in",
|
||||
["正在下载这一卷…"] = "Downloading this volume…",
|
||||
["正在准备漫画…"] = "Preparing comic…",
|
||||
["正在登录…"] = "Signing in…",
|
||||
["漫画目录保存失败"] = "Failed to save comic chapter list",
|
||||
["热门"] = "Popular",
|
||||
["状态"] = "Status",
|
||||
["登录"] = "Sign in",
|
||||
["登录失败"] = "Sign-in failed",
|
||||
["登录成功"] = "Signed in",
|
||||
["确定退出 Kmoe 账号?"] = "Sign out of Kmoe?",
|
||||
["综合"] = "Overall",
|
||||
["网络不可用,请先连接 Wi-Fi"] = "Network unavailable. Connect to Wi-Fi first",
|
||||
["网络请求失败"] = "Network request failed",
|
||||
["英文"] = "English",
|
||||
["评分"] = "Rating",
|
||||
["语言"] = "Language",
|
||||
["请先登录 Kmoe 账号"] = "Sign in to Kmoe first",
|
||||
["请输入邮箱和密码"] = "Enter email and password",
|
||||
["这一卷的 EPUB 还在制作中,过一阵再试"] = "The EPUB for this volume is still being made. Try again later",
|
||||
["这部漫画还没有可下载的卷"] = "This comic has no downloadable volumes yet",
|
||||
["连载"] = "Ongoing",
|
||||
["邮箱"] = "Email",
|
||||
["Kmoe 登录 Cookie"] = "Kmoe sign-in cookie",
|
||||
["同一浏览器的 User-Agent(选填,不填不能下载)"] = "User-Agent of the same browser (optional; required for downloads)",
|
||||
["Kmoe 拒绝下载:登录时的浏览器和现在不一致。改用邮箱密码重新登录;浏览器授权要用新版扩展,粘贴 Cookie 要同时填浏览器 UA。"] = "Kmoe refused the download: this session was signed in from a different browser. Sign in again with email and password; browser sign-in needs the latest extension, and a pasted cookie needs the browser User-Agent too.",
|
||||
["在 mox.moe 登录后,复制请求标头里的完整 Cookie"] = "Sign in on mox.moe, then copy the full Cookie request header",
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
return {
|
||||
["Kmoe"] = "Kmoe",
|
||||
["Kmoe 下载地址响应无效"] = "Kmoe 下載地址回應無效",
|
||||
["Kmoe 书单响应无效"] = "Kmoe 書單回應無效",
|
||||
["Kmoe 书架页面无法识别"] = "無法識別 Kmoe 書架頁面",
|
||||
["Kmoe 卷列表响应无效"] = "Kmoe 卷列表回應無效",
|
||||
["Kmoe 没有确认收藏操作"] = "Kmoe 沒有確認收藏操作",
|
||||
["Kmoe 没有返回下载地址"] = "Kmoe 沒有返回下載地址",
|
||||
["Kmoe 没有返回这部漫画的卷列表"] = "Kmoe 沒有返回這部漫畫的卷列表",
|
||||
["Kmoe 漫画详情解析失败"] = "Kmoe 漫畫詳情解析失敗",
|
||||
["Kmoe 漫画,按卷下载 EPUB 阅读"] = "Kmoe 漫畫,按卷下載 EPUB 閱讀",
|
||||
["Kmoe 登录响应无效"] = "Kmoe 登入回應無效",
|
||||
["Kmoe 登录已失效。打开「设置 → 书库 → 账号 → Kmoe」,重新登录。"] = "Kmoe 登入已失效。打開「設定 → 書庫 → 帳號 → Kmoe」,重新登入。",
|
||||
["Kmoe 登录成功但没有返回 Cookie"] = "Kmoe 登入成功但沒有返回 Cookie",
|
||||
["Kmoe 登录无效,请重新登录"] = "Kmoe 登入無效,請重新登入",
|
||||
["Kmoe 要求先验证手机号才能下载:在 Kmoe 网站「設置 → 帳號信息 → 手機驗證」完成验证,再回月读重新登录。"] = "Kmoe 要求先驗證手機號碼才能下載:在 Kmoe 網站「設置 → 帳號信息 → 手機驗證」完成驗證,再回月讀重新登入。",
|
||||
["Kmoe 账号"] = "Kmoe 帳號",
|
||||
["Kmoe 返回了意外的跳转"] = "Kmoe 返回了意外的跳轉",
|
||||
["Kmoe 返回的不是 EPUB 文件"] = "Kmoe 返回的不是 EPUB 檔案",
|
||||
["下载失败"] = "下載失敗",
|
||||
["中文"] = "中文",
|
||||
["书架同步"] = "書架同步",
|
||||
["全部分类"] = "全部分類",
|
||||
["全部状态"] = "全部狀態",
|
||||
["全部语言"] = "全部語言",
|
||||
["分类"] = "分類",
|
||||
["完结"] = "完結",
|
||||
["密码"] = "密碼",
|
||||
["已登录"] = "已登入",
|
||||
["排序"] = "排序",
|
||||
["搜索与发现"] = "搜尋與發現",
|
||||
["新上架"] = "新上架",
|
||||
["日文"] = "日文",
|
||||
["最近更新"] = "最近更新",
|
||||
["未登录 · 点此登录"] = "未登入 · 點此登入",
|
||||
["正在下载这一卷…"] = "正在下載這一卷…",
|
||||
["正在准备漫画…"] = "正在準備漫畫…",
|
||||
["正在登录…"] = "正在登入…",
|
||||
["漫画目录保存失败"] = "漫畫目錄儲存失敗",
|
||||
["热门"] = "熱門",
|
||||
["状态"] = "狀態",
|
||||
["登录"] = "登入",
|
||||
["登录失败"] = "登入失敗",
|
||||
["登录成功"] = "登入成功",
|
||||
["确定退出 Kmoe 账号?"] = "確定登出 Kmoe 帳號?",
|
||||
["综合"] = "綜合",
|
||||
["网络不可用,请先连接 Wi-Fi"] = "網路不可用,請先連接 Wi-Fi",
|
||||
["网络请求失败"] = "網路請求失敗",
|
||||
["英文"] = "英文",
|
||||
["评分"] = "評分",
|
||||
["语言"] = "語言",
|
||||
["请先登录 Kmoe 账号"] = "請先登入 Kmoe 帳號",
|
||||
["请输入邮箱和密码"] = "請輸入信箱和密碼",
|
||||
["这一卷的 EPUB 还在制作中,过一阵再试"] = "這一卷的 EPUB 還在製作中,過一陣再試",
|
||||
["这部漫画还没有可下载的卷"] = "這部漫畫還沒有可下載的卷",
|
||||
["连载"] = "連載",
|
||||
["邮箱"] = "信箱",
|
||||
["Kmoe 登录 Cookie"] = "Kmoe 登入 Cookie",
|
||||
["同一浏览器的 User-Agent(选填,不填不能下载)"] = "同一瀏覽器的 User-Agent(選填,不填無法下載)",
|
||||
["Kmoe 拒绝下载:登录时的浏览器和现在不一致。改用邮箱密码重新登录;浏览器授权要用新版扩展,粘贴 Cookie 要同时填浏览器 UA。"] = "Kmoe 拒絕下載:登入時的瀏覽器和現在不一致。改用信箱密碼重新登入;瀏覽器授權需要新版擴充功能,貼上 Cookie 需要同時填瀏覽器 UA。",
|
||||
["在 mox.moe 登录后,复制请求标头里的完整 Cookie"] = "在 mox.moe 登入後,複製請求標頭裡的完整 Cookie",
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
return function()
|
||||
local copy = {}
|
||||
for k, v in pairs(require("moonsource_kmoe._meta")) do copy[k] = v end
|
||||
return copy
|
||||
end
|
||||
@@ -0,0 +1,179 @@
|
||||
--[[--
|
||||
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
|
||||
@@ -0,0 +1,161 @@
|
||||
--[[--
|
||||
Kmoe 账号:邮箱密码登录、浏览器授权、粘贴 Cookie 三种方式,最后都只保存校验通过的 Cookie、数字账号和登录时的 UA,不存密码。
|
||||
|
||||
下载接口要求 UA 与登录时一致,所以每种方式都带上自己的 UA:密码登录用插件默认 UA(存 nil),
|
||||
浏览器授权用扩展回传的 `payload.ua`,粘贴 Cookie 用用户一并填的浏览器 UA。
|
||||
|
||||
@module moonsource_kmoe.setting
|
||||
--]]
|
||||
|
||||
local CookieLogin = require("source.cookie_login")
|
||||
local _ = require("l10n").get("moonsource_kmoe")
|
||||
|
||||
local SOURCE_ID = "kmoe"
|
||||
local Setting = {}
|
||||
|
||||
local function loggedIn()
|
||||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||
return type(cfg.cookie) == "string" and cfg.cookie ~= ""
|
||||
end
|
||||
|
||||
---@type CookieLoginSpec
|
||||
local SPEC = {
|
||||
id = SOURCE_ID,
|
||||
title = _("Kmoe 账号"),
|
||||
cookie_title = _("Kmoe 登录 Cookie"),
|
||||
cookie_hint = _("在 mox.moe 登录后,复制请求标头里的完整 Cookie"),
|
||||
logout_text = _("确定退出 Kmoe 账号?"),
|
||||
loggedIn = loggedIn,
|
||||
checkAsync = function(cookie, cb)
|
||||
return require("moonsource_kmoe.client").accountAsync(cookie, cb)
|
||||
end,
|
||||
---@param account { id: string } checkAsync 成功时必带
|
||||
commit = function(cookie, account, cb, current)
|
||||
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, cb, current)
|
||||
end,
|
||||
clear = function(cfg) cfg.cookie, cfg.account_id, cfg.ua = nil, nil, nil end,
|
||||
}
|
||||
|
||||
--- 带登录 UA 的 SPEC:UA 先写进配置再提交 Cookie(提交里会重载源),提交失败就还原。
|
||||
---@param ua string|nil nil 为插件默认 UA
|
||||
---@return CookieLoginSpec
|
||||
local function withUA(ua)
|
||||
return setmetatable({
|
||||
commit = function(cookie, account, cb, current)
|
||||
local Settings = require("utils.settings")
|
||||
local cfg = Settings.getSource(SOURCE_ID)
|
||||
local previous = cfg.ua
|
||||
cfg.ua = ua
|
||||
return CookieLogin.commitAccount(SOURCE_ID, cookie, account.id, function(ok, err)
|
||||
if not ok then
|
||||
cfg.ua = previous
|
||||
Settings.saveSource(SOURCE_ID, cfg)
|
||||
end
|
||||
cb(ok, err)
|
||||
end, current)
|
||||
end,
|
||||
}, { __index = SPEC })
|
||||
end
|
||||
|
||||
---@param s any
|
||||
---@return string|nil
|
||||
local function nonEmpty(s)
|
||||
s = type(s) == "string" and s:match("^%s*(.-)%s*$") or ""
|
||||
return s ~= "" and s or nil
|
||||
end
|
||||
|
||||
---@param email string
|
||||
---@param password string
|
||||
local function doLogin(email, password)
|
||||
local Toast = require("ui.themes.widgets.toast")
|
||||
if email == "" or password == "" then
|
||||
Toast:new{ text = _("请输入邮箱和密码"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
require("ui/network/manager"):runWhenConnected(function()
|
||||
local job
|
||||
local waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = SPEC.title, text = _("正在登录…"), dismissable = false,
|
||||
on_cancel = function() if job then job.cancel() end end,
|
||||
}
|
||||
waiting:show()
|
||||
local function done(ok, err)
|
||||
waiting:close()
|
||||
Toast:new{ text = ok and _("登录成功") or err or _("登录失败"), timeout = ok and 2 or 3 }:show()
|
||||
end
|
||||
job = require("moonsource_kmoe.client").loginAsync(email, password, function(cookie, err)
|
||||
if not cookie then done(false, err); return end
|
||||
job = CookieLogin.save(withUA(nil), cookie, done)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
local function showLogin()
|
||||
require("ui.themes.widgets.input"):new{
|
||||
title = SPEC.title,
|
||||
fields = {
|
||||
{ text = "", hint = _("邮箱") },
|
||||
{ text = "", hint = _("密码"), text_type = "password" },
|
||||
},
|
||||
ok_text = _("登录"),
|
||||
callback = function(email, password) doLogin(email or "", password or "") end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
local function showPaste()
|
||||
require("ui.themes.widgets.input"):new{
|
||||
title = SPEC.cookie_title,
|
||||
fields = {
|
||||
{ text = "", hint = SPEC.cookie_hint },
|
||||
{ text = "", hint = _("同一浏览器的 User-Agent(选填,不填不能下载)") },
|
||||
},
|
||||
ok_text = _("登录"),
|
||||
callback = function(value, ua)
|
||||
local cookie = CookieLogin.parse(value or "")
|
||||
if cookie == "" then
|
||||
require("ui.themes.widgets.toast"):new{ text = _("没有读到 Cookie"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
CookieLogin.verify(withUA(nonEmpty(ua)), cookie)
|
||||
end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
function Setting.rowIcon()
|
||||
return "photo_library"
|
||||
end
|
||||
|
||||
function Setting.rowStatus()
|
||||
local on = CookieLogin.authorized(SPEC)
|
||||
return on and _("已登录") or _("未登录 · 点此登录"), on
|
||||
end
|
||||
|
||||
Setting.browser = {
|
||||
domain = "mox.moe",
|
||||
scope = "cookie",
|
||||
login = "https://mox.moe/login.php",
|
||||
check = "https://mox.moe/my.php",
|
||||
icon = "https://mox.moe/favicon.ico",
|
||||
isAuthorized = function() return CookieLogin.authorized(SPEC) end,
|
||||
apply = function(payload, cb)
|
||||
return CookieLogin.save(withUA(nonEmpty(payload.ua)), CookieLogin.fromPayload(payload, "mox.moe"), cb)
|
||||
end,
|
||||
}
|
||||
|
||||
function Setting.rows()
|
||||
local rows = {
|
||||
CookieLogin.accountRow(SPEC, { callback = showLogin }),
|
||||
CookieLogin.browserRow(),
|
||||
function(iw)
|
||||
return require("ui.themes.widgets.row").widget({ width = iw, chevron = true, kind = "nav",
|
||||
icon = "content_paste", title = _("粘贴 Cookie"), callback = showPaste })
|
||||
end,
|
||||
}
|
||||
if loggedIn() then rows[#rows + 1] = CookieLogin.logoutRow(SPEC) end
|
||||
return rows
|
||||
end
|
||||
|
||||
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
|
||||
Setting.login = showLogin
|
||||
|
||||
return Setting
|
||||
@@ -0,0 +1,293 @@
|
||||
--[[--
|
||||
Kmoe 数据源门面。
|
||||
|
||||
书城与搜索匿名可用;卷列表、下载、书架要登录。一卷是一章,下载整卷 EPUB 阅读。
|
||||
书架是网站的「收藏」,经 Shelf 双向同步。网站没有阅读进度接口,进度只在本机。
|
||||
|
||||
@module moonsource_kmoe.source
|
||||
--]]
|
||||
|
||||
local Browse = require("source.browse")
|
||||
local Client = require("moonsource_kmoe.client")
|
||||
local SourceBase = require("source.base")
|
||||
local Shelf = require("source.shelf")
|
||||
local Toc = require("source.toc")
|
||||
local _ = require("l10n").get("moonsource_kmoe")
|
||||
|
||||
local Kmoe = {}
|
||||
|
||||
---@return BookSourceMeta
|
||||
function Kmoe.meta()
|
||||
return require("moonsource_kmoe.loadmeta")()
|
||||
end
|
||||
|
||||
---@class KmoeSource : SourceBase
|
||||
---@field _client KmoeClient
|
||||
local Source = setmetatable({ shelf_add = true }, { __index = SourceBase })
|
||||
Source.__index = Source
|
||||
|
||||
---@return KmoeSource
|
||||
function Kmoe.new()
|
||||
local cfg = require("utils.settings").getSource("kmoe")
|
||||
local meta = Kmoe.meta()
|
||||
return setmetatable({
|
||||
id = meta.id,
|
||||
name = meta.name,
|
||||
type = meta.type,
|
||||
_client = Client.new(cfg.cookie, cfg.ua),
|
||||
}, Source)
|
||||
end
|
||||
|
||||
--- 已登录才算配置好:书架、卷列表都要登录;书城浏览匿名可用,不看这里。
|
||||
---@return boolean
|
||||
function Source:configured()
|
||||
return self._client:loggedIn()
|
||||
end
|
||||
|
||||
---@param opts { dirty_only?: boolean, force?: boolean, on_progress?: fun(text: string|nil, done: integer|nil, total: integer|nil) }|nil
|
||||
---@param cb fun(result: SyncResult|nil, err: any)
|
||||
---@return { cancel: fun() }
|
||||
function Source:syncBooksAsync(opts, cb)
|
||||
if not self._client:loggedIn() then return SourceBase.syncBooksAsync(self, opts, cb) end
|
||||
return Shelf.syncAsync(self.id, self._client, nil, opts, cb)
|
||||
end
|
||||
|
||||
---@param keyword string
|
||||
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:searchAsync(keyword, cb)
|
||||
return Browse.pages(function(page, done)
|
||||
return self._client:listAsync({ s = keyword }, page, done)
|
||||
end, 63, {}, cb)
|
||||
end
|
||||
|
||||
-- 网站「分類」原文(繁体),既是查询参数也是显示名。
|
||||
local CATEGORIES = {
|
||||
"幽默", "愛情", "競技", "熱血", "冒險", "格鬥", "恐怖", "生存", "懸疑", "偵探", "生活", "勵志",
|
||||
"校園", "職場", "美食", "音樂", "治癒", "歷史", "戰爭", "機戰", "科幻", "魔幻", "魔法", "神鬼",
|
||||
"武俠", "萌系", "耽美", "偽娘", "百合", "後宮", "少女", "少年", "青年", "輕改", "四格",
|
||||
}
|
||||
local SORTS = {
|
||||
{ "sortpoint", _("综合") }, { "score", _("评分") }, { "count_push", _("热门") },
|
||||
{ "newadd", _("新上架") }, { "lastupdate", _("最近更新") },
|
||||
}
|
||||
local STATUSES = { { "", _("全部状态") }, { "連載", _("连载") }, { "完結", _("完结") } }
|
||||
local LANGS = { { "", _("全部语言") }, { "chn", _("中文") }, { "jpn", _("日文") }, { "eng", _("英文") } }
|
||||
|
||||
---@param rows string[][]
|
||||
---@param value string|nil
|
||||
---@return string
|
||||
local function pick(rows, value)
|
||||
for _idx, row in ipairs(rows) do
|
||||
if row[1] == value then return row[1] end
|
||||
end
|
||||
return rows[1][1]
|
||||
end
|
||||
|
||||
--- 书城浏览:节点 `排序,状态,语言,分类`,四段都可空,缺省按综合排序看全部。每页 21 部。
|
||||
---@param opts { node?: string, limit?: integer }
|
||||
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:browseAsync(opts, cb)
|
||||
opts = opts or {}
|
||||
local by, status, lang, cat = (opts.node or ""):match("^([^,]*),([^,]*),([^,]*),(.*)$")
|
||||
local f = { pick(SORTS, by), pick(STATUSES, status), pick(LANGS, lang), "" }
|
||||
for _idx, name in ipairs(CATEGORIES) do
|
||||
if name == cat then f[4] = cat end
|
||||
end
|
||||
local node = table.concat(f, ",")
|
||||
--- 只改第 slot 段的节点。
|
||||
local function with(slot, value)
|
||||
local copy = { f[1], f[2], f[3], f[4] }
|
||||
copy[slot] = value
|
||||
return table.concat(copy, ",")
|
||||
end
|
||||
local function group(slot, rows, title)
|
||||
local chips = {}
|
||||
for i, row in ipairs(rows) do chips[i] = { id = with(slot, row[1]), title = row[2] } end
|
||||
local g = Browse.group(chips, node)
|
||||
g.title = title
|
||||
return g
|
||||
end
|
||||
local cats = { { "", _("全部分类") } }
|
||||
for i, name in ipairs(CATEGORIES) do cats[i + 1] = { name, name } end
|
||||
local groups = {
|
||||
group(1, SORTS, _("排序")), group(2, STATUSES, _("状态")),
|
||||
group(3, LANGS, _("语言")), group(4, cats, _("分类")),
|
||||
}
|
||||
local query = { s = f[4] ~= "" and "CAT*" .. f[4] or "", by = f[1], ["end"] = f[2], lang = f[3] }
|
||||
return Browse.pages(function(page, done)
|
||||
return self._client:listAsync(query, page, done)
|
||||
end, opts.limit or 63, groups, cb)
|
||||
end
|
||||
|
||||
--- 详情不要求登录;登录着且还没有目录缓存时顺手把卷列表存下。
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(book: Book|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:getDetailAsync(identity, cb)
|
||||
local handle = {}
|
||||
handle.inner = self._client:detailAsync(identity.stable_id, function(detail, err)
|
||||
if handle.cancelled then return end
|
||||
if not detail then cb(nil, err); return end
|
||||
local book = detail.book
|
||||
local existing = require("book.books_db").get(self.id, identity.stable_id)
|
||||
if existing then book.deleted = existing.deleted end
|
||||
local function finish()
|
||||
require("book.store").rememberMany({ book })
|
||||
cb(book)
|
||||
end
|
||||
if not detail.logged_in or Toc.read(self.id, identity.stable_id) then return finish() end
|
||||
handle.inner = self._client:volumesAsync(detail, function(chapters)
|
||||
if handle.cancelled then return end
|
||||
if chapters then Toc.put(self.id, identity.stable_id, chapters) end
|
||||
finish()
|
||||
end)
|
||||
end)
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
--- 绕过缓存拉卷列表并写回。
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: KmoeChapter[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:refreshTocAsync(identity, cb)
|
||||
local handle = {}
|
||||
handle.inner = self._client:detailAsync(identity.stable_id, function(detail, err)
|
||||
if handle.cancelled then return end
|
||||
if not detail then cb(nil, err); return end
|
||||
handle.inner = self._client:volumesAsync(detail, function(chapters, vol_err)
|
||||
if handle.cancelled then return end
|
||||
if not chapters then cb(nil, vol_err); return end
|
||||
if not Toc.put(identity.source_id, identity.stable_id, chapters) then
|
||||
cb(nil, _("漫画目录保存失败"))
|
||||
return
|
||||
end
|
||||
cb(chapters)
|
||||
end)
|
||||
end)
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: KmoeChapter[]|nil, err: string|nil)
|
||||
---@param force boolean|nil
|
||||
---@return { cancel: fun() }
|
||||
function Source:loadTocAsync(identity, cb, force)
|
||||
local cached = not force and Toc.read(identity.source_id, identity.stable_id)
|
||||
if cached and #cached > 0 then return SourceBase.defer(cb, cached) end
|
||||
return self:refreshTocAsync(identity, cb)
|
||||
end
|
||||
|
||||
--- 起始卷:显式章号 > 本地进度章号 > 本地进度全书比例 > 第一卷。
|
||||
---@param identity BookIdentity
|
||||
---@param opts table
|
||||
---@param toc KmoeChapter[]
|
||||
---@return integer
|
||||
local function startIndex(identity, opts, toc)
|
||||
local idx = opts.chapter_idx
|
||||
if not idx then
|
||||
local pending = require("book.progress_db").get(identity.source_id, identity.stable_id)
|
||||
idx = pending and (pending.chapter_idx or math.floor(pending.fraction * #toc) + 1)
|
||||
end
|
||||
return math.max(1, math.min(#toc, idx or 1))
|
||||
end
|
||||
|
||||
---@param identity BookIdentity
|
||||
---@param opts table|nil
|
||||
---@param cb fun(path: string|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:openBookAsync(identity, opts, cb)
|
||||
opts = opts or {}
|
||||
local Chapter = require("moonsource_kmoe.chapter")
|
||||
local function finish(path, idx)
|
||||
local ok, err = require("book.store").touch(path, identity, { chapter_idx = idx, book = identity.book })
|
||||
cb(ok and path or nil, err)
|
||||
end
|
||||
|
||||
-- 已下载的卷直接交给阅读器,不经过联网门控。
|
||||
local toc = Toc.read(identity.source_id, identity.stable_id)
|
||||
if toc then
|
||||
local idx = startIndex(identity, opts, toc)
|
||||
local path = Chapter.existing(identity, toc[idx])
|
||||
if path then return SourceBase.defer(finish, path, idx) end
|
||||
end
|
||||
if not require("ui/network/manager"):isConnected() then
|
||||
return SourceBase.defer(cb, nil, _("网络不可用,请先连接 Wi-Fi"))
|
||||
end
|
||||
|
||||
local cancelled, active, dialog = false, nil, nil
|
||||
local function closeDialog()
|
||||
if dialog then dialog:close(); dialog = nil end
|
||||
end
|
||||
require("ui/uimanager"):nextTick(function()
|
||||
if cancelled then return end
|
||||
dialog = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = identity.book and identity.book.title or identity.stable_id,
|
||||
dismissable = false,
|
||||
}
|
||||
dialog:show()
|
||||
dialog:set(_("正在准备漫画…"))
|
||||
active = self:loadTocAsync(identity, function(fresh, err)
|
||||
active = nil
|
||||
if cancelled then return end
|
||||
if not fresh then closeDialog(); cb(nil, err); return end
|
||||
local idx = startIndex(identity, opts, fresh)
|
||||
local mb = 1024 * 1024
|
||||
active = Chapter.materializeAsync(self._client, identity, fresh[idx],
|
||||
function(bytes, total)
|
||||
if dialog and total and total > 0 then
|
||||
dialog:update(_("正在下载这一卷…"), math.floor(bytes / mb), math.ceil(total / mb))
|
||||
end
|
||||
end,
|
||||
function(path, materialize_err)
|
||||
active = nil
|
||||
closeDialog()
|
||||
if cancelled then return end
|
||||
if not path then cb(nil, materialize_err); return end
|
||||
finish(path, idx)
|
||||
end
|
||||
)
|
||||
end)
|
||||
end)
|
||||
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
closeDialog()
|
||||
end }
|
||||
end
|
||||
|
||||
--- 缓存卷(全本或 range 指定的一段);已下载的卷跳过。
|
||||
---@param identity BookIdentity
|
||||
---@param on_progress fun(done: integer, total: integer)|nil
|
||||
---@param cb fun(ok: boolean, cached: integer, err: string|nil, total: integer, failed: integer)
|
||||
---@param range ChapterCacheRange|nil
|
||||
---@return { cancel: fun() }
|
||||
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
|
||||
local handle = {}
|
||||
handle.inner = self:loadTocAsync(identity, function(toc, err)
|
||||
if handle.cancelled then return end
|
||||
if not toc then cb(false, 0, err, 0, 0); return end
|
||||
local from_idx, count = require("source.chapter").cacheRange(toc, range)
|
||||
handle.inner = require("moonsource_kmoe.chapter").cacheAsync(self._client, identity, toc, from_idx, count,
|
||||
on_progress, function(cached, total, failed, last_err)
|
||||
if not handle.cancelled then cb(failed == 0, cached, last_err, total, failed) end
|
||||
end)
|
||||
end)
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
return Kmoe
|
||||
@@ -0,0 +1,4 @@
|
||||
-- 版本号由 tools/package.sh 打包时把占位符换成发布版本;源码运行显示 0.0.0-dev。
|
||||
local VERSION = "@VERSION@"
|
||||
if VERSION:find("^@") then return "0.0.0-dev" end
|
||||
return VERSION
|
||||
Reference in new issue
Block a user