🐛 修复书架不同步、章节打不开与登录状态误判
- 书架改走月读 Shelf.syncAsync:收藏夹分页拉取并写入书库,支持加 / 删收藏 - 开书改用主题进度窗,修 ui/widgets/progressdialog 不存在导致的崩溃 - 章节首次下载回调传路径而不是表,修新章第一次打开必失败 - 站点按页加密的图换成提示页,不再整话放弃 - 源实例元表改为继承 SourceBase,修登录后仍显示未登录
This commit is contained in:
1 parent
4a031e00c5
commit
875dc70861
4 files changed
+236
-144
No files matched your search
@@ -2,11 +2,24 @@ local Cbz = require("source.cbz")
|
||||
local Image = require("utils.image")
|
||||
local Paths = require("utils.paths")
|
||||
local Client = require("moonsource_bilimanga.client")
|
||||
local Mapper = require("moonsource_bilimanga.mapper")
|
||||
local _ = require("moonsource_bilimanga.l10n")
|
||||
|
||||
local Chapter = {}
|
||||
|
||||
-- 站点按页加密(首字节 0x08,免费章也有),解不了;这页换成提示图,其余页照常成章。
|
||||
local ENCRYPTED_PAGE = debug.getinfo(1, "S").source:sub(2):match("^(.*)/") .. "/encrypted.png"
|
||||
|
||||
local function copyFile(from, to)
|
||||
local src = assert(io.open(from, "rb"))
|
||||
local data = src:read("*a")
|
||||
src:close()
|
||||
local dst = io.open(to, "wb")
|
||||
if not dst then return false end
|
||||
dst:write(data)
|
||||
dst:close()
|
||||
return true
|
||||
end
|
||||
|
||||
function Chapter.archivePath(identity, chapter)
|
||||
return Paths.bookWorkDir(identity.stable_id, identity.source_id)
|
||||
.. "/" .. chapter.uid .. ".cbz"
|
||||
@@ -29,24 +42,64 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
||||
return { cancel = function() state.cancelled = true end }
|
||||
end
|
||||
|
||||
local function log(msg)
|
||||
pcall(require("logger").getLogger, "bilimanga")
|
||||
if chapter.vip then
|
||||
require("ui/uimanager"):nextTick(function()
|
||||
if not state.cancelled then cb(nil, _("本章未购买")) end
|
||||
end)
|
||||
return { cancel = function() state.cancelled = true end }
|
||||
end
|
||||
|
||||
Client.imageIndexAsync(chapter.uid, function(wire, err)
|
||||
local function withCode(url)
|
||||
if url:find("code=DanmakuInfo", 1, true) then return url end
|
||||
if url:find("?", 1, true) then return url .. "&code=DanmakuInfo" end
|
||||
return url .. "?code=DanmakuInfo"
|
||||
end
|
||||
|
||||
state.active = Client.imageIndexAsync(chapter.uid, function(wire, err)
|
||||
if state.cancelled then return end
|
||||
state.active = nil
|
||||
if err then cb(nil, err); return end
|
||||
local urls = Mapper.images(wire)
|
||||
if not urls or #urls == 0 then
|
||||
local paths = {}
|
||||
local images = wire and wire.images or {}
|
||||
for i = 1, #images do
|
||||
local path = images[i] and images[i].path
|
||||
if type(path) == "string" and path ~= "" then
|
||||
paths[#paths + 1] = path
|
||||
end
|
||||
end
|
||||
if #paths == 0 then
|
||||
cb(nil, _("章节图片解析失败"))
|
||||
return
|
||||
end
|
||||
|
||||
state.active = Client.imageTokenAsync(paths, function(items, token_err)
|
||||
if state.cancelled then return end
|
||||
state.active = nil
|
||||
if token_err then cb(nil, token_err); return end
|
||||
if type(items) ~= "table" or #items ~= #paths then
|
||||
cb(nil, _("章节图片解析失败"))
|
||||
return
|
||||
end
|
||||
local urls = {}
|
||||
for i = 1, #items do
|
||||
local item = items[i]
|
||||
if type(item) ~= "table" then
|
||||
cb(nil, _("章节图片解析失败"))
|
||||
return
|
||||
end
|
||||
if type(item.complete_url) ~= "string" or item.complete_url == "" then
|
||||
cb(nil, _("章节图片解析失败"))
|
||||
return
|
||||
end
|
||||
urls[i] = withCode(item.complete_url)
|
||||
end
|
||||
|
||||
local image_dir = archive_path:gsub("%.cbz$", "")
|
||||
Paths.ensureDir(image_dir)
|
||||
|
||||
local paths = {}
|
||||
local files = {}
|
||||
for i, url in ipairs(urls) do
|
||||
paths[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg")
|
||||
files[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg")
|
||||
end
|
||||
|
||||
local current = 1
|
||||
@@ -54,16 +107,14 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
||||
if state.cancelled then return end
|
||||
local url = urls[current]
|
||||
if not url then
|
||||
Cbz.build(paths, archive_path, state, function(path, cbz_err)
|
||||
Cbz.build(files, archive_path, state, function(path, cbz_err)
|
||||
if state.cancelled then return end
|
||||
if cbz_err then log("Cbz.build error: " .. tostring(cbz_err)) end
|
||||
cb(path and { title = chapter.title, file = path } or nil,
|
||||
cbz_err or (state.cancelled and nil or _("图片下载失败")))
|
||||
if path then cb(path) else cb(nil, cbz_err or _("图片下载失败")) end
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
local path = paths[current]
|
||||
local path = files[current]
|
||||
if Image.ok(path) then
|
||||
if on_progress then on_progress(current, #urls) end
|
||||
current = current + 1
|
||||
@@ -83,7 +134,17 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
||||
downloaded = true
|
||||
state.active = nil
|
||||
if state.cancelled then return end
|
||||
if not ok or not Image.ok(path) then
|
||||
local fh = io.open(path, "rb")
|
||||
local magic = fh and fh:read(1)
|
||||
if fh then fh:close() end
|
||||
if magic == "\8" then
|
||||
os.remove(path)
|
||||
files[current] = path:gsub("%.[^./]+$", "") .. ".png"
|
||||
if not copyFile(ENCRYPTED_PAGE, files[current]) then
|
||||
cb(nil, _("图片下载失败"))
|
||||
return
|
||||
end
|
||||
elseif not ok or not Image.ok(path) then
|
||||
os.remove(path)
|
||||
cb(nil, download_err and tostring(download_err) or _("图片下载失败"))
|
||||
return
|
||||
@@ -95,6 +156,7 @@ function Chapter.materializeAsync(identity, chapter, on_progress, cb)
|
||||
if not downloaded then state.active = handle end
|
||||
end
|
||||
nextImage()
|
||||
end)
|
||||
end)
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user