This commit is contained in:
commit
99d94c0d51
36 files changed
+4061
No files matched your search
@@ -0,0 +1,5 @@
|
||||
local _ = require("gettext")
|
||||
return {
|
||||
fullname = _("月读书源:拷贝漫画"),
|
||||
description = _("给月读增加「拷贝漫画」书源。需要同时安装月读,与平台官方无关。"),
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
--[[--
|
||||
KOReader 只认带 main.lua 的插件目录;书源本身不需要任何插件钩子,月读读取旁边的 moon_source.lua。
|
||||
--]]
|
||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
|
||||
return WidgetContainer:extend{
|
||||
name = "moonsource_copymanga",
|
||||
is_doc_only = false,
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
--[[--
|
||||
月读第三方书源描述:拷贝漫画。契约见月读 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_copymanga.source").meta() end,
|
||||
new = function() return require("moonsource_copymanga.source").new() end,
|
||||
setting = lazy("moonsource_copymanga.setting"),
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
# 拷贝漫画
|
||||
|
||||
| 能力 | 是否支持 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| API 站点选择 | 支持 | 建议使用已核对的 `https://api.manga2026.xyz`;保留旧站点及自定义地址,不自动切换 |
|
||||
| 账号密码登录 | 支持 | 原 API 登录,保存前校验账号;新登录不保存密码 |
|
||||
| Cookie、扫码与验证码登录 | 不支持 | 未接入可验证的 API 契约 |
|
||||
| 搜索与书城 | 支持 | 搜索、男/女频日周月总榜及题材,默认聚合约 60 本 |
|
||||
| 详情与分组目录 | 支持 | 分组分页、默认组在前,保留章节 UUID |
|
||||
| 漫画阅读 | 支持 | 按页图顺序下载 CBZ;使用 App API,不解析官网 HTML |
|
||||
| 受限与付费漫画 | 未验证 | 没有真实账号权限验证,不承诺受限章节可读 |
|
||||
| 章节缓存与离线阅读 | 支持 | 复用已缓存 CBZ;未缓存内容离线明确失败 |
|
||||
| 书架拉取 | 支持 | 分页读取收藏 |
|
||||
| 书架新增与移出 | 已实现,未实测 | 本地先生效,收藏上行后回读确认 |
|
||||
| 阅读进度同步 | 部分支持 | 旧 API 保留章节级读写;当前推荐节点未验证写入和回读,只保存本地进度 |
|
||||
| 笔记与统计同步 | 不支持 | 仅本地保存 |
|
||||
| 书评、章评、段评与回复 | 不支持 | 未接入可验证接口,不代表其他客户端没有评论 |
|
||||
| 账号切换 | 部分支持 | 需先退出清理旧数据,未同步记录会丢失;换站点不改变书籍身份和 CBZ 路径 |
|
||||
@@ -0,0 +1,15 @@
|
||||
--[[--
|
||||
书源元信息:id / 名称 / 类型,加上启用页展示用的说明与能力清单。`init.lua` 的 `meta()` 直接返回本表。
|
||||
|
||||
@module moonsource_copymanga._meta
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
return {
|
||||
id = "copymanga",
|
||||
name = _("拷贝漫画"),
|
||||
type = "chapter",
|
||||
description = _("拷贝漫画在线阅读"),
|
||||
abilities = { _("搜索与发现"), _("书架同步"), _("进度同步") },
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
--[[--
|
||||
拷贝漫画账号会话:按所选站点的 API 登录,持久化 Token 与账号密码。
|
||||
|
||||
密码要落盘:令牌被挤掉(HTTP 401)时 `client` 用本地账号自动重登;
|
||||
只存 Token 会在过期后只能弹「重新登录」,整书缓存也会跟着挂。
|
||||
|
||||
登录规则与 kComics / 官方 App 一致:
|
||||
salt = 6 位随机整数
|
||||
password = base64(明文密码 .. "-" .. salt)
|
||||
POST /api/v3/login (x-www-form-urlencoded)
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.auth
|
||||
--]]
|
||||
|
||||
local Client = require("moonsource_copymanga.client")
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
local Auth = {}
|
||||
local SOURCE_ID = "copymanga"
|
||||
|
||||
---@return table
|
||||
local function cfg()
|
||||
return require("utils.settings").getSource(SOURCE_ID)
|
||||
end
|
||||
|
||||
---@param patch table
|
||||
local function save(patch)
|
||||
local settings = require("utils.settings")
|
||||
local current = settings.getSource(SOURCE_ID)
|
||||
for key, value in pairs(patch) do
|
||||
current[key] = value
|
||||
end
|
||||
settings.saveSource(SOURCE_ID, current)
|
||||
end
|
||||
|
||||
--- 站点前端同款密码编码。
|
||||
---@param password string
|
||||
---@param salt string|number
|
||||
---@return string
|
||||
function Auth.encodePassword(password, salt)
|
||||
return Text.base64Encode(tostring(password) .. "-" .. tostring(salt))
|
||||
end
|
||||
|
||||
---@return string
|
||||
local function newSalt()
|
||||
return tostring(math.random(100000, 999999))
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Auth.hasSession()
|
||||
local token = cfg().token
|
||||
return type(token) == "string" and token ~= ""
|
||||
end
|
||||
|
||||
---@return string|nil
|
||||
function Auth.userLabel()
|
||||
local current = cfg()
|
||||
if type(current.nickname) == "string" and current.nickname ~= "" then
|
||||
return current.nickname
|
||||
end
|
||||
if type(current.username) == "string" and current.username ~= "" then
|
||||
return current.username
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- 本地保存的账号密码;缺任一端则没有可自动重登的凭据。
|
||||
---@return string|nil, string|nil
|
||||
function Auth.credentials()
|
||||
local current = cfg()
|
||||
local username = current.username
|
||||
local password = current.password
|
||||
if type(username) == "string" and username ~= ""
|
||||
and type(password) == "string" and password ~= ""
|
||||
then
|
||||
return username, password
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- 清除登录态,保留站点地址。
|
||||
function Auth.clearSession()
|
||||
save({
|
||||
token = "",
|
||||
user_id = "",
|
||||
username = "",
|
||||
nickname = "",
|
||||
password = "",
|
||||
})
|
||||
end
|
||||
|
||||
--- 账号密码登录;成功后写入 token / 用户信息。
|
||||
---@param username string
|
||||
---@param password string
|
||||
---@param cb fun(ok: boolean|nil, err: string|nil, rejected: boolean|nil) rejected = 服务端拒绝(非网络错误)
|
||||
---@return { cancel: fun() }
|
||||
function Auth.loginAsync(username, password, cb)
|
||||
username = Text.stripWhitespace(username)
|
||||
password = tostring(password or "")
|
||||
if username == "" or password == "" then
|
||||
return require("source.base").defer(cb, nil, _("请输入账号和密码"))
|
||||
end
|
||||
|
||||
local current = cfg()
|
||||
local base_url = Client.normalizeBaseUrl(current.base_url)
|
||||
local old_token, old_user_id = current.token, current.user_id
|
||||
local cancelled = false
|
||||
local salt = newSalt()
|
||||
local body = Text.formEncode({
|
||||
username = username,
|
||||
password = Auth.encodePassword(password, salt),
|
||||
salt = salt,
|
||||
})
|
||||
local headers = Client.headers()
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
||||
|
||||
local request = require("http.request").request({
|
||||
url = base_url .. "/api/v3/login",
|
||||
method = "POST",
|
||||
body = body,
|
||||
headers = headers,
|
||||
cache_ttl = 0,
|
||||
allow_redirects = false,
|
||||
timeout = require("http.timeout").net(1),
|
||||
connect_timeout = require("http.timeout").net(1),
|
||||
}, function(res, err)
|
||||
if cancelled then return end
|
||||
local wire, decode_err = Client.decode(res and res.body, err)
|
||||
if not wire then
|
||||
-- 第三个参数:服务端回了包并拒绝(密码错 / 风控),区别于网络错误。
|
||||
cb(nil, decode_err, err == nil and res ~= nil)
|
||||
return
|
||||
end
|
||||
local results = type(wire.results) == "table" and wire.results or {}
|
||||
local token = results.token
|
||||
if type(token) ~= "string" or token == "" then
|
||||
cb(nil, _("登录失败:未返回令牌"), true)
|
||||
return
|
||||
end
|
||||
if cfg().token ~= old_token or Client.normalizeBaseUrl(cfg().base_url) ~= base_url then
|
||||
cb(nil, _("登录状态已改变,请重试"))
|
||||
return
|
||||
end
|
||||
local user_id = tostring(results.user_id or "")
|
||||
if user_id == "" then
|
||||
cb(nil, _("登录失败:未返回账号标识"), true)
|
||||
return
|
||||
end
|
||||
local function commit()
|
||||
save({
|
||||
token = token,
|
||||
user_id = user_id,
|
||||
username = tostring(results.username or username),
|
||||
nickname = tostring(results.nickname or results.username or username),
|
||||
-- 明文密码供 401 自动重登(#42);退出登录时 clearSession 清掉。
|
||||
password = password,
|
||||
})
|
||||
end
|
||||
if old_user_id ~= user_id then
|
||||
if not require("source.logout").replaceLibrary(SOURCE_ID, commit) then
|
||||
cb(nil, _("准备本地同步失败"))
|
||||
return
|
||||
end
|
||||
else
|
||||
commit()
|
||||
end
|
||||
cb(true)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
request.cancel()
|
||||
end }
|
||||
end
|
||||
|
||||
return Auth
|
||||
@@ -0,0 +1,169 @@
|
||||
--[[--
|
||||
拷贝漫画章节图片下载与 CBZ 打包。
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.chapter
|
||||
--]]
|
||||
|
||||
local Cbz = require("source.cbz")
|
||||
local Image = require("utils.image")
|
||||
local Paths = require("utils.paths")
|
||||
local Protocol = require("moonsource_copymanga.protocol")
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
local Chapter = {}
|
||||
|
||||
--- 章节 CBZ 路径。按章节 uuid 命名:多分组漫画更新后连续章序号会整体后移,按序号命名会串章。
|
||||
---@param identity BookIdentity
|
||||
---@param chapter BookChapter
|
||||
---@return string
|
||||
function Chapter.archivePath(identity, chapter)
|
||||
return Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/" .. chapter.uid .. ".cbz"
|
||||
end
|
||||
|
||||
--- 已下载好的章节 CBZ;没有则 nil。
|
||||
---@param identity BookIdentity
|
||||
---@param chapter BookChapter
|
||||
---@return string|nil
|
||||
function Chapter.existing(identity, chapter)
|
||||
local path = Chapter.archivePath(identity, chapter)
|
||||
return Cbz.ready(path) and path or nil
|
||||
end
|
||||
|
||||
--- 下载并打包一个漫画章节;已有 CBZ 直接复用。
|
||||
---@param client CopymangaClient
|
||||
---@param identity BookIdentity
|
||||
---@param chapter BookChapter
|
||||
---@param on_progress fun(done: integer, total: integer)|nil
|
||||
---@param cb fun(path: string|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Chapter.materializeAsync(client, identity, chapter, on_progress, cb)
|
||||
Paths.ensureBookWork(identity.stable_id, identity.source_id)
|
||||
local archive_path = Chapter.archivePath(identity, chapter)
|
||||
---@type { cancelled: boolean, active: CancelHandle|nil, writer: ArchiverWriter|nil }
|
||||
local state = { cancelled = false, active = nil, writer = nil }
|
||||
if Cbz.ready(archive_path) then
|
||||
require("ui/uimanager"):nextTick(require("ui.guard").wrap("copymanga callback", function()
|
||||
if not state.cancelled then cb(archive_path) end
|
||||
end))
|
||||
return { cancel = function() state.cancelled = true end }
|
||||
end
|
||||
|
||||
state.active = client:chapterAsync(identity.stable_id, chapter.uid, function(wire, err)
|
||||
state.active = nil
|
||||
if state.cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
local urls, decode_err = Protocol.chapterImages(wire)
|
||||
if not urls then cb(nil, decode_err); return end
|
||||
|
||||
local image_dir = archive_path:gsub("%.cbz$", "")
|
||||
Paths.ensureDir(image_dir)
|
||||
local paths, current = {}, 1
|
||||
for i, url in ipairs(urls) do
|
||||
paths[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg")
|
||||
end
|
||||
|
||||
local function nextImage()
|
||||
if state.cancelled then return end
|
||||
local url = urls[current]
|
||||
if not url then
|
||||
Cbz.build(paths, archive_path, state, cb)
|
||||
return
|
||||
end
|
||||
local path = paths[current]
|
||||
if Image.ok(path) then
|
||||
if on_progress then on_progress(current, #urls) end
|
||||
current = current + 1
|
||||
require("ui/uimanager"):nextTick(require("ui.guard").wrap("copymanga image", nextImage))
|
||||
return
|
||||
end
|
||||
local mine = current
|
||||
local handle = require("http.request").download({
|
||||
url = url,
|
||||
headers = { ["Referer"] = client.base_url .. "/" },
|
||||
timeout = require("http.timeout").net(3),
|
||||
max_bytes = 32 * 1024 * 1024,
|
||||
}, path, function(ok, download_err)
|
||||
state.active = nil
|
||||
if state.cancelled then return end
|
||||
if not ok or not Image.ok(path) then
|
||||
os.remove(path)
|
||||
cb(nil, download_err or _("漫画图片下载失败"))
|
||||
return
|
||||
end
|
||||
if on_progress then on_progress(current, #urls) end
|
||||
current = current + 1
|
||||
nextImage()
|
||||
end)
|
||||
-- 回调同步触发时已发出下一个请求,不能用旧句柄覆盖它(同 http/github)。
|
||||
if current == mine then state.active = handle end
|
||||
end
|
||||
nextImage()
|
||||
end)
|
||||
|
||||
return { cancel = function()
|
||||
if state.cancelled then return end
|
||||
state.cancelled = true
|
||||
if state.active then state.active.cancel() end
|
||||
if state.writer then state.writer:close() end
|
||||
os.remove(archive_path .. ".part")
|
||||
end }
|
||||
end
|
||||
|
||||
--- 后台预取章节 CBZ;已有文件由 materializeAsync 直接复用。
|
||||
--- 单章失败不阻断后续。不 touch 书架路径,避免阅读中改掉当前章登记。
|
||||
---@param client CopymangaClient
|
||||
---@param identity BookIdentity
|
||||
---@param toc BookChapter[]
|
||||
---@param from_idx integer 当前章序号(预取 from_idx+1 …);0 表示从第一章开始
|
||||
---@param count integer
|
||||
---@param ops { progress: fun(done: integer, total: integer)|nil, interval_seconds: number|nil }|nil
|
||||
---@param cb fun(cached: integer, total: integer, failed: integer, err: any)|nil
|
||||
---@return { cancel: fun() }
|
||||
function Chapter.prefetchAsync(client, identity, toc, from_idx, count, ops, cb)
|
||||
ops = ops or {}
|
||||
local cancelled, active = false, nil
|
||||
local cached_count, failed_count, last_error = 0, 0, nil
|
||||
local chapters = {}
|
||||
for idx = from_idx + 1, math.min(#toc, from_idx + count) do
|
||||
chapters[#chapters + 1] = toc[idx]
|
||||
end
|
||||
|
||||
local pos = 1
|
||||
local interval = ops.interval_seconds or 0
|
||||
local UIManager = require("ui/uimanager")
|
||||
local function step()
|
||||
if cancelled then return end
|
||||
local chapter = chapters[pos]
|
||||
pos = pos + 1
|
||||
if not chapter then
|
||||
if cb then cb(cached_count, #chapters, failed_count, last_error) end
|
||||
return
|
||||
end
|
||||
active = Chapter.materializeAsync(client, identity, chapter, nil, function(path, err)
|
||||
active = nil
|
||||
if cancelled then return end
|
||||
if path then
|
||||
cached_count = cached_count + 1
|
||||
else
|
||||
failed_count = failed_count + 1
|
||||
last_error = err or last_error
|
||||
end
|
||||
if ops.progress then
|
||||
ops.progress(cached_count + failed_count, #chapters)
|
||||
end
|
||||
if interval > 0 then
|
||||
UIManager:scheduleIn(interval, require("ui.guard").wrap("copymanga step", step))
|
||||
else
|
||||
UIManager:nextTick(require("ui.guard").wrap("copymanga step", step))
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
UIManager:nextTick(require("ui.guard").wrap("copymanga step", step))
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
return Chapter
|
||||
@@ -0,0 +1,435 @@
|
||||
--[[--
|
||||
拷贝漫画 App API 客户端。
|
||||
|
||||
协议对齐 kComics:``COPY/3.0.0`` + ``/api/v3/*``,返回 JSON wire。
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.client
|
||||
--]]
|
||||
|
||||
local JSON = require("json")
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
local Auth = require("source.auth")
|
||||
|
||||
---@class CopymangaClient
|
||||
---@field base_url string
|
||||
---@field token string
|
||||
---@field progress_supported boolean
|
||||
local Client = {}
|
||||
Client.__index = Client
|
||||
|
||||
local DEFAULT_BASE_URL = "https://api.copy202601.com"
|
||||
|
||||
Client.DEFAULT_BASE_URL = DEFAULT_BASE_URL
|
||||
Client.CURRENT_BASE_URL = "https://api.manga2026.xyz"
|
||||
Client.SITES = { "https://www.mangacopy.com", "https://www.copy4000.com", Client.CURRENT_BASE_URL }
|
||||
local SITE_APIS = {
|
||||
["https://www.mangacopy.com"] = "https://api.mangacopy.com",
|
||||
["https://www.copy4000.com"] = "https://api.copy4000.com",
|
||||
}
|
||||
|
||||
---@return string
|
||||
function Client.normalizeBaseUrl(url)
|
||||
url = Text.rtrimSlashes(Text.stripWhitespace(url))
|
||||
if url == "" then
|
||||
return DEFAULT_BASE_URL
|
||||
end
|
||||
return SITE_APIS[url] or url
|
||||
end
|
||||
|
||||
--- App 请求头。登录与章节接口都靠这组字段识别客户端。
|
||||
---@param token string|nil
|
||||
---@return table
|
||||
function Client.headers(token)
|
||||
local headers = {
|
||||
["User-Agent"] = "COPY/3.0.0",
|
||||
["version"] = "2025.08.15",
|
||||
["platform"] = "1",
|
||||
["webp"] = "1",
|
||||
["region"] = "1",
|
||||
}
|
||||
if type(token) == "string" and token ~= "" then
|
||||
headers["Authorization"] = "Token " .. token
|
||||
end
|
||||
return headers
|
||||
end
|
||||
|
||||
---@param cfg { base_url: string|nil, token: string|nil }
|
||||
---@return CopymangaClient
|
||||
function Client:new(cfg)
|
||||
local base_url = Client.normalizeBaseUrl(cfg.base_url)
|
||||
return setmetatable({
|
||||
base_url = base_url,
|
||||
token = cfg.token or "",
|
||||
progress_supported = base_url ~= Client.CURRENT_BASE_URL,
|
||||
}, self)
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Client:configured()
|
||||
return self.base_url:match("^https?://[^/]+$") ~= nil
|
||||
end
|
||||
|
||||
--- code 200 才算成功;拒绝请求时保留服务端原因,不据 210 推断用户账号状态。
|
||||
---@param raw string|nil
|
||||
---@param err string|nil
|
||||
---@return table|nil, string|nil
|
||||
function Client.decode(raw, err)
|
||||
if not raw then return nil, err end
|
||||
local ok, wire = pcall(JSON.decode, raw)
|
||||
if not ok or type(wire) ~= "table" then
|
||||
return nil, _("响应无效")
|
||||
end
|
||||
local code = tonumber(wire.code)
|
||||
if code ~= 200 then
|
||||
if type(wire.message) == "string" and wire.message ~= "" then
|
||||
return nil, wire.message
|
||||
end
|
||||
return nil, code == 210 and _("拷贝漫画 API 拒绝请求(210),请按官网提示处理") or _("请求失败")
|
||||
end
|
||||
return wire
|
||||
end
|
||||
|
||||
--- 进行中的自动重登:等待者回调列表;nil = 空闲。全进程只发一次登录,免得并发 401 各自登录、互相挤掉令牌。
|
||||
---@type fun(ok: boolean|nil, err: string|nil, rejected: boolean|nil)[]|nil
|
||||
local relogin_waiters
|
||||
|
||||
--- 用本地保存的账号密码重登(单飞)。没存密码时立即 cb(nil, nil, true)。
|
||||
--- 不随单个请求取消:登录结果要分给所有等待者。
|
||||
---@param cb fun(ok: boolean|nil, err: string|nil, rejected: boolean|nil)
|
||||
local function relogin(cb)
|
||||
if relogin_waiters then
|
||||
relogin_waiters[#relogin_waiters + 1] = cb
|
||||
return
|
||||
end
|
||||
local Auth = require("moonsource_copymanga.auth")
|
||||
local username, password = Auth.credentials()
|
||||
if not username then
|
||||
cb(nil, nil, true)
|
||||
return
|
||||
end
|
||||
relogin_waiters = { cb }
|
||||
Auth.loginAsync(username, password, function(ok, err, rejected)
|
||||
local waiters = relogin_waiters or {}
|
||||
relogin_waiters = nil
|
||||
for _idx, waiter in ipairs(waiters) do waiter(ok, err, rejected) end
|
||||
end)
|
||||
end
|
||||
|
||||
--- 只使用当前站点的最新令牌,退出或切站后不复用旧实例的登录态。
|
||||
---@return string
|
||||
function Client:_latestToken()
|
||||
local cfg = require("utils.settings").getSource("copymanga")
|
||||
if Client.normalizeBaseUrl(cfg.base_url) ~= self.base_url then return "" end
|
||||
self.token = type(cfg.token) == "string" and cfg.token or ""
|
||||
return self.token
|
||||
end
|
||||
|
||||
--- GET JSON。令牌失效(HTTP 401,如在别处登录后被挤掉)时用本地保存的账号密码重登一次再重试。
|
||||
--- 别的请求已经换过令牌时直接用新令牌重试,不再登录。重登被服务端拒绝才提示重新登录,网络错误原样回传。
|
||||
---@param path string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:_getJson(path, cb, public)
|
||||
local expired = _("点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → 拷贝漫画 → 拷贝漫画账号」。账号和密码重新填一遍,点「登录」。")
|
||||
local active, cancelled
|
||||
local function send(may_relogin)
|
||||
local sent = public and "" or self:_latestToken()
|
||||
local pending = true
|
||||
local handle = require("http.request").get(self.base_url .. path, {
|
||||
accept = "application/json",
|
||||
headers = Client.headers(sent),
|
||||
allow_redirects = public == true,
|
||||
timeout = require("http.timeout").net(2),
|
||||
connect_timeout = require("http.timeout").net(1),
|
||||
}, function(raw, err, res)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not (res and res.code == 401) then
|
||||
cb(Client.decode(raw, err))
|
||||
return
|
||||
end
|
||||
if not may_relogin then
|
||||
cb(nil, Auth.expire("copymanga", expired))
|
||||
return
|
||||
end
|
||||
if self:_latestToken() ~= sent then
|
||||
send(false)
|
||||
return
|
||||
end
|
||||
active = nil
|
||||
relogin(function(ok, login_err, rejected)
|
||||
if cancelled then return end
|
||||
if ok then Auth.ok("copymanga"); send(false); return end
|
||||
cb(nil, rejected and Auth.expire("copymanga", expired) or (login_err or _("请求失败")))
|
||||
end)
|
||||
end)
|
||||
if pending then active = handle end
|
||||
end
|
||||
send(true)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
---@param stable_id string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:detailAsync(stable_id, cb)
|
||||
return self:_getJson("/api/v3/comic2/" .. Text.urlEncode(stable_id) .. "?platform=1", cb, true)
|
||||
end
|
||||
|
||||
---@param keyword string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:searchAsync(keyword, cb)
|
||||
return self:_getJson("/api/v3/search/comic?limit=30&offset=0&platform=1&q_type=&q=" .. Text.urlEncode(keyword), cb, true)
|
||||
end
|
||||
|
||||
--- 排行榜一页(20 部)。audience 为 male / female,nil 是全部;date_type 1 日 2 周 3 月 4 总。
|
||||
---@param date_type string
|
||||
---@param audience string|nil
|
||||
---@param offset integer
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:rankAsync(date_type, audience, offset, cb)
|
||||
local path = "/api/v3/ranks?limit=20&type=1&platform=1&offset=" .. offset .. "&date_type=" .. date_type
|
||||
if audience then path = path .. "&audience_type=" .. audience end
|
||||
return self:_getJson(path, cb, true)
|
||||
end
|
||||
|
||||
--- 分类列表一页(20 部),按人气。
|
||||
---@param theme string
|
||||
---@param offset integer
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:themeAsync(theme, offset, cb)
|
||||
return self:_getJson("/api/v3/comics?limit=20&free_type=1&ordering=-popular&offset=" .. offset
|
||||
.. "&theme=" .. Text.urlEncode(theme), cb, true)
|
||||
end
|
||||
|
||||
--- 云端浏览记录(需登录)。browse 为 null 表示从未读过。
|
||||
---@param stable_id string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:getProgressAsync(stable_id, cb)
|
||||
return self:_getJson(
|
||||
"/api/v3/comic2/" .. Text.urlEncode(stable_id) .. "/query?platform=1",
|
||||
cb
|
||||
)
|
||||
end
|
||||
|
||||
--- 按分组分页拉全量章节行(uuid / name / group_*)。
|
||||
---@param stable_id string
|
||||
---@param groups { path_word: string, name: string }[] 见 Mapper.groups(至少有默认组)
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:chaptersAsync(stable_id, groups, cb)
|
||||
local cancelled, active = false, nil
|
||||
local rows, gi = {}, 1
|
||||
local offset, limit = 0, 100
|
||||
|
||||
local function step()
|
||||
if cancelled then return end
|
||||
local group = groups[gi]
|
||||
if not group then
|
||||
cb(rows)
|
||||
return
|
||||
end
|
||||
local path = "/api/v3/comic/" .. Text.urlEncode(stable_id)
|
||||
.. "/group/" .. Text.urlEncode(group.path_word)
|
||||
.. "/chapters?limit=" .. limit .. "&offset=" .. offset .. "&platform=1"
|
||||
local pending = true
|
||||
local handle = self:_getJson(path, function(wire, err)
|
||||
pending = false
|
||||
active = nil
|
||||
if cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
local list, parse_err = require("moonsource_copymanga.mapper").list(wire, offset)
|
||||
if not list then cb(nil, parse_err); return end
|
||||
local root = wire.results
|
||||
for _idx, row in ipairs(list) do
|
||||
if type(row) == "table" and type(row.uuid) == "string" and row.uuid ~= "" then
|
||||
rows[#rows + 1] = {
|
||||
uuid = row.uuid,
|
||||
name = row.name,
|
||||
group_path = group.path_word,
|
||||
group_name = group.name,
|
||||
}
|
||||
end
|
||||
end
|
||||
local total = tonumber(root.total) or 0
|
||||
offset = offset + #list
|
||||
if #list == 0 or offset >= total then
|
||||
gi = gi + 1
|
||||
offset = 0
|
||||
end
|
||||
step()
|
||||
end, true)
|
||||
if pending then active = handle end
|
||||
end
|
||||
|
||||
step()
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
---@param stable_id string
|
||||
---@param chapter_uid string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:chapterAsync(stable_id, chapter_uid, cb)
|
||||
local endpoint = self.base_url == Client.CURRENT_BASE_URL and "/chapter/" or "/chapter2/"
|
||||
local path = "/api/v3/comic/" .. Text.urlEncode(stable_id)
|
||||
.. endpoint .. Text.urlEncode(chapter_uid) .. "?platform=1"
|
||||
return self:_getJson(path, cb)
|
||||
end
|
||||
|
||||
--- 收藏或取消收藏(需登录)。comic_id 是 comic2 返回的 uuid,不是 path_word。
|
||||
---@param comic_id string
|
||||
---@param collect boolean
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:setCollectAsync(comic_id, collect, cb)
|
||||
-- 自动重登后新 token 只写进设置,这里先取最新的,免得拿旧 token 收藏失败。
|
||||
if self:_latestToken() == "" then
|
||||
return require("source.base").defer(cb, nil, _("请先登录拷贝漫画账号"))
|
||||
end
|
||||
local body = Text.formEncode({
|
||||
comic_id = comic_id,
|
||||
is_collect = collect and "1" or "0",
|
||||
authorization = "Token " .. self.token,
|
||||
})
|
||||
return require("http.request").post(
|
||||
self.base_url .. "/api/v3/member/collect/comic",
|
||||
body,
|
||||
{
|
||||
accept = "application/json",
|
||||
headers = Client.headers(self.token),
|
||||
content_type = "application/x-www-form-urlencoded;charset=utf-8",
|
||||
allow_redirects = false,
|
||||
cache_ttl = 0,
|
||||
timeout = require("http.timeout").net(1),
|
||||
connect_timeout = require("http.timeout").net(1),
|
||||
},
|
||||
function(raw, err)
|
||||
cb(Client.decode(raw, err))
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
-- 收藏列表每页条数(接口上限 50)。
|
||||
local COLLECT_PAGE = 50
|
||||
|
||||
--- 分页拉全量收藏(需登录),回调收藏行数组(每行 ``{ comic = {...} }``)。
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:collectAllAsync(cb)
|
||||
if self:_latestToken() == "" then
|
||||
return require("source.base").defer(cb, nil, _("请先登录拷贝漫画账号"))
|
||||
end
|
||||
local cancelled, rows, offset = false, {}, 0
|
||||
---@type { cancel: fun() }
|
||||
local active
|
||||
|
||||
local function step()
|
||||
local pending = true
|
||||
local handle = self:_getJson("/api/v3/member/collect/comics?free_type=1&limit=" .. COLLECT_PAGE
|
||||
.. "&offset=" .. offset .. "&ordering=-datetime_updated", function(wire, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
local list, parse_err = require("moonsource_copymanga.mapper").list(wire, offset)
|
||||
if not list then cb(nil, parse_err); return end
|
||||
local root = wire.results
|
||||
for _idx, row in ipairs(list) do
|
||||
rows[#rows + 1] = row
|
||||
end
|
||||
offset = offset + #list
|
||||
if #list == 0 or offset >= (tonumber(root.total) or 0) then
|
||||
cb(rows)
|
||||
return
|
||||
end
|
||||
step()
|
||||
end)
|
||||
if pending then active = handle end
|
||||
end
|
||||
|
||||
step()
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
---@param cb fun(books: Book[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:shelfSyncAsync(cb)
|
||||
return self:collectAllAsync(function(rows, err)
|
||||
if not rows then cb(nil, err); return end
|
||||
cb(require("moonsource_copymanga.mapper").collect(rows))
|
||||
end)
|
||||
end
|
||||
|
||||
---@param client CopymangaClient
|
||||
---@param stable_id string
|
||||
---@param collect boolean
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
local function changeCollect(client, stable_id, collect, cb)
|
||||
local cancelled, active, pending = false, nil, true
|
||||
local detail = client:detailAsync(stable_id, function(wire, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
local comic_id = wire and require("moonsource_copymanga.mapper").comicId(wire)
|
||||
if not comic_id then cb(nil, err or _("漫画详情解析失败")); return end
|
||||
local writing = true
|
||||
local write = client:setCollectAsync(comic_id, collect, function(result, write_err)
|
||||
writing = false
|
||||
if cancelled then return end
|
||||
if not result then cb(nil, write_err); return end
|
||||
local reading = true
|
||||
local readback = client:collectAllAsync(function(rows, read_err)
|
||||
reading = false
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, read_err); return end
|
||||
local found = false
|
||||
for _idx, row in ipairs(rows) do
|
||||
local comic = type(row) == "table" and row.comic
|
||||
if type(comic) ~= "table" or type(comic.uuid) ~= "string" or comic.uuid == "" then
|
||||
cb(nil, _("响应无效")); return
|
||||
end
|
||||
if comic.uuid == comic_id then found = true end
|
||||
end
|
||||
if found ~= collect then cb(nil, _("书架同步未确认")); return end
|
||||
cb(result)
|
||||
end)
|
||||
if reading then active = readback end
|
||||
end)
|
||||
if writing then active = write end
|
||||
end)
|
||||
if pending then active = detail end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
---@param stable_id string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:addToShelfAsync(stable_id, cb)
|
||||
return changeCollect(self, stable_id, true, cb)
|
||||
end
|
||||
|
||||
---@param stable_id string
|
||||
---@param cb fun(wire: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client:removeFromShelfAsync(stable_id, cb)
|
||||
return changeCollect(self, stable_id, false, cb)
|
||||
end
|
||||
|
||||
return Client
|
||||
@@ -0,0 +1,52 @@
|
||||
return {
|
||||
["百合"] = "Girls' love",
|
||||
["热血"] = "Hot-blooded",
|
||||
["搞笑"] = "Comedy",
|
||||
["战争"] = "War",
|
||||
["穿越"] = "Time travel",
|
||||
["治愈"] = "Heartwarming",
|
||||
["惊悚"] = "Thriller",
|
||||
["校园"] = "School life",
|
||||
["重生"] = "Rebirth",
|
||||
["恐怖"] = "Horror",
|
||||
["确定退出拷贝漫画账号?"] = "Sign out of CopyManga?",
|
||||
["拷贝漫画账号"] = "CopyManga account",
|
||||
["正在准备漫画…"] = "Preparing comic…",
|
||||
["正在登录…"] = "Signing in…",
|
||||
["漫画图片下载失败"] = "Failed to download comic image",
|
||||
["漫画目录为空"] = "Comic chapter list is empty",
|
||||
["漫画目录保存失败"] = "Failed to save comic chapter list",
|
||||
["漫画详情解析失败"] = "Failed to parse comic details",
|
||||
["登录失败:未返回令牌"] = "Sign-in failed: no token returned",
|
||||
["章节图片解析失败"] = "Failed to parse chapter images",
|
||||
["缺少章节信息"] = "Missing chapter info",
|
||||
["站点地址无效"] = "Invalid site address",
|
||||
["登录状态已改变,请重试"] = "Sign-in state changed. Please try again",
|
||||
["请先退出当前账号"] = "Sign out of the current account first",
|
||||
["登录失败:未返回账号标识"] = "Sign-in failed: no account ID returned",
|
||||
["书架同步未确认"] = "Bookshelf synchronization could not be confirmed",
|
||||
["请先登录拷贝漫画账号"] = "Sign in to CopyManga first",
|
||||
["点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → 拷贝漫画 → 拷贝漫画账号」。账号和密码重新填一遍,点「登录」。"] = "Tap Sign in again on this message. If there is no button, open Settings → Library → Account → CopyManga → CopyManga account. Enter the username and password again, then tap Sign in.",
|
||||
["请输入账号和密码"] = "Enter account and password",
|
||||
["拷贝漫画 API 拒绝请求(210),请按官网提示处理"] = "CopyManga API rejected the request (210). Follow the official site's instructions.",
|
||||
["励志"] = "Self-improvement",
|
||||
["美食"] = "Food",
|
||||
["仙侠"] = "Xianxia",
|
||||
["历史"] = "History",
|
||||
["周榜"] = "Weekly",
|
||||
["奇幻"] = "Fantasy",
|
||||
["女频"] = "Female",
|
||||
["总榜"] = "All-time",
|
||||
["悬疑"] = "Mystery",
|
||||
["日榜"] = "Daily",
|
||||
["月榜"] = "Monthly",
|
||||
["武侠"] = "Wuxia",
|
||||
["男频"] = "Male",
|
||||
["科幻"] = "Sci-Fi",
|
||||
["轻小说"] = "Light Novels",
|
||||
["都市"] = "Urban",
|
||||
["书架同步"] = "Shelf sync",
|
||||
["拷贝漫画在线阅读"] = "Copymanga online reading",
|
||||
["搜索与发现"] = "Search and discover",
|
||||
["进度同步"] = "Progress sync",
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
return {
|
||||
["百合"] = "百合",
|
||||
["热血"] = "熱血",
|
||||
["搞笑"] = "搞笑",
|
||||
["战争"] = "戰爭",
|
||||
["穿越"] = "穿越",
|
||||
["治愈"] = "療癒",
|
||||
["惊悚"] = "驚悚",
|
||||
["校园"] = "校園",
|
||||
["重生"] = "重生",
|
||||
["恐怖"] = "恐怖",
|
||||
["确定退出拷贝漫画账号?"] = "確定登出拷貝漫畫帳號?",
|
||||
["拷贝漫画账号"] = "拷貝漫畫帳號",
|
||||
["正在准备漫画…"] = "正在準備漫畫…",
|
||||
["正在登录…"] = "正在登入…",
|
||||
["漫画图片下载失败"] = "漫畫圖片下載失敗",
|
||||
["漫画目录为空"] = "漫畫目錄為空",
|
||||
["漫画目录保存失败"] = "漫畫目錄儲存失敗",
|
||||
["漫画详情解析失败"] = "漫畫詳情解析失敗",
|
||||
["登录失败:未返回令牌"] = "登入失敗:未返回權杖",
|
||||
["章节图片解析失败"] = "章節圖片解析失敗",
|
||||
["缺少章节信息"] = "缺少章節資訊",
|
||||
["站点地址无效"] = "站點地址無效",
|
||||
["登录状态已改变,请重试"] = "登入狀態已改變,請重試",
|
||||
["请先退出当前账号"] = "請先登出目前帳號",
|
||||
["登录失败:未返回账号标识"] = "登入失敗:未回傳帳號識別碼",
|
||||
["书架同步未确认"] = "書架同步未確認",
|
||||
["请先登录拷贝漫画账号"] = "請先登入拷貝漫畫帳號",
|
||||
["点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → 拷贝漫画 → 拷贝漫画账号」。账号和密码重新填一遍,点「登录」。"] = "點這個提示上的「重新登入」。沒有按鈕就打開「設定 → 書庫 → 帳號 → 拷貝漫畫 → 拷貝漫畫帳號」。帳號和密碼重新填一遍,點「登入」。",
|
||||
["请输入账号和密码"] = "請輸入帳號和密碼",
|
||||
["拷贝漫画 API 拒绝请求(210),请按官网提示处理"] = "拷貝漫畫 API 拒絕請求(210),請按官網提示處理",
|
||||
["励志"] = "勵志",
|
||||
["美食"] = "美食",
|
||||
["仙侠"] = "仙俠",
|
||||
["历史"] = "歷史",
|
||||
["周榜"] = "週榜",
|
||||
["奇幻"] = "奇幻",
|
||||
["女频"] = "女頻",
|
||||
["总榜"] = "總榜",
|
||||
["悬疑"] = "懸疑",
|
||||
["日榜"] = "日榜",
|
||||
["月榜"] = "月榜",
|
||||
["武侠"] = "武俠",
|
||||
["男频"] = "男頻",
|
||||
["科幻"] = "科幻",
|
||||
["轻小说"] = "輕小說",
|
||||
["都市"] = "都市",
|
||||
["书架同步"] = "書架同步",
|
||||
["拷贝漫画在线阅读"] = "拷貝漫畫線上閱讀",
|
||||
["搜索与发现"] = "搜尋與發現",
|
||||
["进度同步"] = "進度同步",
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
return function()
|
||||
local copy = {}
|
||||
for k, v in pairs(require("moonsource_copymanga._meta")) do copy[k] = v end
|
||||
return copy
|
||||
end
|
||||
@@ -0,0 +1,193 @@
|
||||
--[[--
|
||||
拷贝漫画 App API 数据映射。
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.mapper
|
||||
--]]
|
||||
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
local Mapper = {}
|
||||
local SOURCE_ID = "copymanga"
|
||||
|
||||
---@param wire table
|
||||
---@param offset integer|nil
|
||||
---@return table[]|nil, string|nil, boolean|nil
|
||||
function Mapper.list(wire, offset)
|
||||
local root = wire.results
|
||||
if type(root) ~= "table" or type(root.list) ~= "table" then return nil, _("响应无效") end
|
||||
local total = tonumber(root.total)
|
||||
offset = offset or tonumber(root.offset) or 0
|
||||
if total and #root.list == 0 and offset < total then return nil, _("响应无效") end
|
||||
return root.list, nil, total and offset + #root.list < total or (not total and #root.list > 0)
|
||||
end
|
||||
|
||||
---@param rows table[]|nil
|
||||
---@return string|nil
|
||||
local function names(rows)
|
||||
local list = {}
|
||||
for _idx, row in ipairs(rows or {}) do
|
||||
if type(row) == "table" and type(row.name) == "string" and row.name ~= "" then
|
||||
list[#list + 1] = row.name
|
||||
end
|
||||
end
|
||||
return #list > 0 and table.concat(list, ", ") or nil
|
||||
end
|
||||
|
||||
--- 回包的 results 表;wire 或 results 不是表时给空表。
|
||||
---@param wire table|nil
|
||||
---@return table
|
||||
local function resultsOf(wire)
|
||||
local results = type(wire) == "table" and wire.results
|
||||
return type(results) == "table" and results or {}
|
||||
end
|
||||
|
||||
---@param row table|nil
|
||||
---@return table|nil
|
||||
local function comicOf(row)
|
||||
if type(row) ~= "table" then return nil end
|
||||
if type(row.comic) == "table" then return row.comic end
|
||||
return row
|
||||
end
|
||||
|
||||
---@param row table|nil
|
||||
---@return Book|nil
|
||||
function Mapper.book(row)
|
||||
row = comicOf(row)
|
||||
if type(row) ~= "table" or type(row.path_word) ~= "string" or row.path_word == "" then
|
||||
return nil
|
||||
end
|
||||
local intro = row.intro or row.brief
|
||||
return {
|
||||
source_id = SOURCE_ID,
|
||||
stable_id = row.path_word,
|
||||
title = row.name,
|
||||
authors = names(row.author),
|
||||
category = row.category or names(row.theme),
|
||||
intro = intro and Text.stripHtml(intro),
|
||||
cover = row.cover,
|
||||
percent = tonumber(row.percent) or 0,
|
||||
deleted = (row.deleted == true or tonumber(row.deleted) == 1) and 1 or nil,
|
||||
}
|
||||
end
|
||||
|
||||
--- 搜索 wire → Book[]。
|
||||
---@param wire table
|
||||
---@return Book[]|nil, string|nil, boolean|nil
|
||||
function Mapper.search(wire)
|
||||
local rows, err, more = Mapper.list(wire)
|
||||
if not rows then return nil, err end
|
||||
local out = {}
|
||||
for _idx, row in ipairs(rows) do
|
||||
local book = Mapper.book(row)
|
||||
if not book then return nil, _("响应无效") end
|
||||
out[#out + 1] = book
|
||||
end
|
||||
return out, nil, more
|
||||
end
|
||||
|
||||
--- 收藏行(见 Client:collectAllAsync)→ 在架书。
|
||||
---@param rows table[]
|
||||
---@return Book[]|nil, string|nil
|
||||
function Mapper.collect(rows)
|
||||
local books = {}
|
||||
for _idx, row in ipairs(rows) do
|
||||
local book = Mapper.book(row)
|
||||
if not book then return nil, _("响应无效") end
|
||||
book.deleted = 0
|
||||
books[#books + 1] = book
|
||||
end
|
||||
return books
|
||||
end
|
||||
|
||||
--- comic2 JSON → 书籍元数据。
|
||||
---@param wire table
|
||||
---@return Book|nil
|
||||
function Mapper.detail(wire)
|
||||
return Mapper.book(resultsOf(wire).comic)
|
||||
end
|
||||
|
||||
--- comic2 JSON → 云端收藏用的漫画 uuid。
|
||||
---@param wire table|nil
|
||||
---@return string|nil
|
||||
function Mapper.comicId(wire)
|
||||
local comic = resultsOf(wire).comic
|
||||
local uuid = type(comic) == "table" and comic.uuid or nil
|
||||
if type(uuid) == "string" and uuid ~= "" then return uuid end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- comic2.groups → 默认组在前的分组列表。
|
||||
---@param wire table|nil
|
||||
---@return { path_word: string, name: string }[]
|
||||
function Mapper.groups(wire)
|
||||
local raw = resultsOf(wire).groups
|
||||
local list = {}
|
||||
if type(raw) == "table" then
|
||||
for _group_key, group in pairs(raw) do
|
||||
if type(group) == "table"
|
||||
and type(group.path_word) == "string"
|
||||
and group.path_word ~= ""
|
||||
then
|
||||
list[#list + 1] = {
|
||||
path_word = group.path_word,
|
||||
name = type(group.name) == "string" and group.name ~= ""
|
||||
and group.name or group.path_word,
|
||||
}
|
||||
end
|
||||
end
|
||||
table.sort(list, function(a, b)
|
||||
if a.path_word == "default" then return true end
|
||||
if b.path_word == "default" then return false end
|
||||
return a.path_word < b.path_word
|
||||
end)
|
||||
end
|
||||
if #list == 0 then
|
||||
return { { path_word = "default", name = "默认" } }
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
--- 分组章节行 → 连续目录;非默认组加组名前缀。
|
||||
---@param rows table[]|nil
|
||||
---@return BookChapter[]|nil
|
||||
function Mapper.chapters(rows)
|
||||
local chapters, seen = {}, {}
|
||||
for _idx, row in ipairs(rows or {}) do
|
||||
local uid = type(row) == "table" and row.uuid or nil
|
||||
if type(uid) == "string" and uid ~= "" and not seen[uid] then
|
||||
seen[uid] = true
|
||||
local title = tostring(row.name or uid)
|
||||
if row.group_path and row.group_path ~= "default"
|
||||
and type(row.group_name) == "string" and row.group_name ~= ""
|
||||
then
|
||||
title = row.group_name .. " · " .. title
|
||||
end
|
||||
chapters[#chapters + 1] = {
|
||||
idx = #chapters + 1,
|
||||
uid = uid,
|
||||
title = title,
|
||||
depth = 1,
|
||||
}
|
||||
end
|
||||
end
|
||||
return #chapters > 0 and chapters or nil
|
||||
end
|
||||
|
||||
--- comic2/query wire → 占位进度 + 章节 uuid。browse 为 null 表示从未读过。
|
||||
--- 云端只有章粒度,fraction 由门面按目录换算。
|
||||
---@param wire table|nil
|
||||
---@return ProgressPosition|nil, string|nil
|
||||
function Mapper.progress(wire)
|
||||
local browse = resultsOf(wire).browse
|
||||
if type(browse) ~= "table" then return nil end
|
||||
local uid = browse.chapter_uuid or browse.chapter_id
|
||||
if type(uid) ~= "string" or uid == "" then return nil end
|
||||
local title = browse.chapter_name
|
||||
return {
|
||||
fraction = 0,
|
||||
chapter_title = type(title) == "string" and title ~= "" and title or nil,
|
||||
}, uid
|
||||
end
|
||||
|
||||
return Mapper
|
||||
@@ -0,0 +1,64 @@
|
||||
--[[--
|
||||
拷贝漫画 App 章节载荷。
|
||||
|
||||
chapter2 返回 ``contents``(图片 URL)和 ``words``(阅读序号)。
|
||||
图床把宽度写在文件名里(``.c800x.`` / ``.c1500x.``)。手机端要 1500 宽 webp,
|
||||
墨水屏翻页会整页解码,长图会卡。这里固定要 800 宽 JPEG。没有档位的地址不动。
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.protocol
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
local Protocol = {}
|
||||
|
||||
---@param wire table|nil
|
||||
---@return table|nil
|
||||
local function chapterRoot(wire)
|
||||
if type(wire) ~= "table" then return nil end
|
||||
if type(wire.chapter) == "table" then return wire.chapter end
|
||||
local results = wire.results
|
||||
if type(results) == "table" and type(results.chapter) == "table" then
|
||||
return results.chapter
|
||||
end
|
||||
if type(wire.contents) == "table" then return wire end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- 图床档位改成 800 宽 JPEG。``.webp.c1500x.webp`` 与 ``.c800x.webp`` 两种写法都认。
|
||||
---@param url string
|
||||
---@return string
|
||||
local function screenUrl(url)
|
||||
local rewritten = url:gsub("%.webp(%.c%d+x%.)webp", ".jpg%1jpg", 1)
|
||||
rewritten = rewritten:gsub("%.c%d+x%.webp", ".c800x.jpg")
|
||||
return (rewritten:gsub("%.c%d+x%.jpe?g", ".c800x.jpg"))
|
||||
end
|
||||
|
||||
--- 解码章节页,返回按 ``words`` 排序的图片 URL。
|
||||
---@param wire table|nil
|
||||
---@return string[]|nil, string|nil
|
||||
function Protocol.chapterImages(wire)
|
||||
local chapter = chapterRoot(wire)
|
||||
local contents = chapter and chapter.contents
|
||||
if type(contents) ~= "table" then return nil, _("章节图片解析失败") end
|
||||
local words = type(chapter.words) == "table" and chapter.words or {}
|
||||
local pages = {}
|
||||
for i, row in ipairs(contents) do
|
||||
local url = type(row) == "table" and row.url or nil
|
||||
if type(url) == "string" and url:match("^https?://") then
|
||||
pages[#pages + 1] = {
|
||||
url = screenUrl(url),
|
||||
order = tonumber(words[i]) or (i - 1),
|
||||
}
|
||||
end
|
||||
end
|
||||
table.sort(pages, function(a, b) return a.order < b.order end)
|
||||
local images = {}
|
||||
for _idx, page in ipairs(pages) do
|
||||
images[#images + 1] = page.url
|
||||
end
|
||||
if #images == 0 then return nil, _("章节图片解析失败") end
|
||||
return images
|
||||
end
|
||||
|
||||
return Protocol
|
||||
@@ -0,0 +1,156 @@
|
||||
--[[--
|
||||
拷贝漫画源设置。
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.setting
|
||||
--]]
|
||||
|
||||
local Client = require("moonsource_copymanga.client")
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
local SOURCE_ID = "copymanga"
|
||||
local Setting = {}
|
||||
local editBaseUrl
|
||||
|
||||
local function selectBaseUrl()
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource(SOURCE_ID)
|
||||
local current = Client.normalizeBaseUrl(cfg.base_url)
|
||||
local items = {}
|
||||
for _idx, url in ipairs(Client.SITES) do
|
||||
items[#items + 1] = {
|
||||
text = url,
|
||||
variant = Client.normalizeBaseUrl(url) == current and "primary" or "normal",
|
||||
callback = function()
|
||||
cfg.base_url = url
|
||||
settings.saveSource(SOURCE_ID, cfg)
|
||||
require("source.registry").reload()
|
||||
end,
|
||||
}
|
||||
end
|
||||
items[#items + 1] = {
|
||||
text = _("自定义"),
|
||||
callback = function() editBaseUrl() end,
|
||||
}
|
||||
require("ui.themes.widgets.sheet"):new{ title = _("站点地址"), items = items }:show()
|
||||
end
|
||||
|
||||
editBaseUrl = function()
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource(SOURCE_ID)
|
||||
require("ui.themes.widgets.input"):new{
|
||||
title = _("站点地址"),
|
||||
fields = { { text = Client.normalizeBaseUrl(cfg.base_url), hint = Client.DEFAULT_BASE_URL } },
|
||||
ok_text = _("保存"),
|
||||
callback = function(value)
|
||||
local url = Client.normalizeBaseUrl(value)
|
||||
if not url:match("^https?://[^/%s?#]+$") then
|
||||
require("ui.themes.widgets.toast"):new{ text = _("站点地址无效"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
cfg.base_url = url
|
||||
settings.saveSource(SOURCE_ID, cfg)
|
||||
require("source.registry").reload()
|
||||
end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
---@param username string
|
||||
---@param password string
|
||||
local function doLogin(username, password)
|
||||
local Auth = require("moonsource_copymanga.auth")
|
||||
local NetworkMgr = require("ui/network/manager")
|
||||
local Toast = require("ui.themes.widgets.toast")
|
||||
if not NetworkMgr:isConnected() then
|
||||
Toast:new{ text = _("网络不可用,请先连接 Wi-Fi"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
local job
|
||||
local waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = _("拷贝漫画账号"), text = _("正在登录…"), dismissable = false,
|
||||
on_cancel = function() if job then job.cancel() end end,
|
||||
}
|
||||
job = waiting:showWhile(Auth.loginAsync, username, password, function(ok, err)
|
||||
waiting:close()
|
||||
if not ok then
|
||||
Toast:new{ text = err or _("登录失败"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
Toast:new{ text = _("登录成功"), timeout = 2 }:show()
|
||||
require("source.registry").reload()
|
||||
end)
|
||||
end
|
||||
|
||||
local function showLogin()
|
||||
local Auth = require("moonsource_copymanga.auth")
|
||||
local username, password = Auth.credentials()
|
||||
if not username then
|
||||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||
username = tostring(cfg.username or "")
|
||||
password = ""
|
||||
end
|
||||
require("ui.themes.widgets.input"):new{
|
||||
title = _("拷贝漫画账号"),
|
||||
fields = {
|
||||
{ text = username, hint = _("账号") },
|
||||
{ text = password, hint = _("密码"), text_type = "password" },
|
||||
},
|
||||
ok_text = _("登录"),
|
||||
callback = function(user, pass) doLogin(user, pass) end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
---@return table[]
|
||||
function Setting.rows()
|
||||
local Auth = require("moonsource_copymanga.auth")
|
||||
local rows = {
|
||||
function(iw)
|
||||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||
return require("ui.themes.widgets.row").widget({
|
||||
width = iw,
|
||||
chevron = true,
|
||||
kind = "nav",
|
||||
icon = "dns",
|
||||
title = _("站点地址"),
|
||||
status = Client.normalizeBaseUrl(cfg.base_url),
|
||||
status_on = true,
|
||||
callback = selectBaseUrl,
|
||||
})
|
||||
end,
|
||||
function(iw)
|
||||
local status, status_on
|
||||
if Auth.hasSession() then
|
||||
status, status_on = Auth.userLabel() or _("已登录"), true
|
||||
else
|
||||
status, status_on = _("未登录 · 点此登录"), false
|
||||
end
|
||||
return require("ui.themes.widgets.row").widget({
|
||||
width = iw,
|
||||
chevron = true,
|
||||
kind = "nav",
|
||||
icon = "account_circle",
|
||||
title = _("拷贝漫画账号"),
|
||||
status = status,
|
||||
status_on = status_on,
|
||||
callback = function() showLogin() end,
|
||||
})
|
||||
end,
|
||||
}
|
||||
if not Auth.hasSession() then return rows end
|
||||
rows[#rows + 1] = require("source.logout").row(SOURCE_ID, _("确定退出拷贝漫画账号?"), Auth.clearSession)
|
||||
return rows
|
||||
end
|
||||
|
||||
---@return string, boolean
|
||||
function Setting.rowStatus()
|
||||
local Auth = require("moonsource_copymanga.auth")
|
||||
if Auth.hasSession() then
|
||||
return Auth.userLabel() or _("已登录"), true
|
||||
end
|
||||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||
return Client.normalizeBaseUrl(cfg.base_url), true
|
||||
end
|
||||
|
||||
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
|
||||
Setting.login = showLogin
|
||||
|
||||
return Setting
|
||||
@@ -0,0 +1,449 @@
|
||||
--[[--
|
||||
拷贝漫画数据源门面。
|
||||
|
||||
书架是收藏列表,经 Shelf 双向同步;进度双向(章粒度),无远端笔记、统计。
|
||||
一章是一组图片 URL,章节 CBZ 按章节 uuid 命名(分组拼接后章号会整体后移)。重登后用最新 token。
|
||||
|
||||
@module koplugin.book.moonsource_copymanga.source
|
||||
--]]
|
||||
|
||||
local Browse = require("source.browse")
|
||||
local Client = require("moonsource_copymanga.client")
|
||||
local Mapper = require("moonsource_copymanga.mapper")
|
||||
local SourceBase = require("source.base")
|
||||
local Shelf = require("source.shelf")
|
||||
local Toc = require("source.toc")
|
||||
local logger = require("utils.log")
|
||||
local _ = require("l10n").get("moonsource_copymanga")
|
||||
|
||||
local Copymanga = {}
|
||||
|
||||
---@return BookSourceMeta
|
||||
function Copymanga.meta()
|
||||
return require("moonsource_copymanga.loadmeta")()
|
||||
end
|
||||
|
||||
---@class CopymangaSource : SourceBase
|
||||
---@field _client CopymangaClient
|
||||
local Source = setmetatable({ shelf_add = true }, { __index = SourceBase })
|
||||
Source.__index = Source
|
||||
|
||||
---@return CopymangaSource
|
||||
function Copymanga.new()
|
||||
local cfg = require("utils.settings").getSource("copymanga")
|
||||
local meta = Copymanga.meta()
|
||||
local client = Client:new(cfg)
|
||||
-- 新节点只验证了读章节,不能把拿到图片当作云端进度写入成功。
|
||||
local function index(_instance, key)
|
||||
if client.progress_supported == false and (key == "getProgressAsync" or key == "putProgressAsync") then return nil end
|
||||
return Source[key]
|
||||
end
|
||||
return setmetatable({
|
||||
id = meta.id,
|
||||
name = meta.name,
|
||||
type = meta.type,
|
||||
_client = client,
|
||||
}, { __index = index })
|
||||
end
|
||||
|
||||
--- 已登录才算配置好:书架、进度都要令牌;书城浏览匿名可用,不看这里。
|
||||
--- 未登录时删书当场撕墓碑、不入队推脏行(见 SourceBase:deleteBookAsync)。
|
||||
---@return boolean
|
||||
function Source:configured()
|
||||
return self._client:configured() and require("moonsource_copymanga.auth").hasSession()
|
||||
end
|
||||
|
||||
--- API 收藏双向同步,写入后的确认由客户端回读完成。
|
||||
---@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)
|
||||
opts = opts or {}
|
||||
if not require("moonsource_copymanga.auth").hasSession() then
|
||||
return SourceBase.syncBooksAsync(self, opts, cb)
|
||||
end
|
||||
return Shelf.syncAsync(self.id, self._client, nil, opts, cb)
|
||||
end
|
||||
|
||||
---@param client CopymangaClient
|
||||
---@param stable_id string
|
||||
---@param groups { path_word: string, name: string }[]
|
||||
---@param cb fun(chapters: BookChapter[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
local function loadChapters(client, stable_id, groups, cb)
|
||||
return client:chaptersAsync(stable_id, groups, function(rows, err)
|
||||
if not rows then cb(nil, err); return end
|
||||
local chapters = Mapper.chapters(rows)
|
||||
if not chapters then cb(nil, _("漫画目录为空")); return end
|
||||
cb(chapters)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 绕过缓存拉目录并写回。
|
||||
---@param self CopymangaSource
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
local function fetchTocAsync(self, identity, cb)
|
||||
local cancelled, job = false, nil
|
||||
job = self._client:detailAsync(identity.stable_id, function(wire, err)
|
||||
if cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
job = loadChapters(self._client, identity.stable_id, Mapper.groups(wire), function(chapters, chapter_err)
|
||||
if cancelled then return end
|
||||
if not chapters then cb(nil, chapter_err); return end
|
||||
if not Toc.put(identity.source_id, identity.stable_id, chapters) then
|
||||
cb(nil, _("漫画目录保存失败"))
|
||||
return
|
||||
end
|
||||
cb(chapters)
|
||||
end)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
job.cancel()
|
||||
end }
|
||||
end
|
||||
|
||||
--- 书城搜索:回包形状同 listStoreAsync。
|
||||
---@param keyword string
|
||||
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
function Source:searchAsync(keyword, cb)
|
||||
return self._client:searchAsync(keyword, function(wire, err)
|
||||
if not wire then
|
||||
cb(nil, err)
|
||||
return
|
||||
end
|
||||
local books, parse_err = Mapper.search(wire)
|
||||
cb(books and { data = books } or nil, parse_err)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
-- 官网「查看全部分類」的题材(接口 filter/comic/tags 只回前 20 个,其余题材同样可查);漫展场次标签不收。
|
||||
local THEMES = {
|
||||
{ "aiqing", "愛情" }, { "huanlexiang", "歡樂向" }, { "maoxian", "冒險" }, { "qihuan", "奇幻" },
|
||||
{ "baihe", "百合" }, { "xiaoyuan", "校园" }, { "kehuan", "科幻" }, { "dongfang", "東方" },
|
||||
{ "danmei", "耽美" }, { "gedou", "格鬥" }, { "teenslove", "TL" }, { "shenghuo", "生活" },
|
||||
{ "qingxiaoshuo", "轻小说" }, { "xuanyi", "悬疑" }, { "mengxi", "萌系" }, { "shengui", "神鬼" },
|
||||
{ "zhichang", "职场" }, { "zhiyu", "治愈" }, { "jiecao", "节操" }, { "sige", "四格" },
|
||||
{ "gaoxiao", "搞笑" }, { "changtiao", "長條" }, { "jingji", "竞技" }, { "mohuan", "魔幻" },
|
||||
{ "jianniang", "舰娘" }, { "weiniang", "伪娘" }, { "rexue", "热血" }, { "jingsong", "惊悚" },
|
||||
{ "meishi", "美食" }, { "xingzhuanhuan", "性转换" }, { "lizhi", "励志" }, { "hougong", "後宮" },
|
||||
{ "yinyuewudao", "音乐舞蹈" }, { "zhentan", "侦探" }, { "COLOR", "彩色" }, { "aa", "AA" },
|
||||
{ "yishijie", "异世界" }, { "lishi", "历史" }, { "chuanyue", "穿越" }, { "jizhan", "机战" },
|
||||
{ "zhanzheng", "战争" }, { "dushi", "都市" }, { "chongsheng", "重生" }, { "kongbu", "恐怖" },
|
||||
{ "shengcun", "生存" }, { "wuxia", "武侠" }, { "zhaixi", "宅系" }, { "zhuansheng", "转生" },
|
||||
{ "fate", "FATE" }, { "Uncensored", "無修正" }, { "xianxia", "仙侠" }, { "loveLive", "LoveLive" },
|
||||
{ "zazhifuzengxiezhenji", "雜誌附贈寫真集" }, { "qita", "其他" },
|
||||
}
|
||||
|
||||
local PERIODS = { { "day", _("日榜") }, { "week", _("周榜") }, { "month", _("月榜") }, { "total", _("总榜") } }
|
||||
local AUDIENCES = { { "all", _("全部") }, { "male", _("男频") }, { "female", _("女频") } }
|
||||
|
||||
--- 书城浏览:节点 `周期:受众`(缺省 day:all,每页 20 部)或 `theme:<题材>`(按人气)。
|
||||
--- 书城 chip 最多两行:第一行周期,第二行受众 + 题材(放不下的折进「更多」);选题材后周期 / 受众不高亮,点回去就是榜单。
|
||||
---@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 node = opts.node or "day:all"
|
||||
local theme = node:match("^theme:(.+)$")
|
||||
local period, audience = node:match("^(%a+):(%a+)$")
|
||||
local valid_period, valid_audience = false, false
|
||||
for _idx, row in ipairs(PERIODS) do if row[1] == period then valid_period = true end end
|
||||
for _idx, row in ipairs(AUDIENCES) do if row[1] == audience then valid_audience = true end end
|
||||
if not theme and (not valid_period or not valid_audience) then period, audience, node = "day", "all", "day:all" end
|
||||
if theme then period, audience = "day", "all" end
|
||||
local periods, filters = {}, {}
|
||||
for _idx, row in ipairs(PERIODS) do periods[#periods + 1] = { id = row[1] .. ":" .. audience, title = row[2] } end
|
||||
for _idx, row in ipairs(AUDIENCES) do filters[#filters + 1] = { id = period .. ":" .. row[1], title = row[2] } end
|
||||
for _idx, row in ipairs(THEMES) do filters[#filters + 1] = { id = "theme:" .. row[1], title = row[2] } end
|
||||
local groups = { Browse.group(periods, node), Browse.group(filters, node) }
|
||||
return Browse.pages(function(page, done)
|
||||
local function finish(wire, err)
|
||||
if not wire then
|
||||
done(nil, err)
|
||||
return
|
||||
end
|
||||
done(Mapper.search(wire))
|
||||
end
|
||||
local offset = (page - 1) * 20
|
||||
if theme then
|
||||
return self._client:themeAsync(theme, offset, finish)
|
||||
end
|
||||
return self._client:rankAsync(period, audience ~= "all" and audience or nil, offset, finish)
|
||||
end, opts.limit or 60, groups, cb)
|
||||
end
|
||||
|
||||
function Source:getDetailAsync(identity, cb)
|
||||
local cancelled, job = false, nil
|
||||
job = self._client:detailAsync(identity.stable_id, function(wire, err)
|
||||
if cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
local book = Mapper.detail(wire)
|
||||
if not book then cb(nil, _("漫画详情解析失败")); return end
|
||||
local existing = require("book.books_db").get(self.id, identity.stable_id)
|
||||
if existing then
|
||||
book.deleted = existing.deleted
|
||||
end
|
||||
local progress = require("book.progress_db").get(self.id, identity.stable_id)
|
||||
if progress then
|
||||
book.percent = require("book.progress").clampPercent(progress.fraction, false, true)
|
||||
end
|
||||
local function finish()
|
||||
require("book.store").rememberMany({ book })
|
||||
cb(book)
|
||||
end
|
||||
if Toc.read(self.id, identity.stable_id) then
|
||||
finish()
|
||||
return
|
||||
end
|
||||
-- 顺手预热目录:详情回包里已有分组,省一次 comic2。
|
||||
job = loadChapters(self._client, identity.stable_id, Mapper.groups(wire), function(chapters)
|
||||
if cancelled then return end
|
||||
if chapters and not Toc.put(self.id, identity.stable_id, chapters) then
|
||||
logger.warn("copymanga toc persist failed", identity.stable_id)
|
||||
end
|
||||
finish()
|
||||
end)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
job.cancel()
|
||||
end }
|
||||
end
|
||||
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
||||
---@param force boolean|nil 真=跳过缓存强制拉
|
||||
---@return { cancel: fun() }|nil
|
||||
function Source:loadTocAsync(identity, cb, force)
|
||||
if not force then
|
||||
local cached = Toc.read(identity.source_id, identity.stable_id)
|
||||
if cached and #cached > 0 then
|
||||
return SourceBase.defer(cb, cached)
|
||||
end
|
||||
end
|
||||
return fetchTocAsync(self, identity, cb)
|
||||
end
|
||||
|
||||
Source.refreshTocAsync = fetchTocAsync
|
||||
|
||||
--- 起始章:显式章号 > 本地进度章号 > 本地进度全书比例 > 第一章。
|
||||
---@param identity BookIdentity
|
||||
---@param opts table
|
||||
---@param toc BookChapter[]
|
||||
---@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_copymanga.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)
|
||||
active = Chapter.materializeAsync(self._client, identity, fresh[idx],
|
||||
function(done, total)
|
||||
if dialog and total > 0 then dialog:update(_("正在准备漫画…"), done, total) 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
|
||||
|
||||
--- 阅读中后台预取后续章节 CBZ。会话侧固定预取后面 3 章。
|
||||
---@param identity BookIdentity
|
||||
---@param toc BookChapter[]
|
||||
---@param from_idx integer
|
||||
---@param count integer
|
||||
---@param cb fun()|nil
|
||||
---@return { cancel: fun() }
|
||||
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
||||
return require("moonsource_copymanga.chapter").prefetchAsync(
|
||||
self._client, identity, toc, from_idx, count, nil, cb
|
||||
)
|
||||
end
|
||||
|
||||
--- 拉取云端浏览记录,用目录把 chapter_uuid 换成本地章序号。
|
||||
--- 官方只有章粒度,没有页内坐标。
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(pos: ProgressPosition|nil, err: string|nil, meta: table|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:getProgressAsync(identity, cb)
|
||||
if not require("moonsource_copymanga.auth").hasSession() then
|
||||
return SourceBase.defer(cb, nil, nil, { empty = true })
|
||||
end
|
||||
local cancelled, toc_job = false, nil
|
||||
local request = self._client:getProgressAsync(identity.stable_id, function(wire, err)
|
||||
if cancelled then return end
|
||||
if not wire then cb(nil, err); return end
|
||||
local pos, uid = Mapper.progress(wire)
|
||||
if not pos or not uid then cb(nil, nil, { empty = true }); return end
|
||||
|
||||
local function finish(idx)
|
||||
if not idx then cb(nil, nil, { empty = true }); return end
|
||||
pos.chapter_idx = idx
|
||||
pos.fraction = Toc.wholeFraction(
|
||||
identity.source_id, identity.stable_id, idx, pos.chapter_fraction
|
||||
) or pos.fraction
|
||||
pos.extra = { chapter_uid = uid, chapter_idx = idx }
|
||||
cb(pos)
|
||||
end
|
||||
|
||||
local idx = Toc.index(identity.source_id, identity.stable_id, uid)
|
||||
if idx then
|
||||
finish(idx)
|
||||
return
|
||||
end
|
||||
-- 缓存目录里没有这一话:多半是别的设备读了新更新的章节,缓存还没过期也得重拉。
|
||||
toc_job = fetchTocAsync(self, identity, function(toc, toc_err)
|
||||
if cancelled then return end
|
||||
if not toc then cb(nil, toc_err); return end
|
||||
finish(Toc.index(identity.source_id, identity.stable_id, uid))
|
||||
end)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
request.cancel()
|
||||
if toc_job then toc_job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
--- 推送当前章到云端。官方没有独立浏览写入接口;
|
||||
--- 带 Token 的 chapter2 GET 就是打开章节时写入浏览记录的副作用。
|
||||
---@param identity BookIdentity
|
||||
---@param pos ProgressPosition
|
||||
---@param cb fun(ok: boolean|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:putProgressAsync(identity, pos, cb)
|
||||
if not require("moonsource_copymanga.auth").hasSession() then
|
||||
return SourceBase.defer(cb, nil, _("请先登录拷贝漫画账号"))
|
||||
end
|
||||
local chapter_idx = pos.chapter_idx or identity.chapter_idx
|
||||
if not chapter_idx then
|
||||
return SourceBase.defer(cb, nil, _("缺少章节信息"))
|
||||
end
|
||||
local cancelled, toc_job, push_job = false, nil, nil
|
||||
|
||||
local function push(uid)
|
||||
if not uid then
|
||||
cb(nil, _("缺少章节信息"))
|
||||
return
|
||||
end
|
||||
push_job = self._client:chapterAsync(identity.stable_id, uid, function(wire, err)
|
||||
if cancelled then return end
|
||||
cb(wire and true or nil, err)
|
||||
end)
|
||||
end
|
||||
|
||||
local extra = pos.extra
|
||||
local uid = extra and extra.chapter_idx == chapter_idx and extra.chapter_uid
|
||||
or Toc.uid(identity.source_id, identity.stable_id, chapter_idx)
|
||||
if uid then
|
||||
push(uid)
|
||||
else
|
||||
toc_job = self:loadTocAsync(identity, function(toc, err)
|
||||
if cancelled then return end
|
||||
if not toc then cb(nil, err); return end
|
||||
push(Toc.uid(identity.source_id, identity.stable_id, chapter_idx))
|
||||
end)
|
||||
end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if toc_job then toc_job.cancel() end
|
||||
if push_job then push_job.cancel() end
|
||||
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 cancelled, active = false, nil
|
||||
active = self:loadTocAsync(identity, function(toc, err)
|
||||
if 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)
|
||||
active = require("moonsource_copymanga.chapter").prefetchAsync(
|
||||
self._client, identity, toc, from_idx, count,
|
||||
{
|
||||
progress = on_progress,
|
||||
interval_seconds = 1.5,
|
||||
},
|
||||
function(cached, total, failed, last_err)
|
||||
if not cancelled then
|
||||
cb(failed == 0, cached, last_err, total, failed)
|
||||
end
|
||||
end
|
||||
)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
active.cancel()
|
||||
end }
|
||||
end
|
||||
|
||||
return Copymanga
|
||||
@@ -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