书城搜索与筛选、详情、卷列表、整卷 EPUB 下载与缓存、收藏书架同步。 支持邮箱密码、浏览器授权、粘贴 Cookie 登录;下载要求 UA 与登录时一致, 会话 UA 随 Cookie 一起保存。
120 lines
4.2 KiB
Lua
120 lines
4.2 KiB
Lua
--[[--
|
|
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
|