local Cbz = require("source.cbz") local Image = require("utils.image") local Paths = require("utils.paths") local Client = require("moonsource_bilimanga.client") 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" end function Chapter.existing(identity, chapter) local path = Chapter.archivePath(identity, chapter) return Cbz.ready(path) and path or nil end function Chapter.materializeAsync(identity, chapter, on_progress, cb) local archive_path = Chapter.archivePath(identity, chapter) local state = { cancelled = false, active = nil } if Cbz.ready(archive_path) then require("ui/uimanager"):nextTick(function() if not state.cancelled then cb(archive_path) end end) return { cancel = function() state.cancelled = true end } end 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 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 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 files = {} for i, url in ipairs(urls) do files[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg") end local current = 1 local function nextImage() if state.cancelled then return end local url = urls[current] if not url then Cbz.build(files, archive_path, state, function(path, cbz_err) if state.cancelled then return end if path then cb(path) else cb(nil, cbz_err or _("图片下载失败")) end end) return end local path = files[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("bilimanga image", nextImage)) return end local downloaded = false local handle = require("http.request").download({ url = url, headers = { ["Referer"] = "https://manga.bilibili.com/", }, timeout = require("http.timeout").net(3), max_bytes = 32 * 1024 * 1024, }, path, function(ok, download_err) downloaded = true state.active = nil if state.cancelled then return end 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 end if on_progress then on_progress(current, #urls) end current = current + 1 require("ui/uimanager"):nextTick(require("ui.guard").wrap("bilimanga image", nextImage)) end) if not downloaded then state.active = handle end end nextImage() end) end) return { cancel = function() state.cancelled = true if state.active then state.active.cancel() end end, } end function Chapter.fetcher(source) return function(identity, chapter, cb) local existing = Chapter.existing(identity, chapter) if existing then require("ui/uimanager"):nextTick(function() cb({ title = chapter.title, file = existing }) end) return { cancel = function() end } end local result = Chapter.materializeAsync(identity, chapter, nil, function(path, err) if path then cb({ title = chapter.title, file = path }) else cb(nil, err) end end) return result end end return Chapter