✨ (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

@@ -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