From 875dc70861b1391f6ac6784e44e9e21b57cf9dbc Mon Sep 17 00:00:00 2001 From: Garfield Date: Fri, 27 Nov 2026 07:44:00 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E4=B9=A6?= =?UTF-8?q?=E6=9E=B6=E4=B8=8D=E5=90=8C=E6=AD=A5=E3=80=81=E7=AB=A0=E8=8A=82?= =?UTF-8?q?=E6=89=93=E4=B8=8D=E5=BC=80=E4=B8=8E=E7=99=BB=E5=BD=95=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E8=AF=AF=E5=88=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 书架改走月读 Shelf.syncAsync:收藏夹分页拉取并写入书库,支持加 / 删收藏 - 开书改用主题进度窗,修 ui/widgets/progressdialog 不存在导致的崩溃 - 章节首次下载回调传路径而不是表,修新章第一次打开必失败 - 站点按页加密的图换成提示页,不再整话放弃 - 源实例元表改为继承 SourceBase,修登录后仍显示未登录 --- moonsource-bilimanga.koplugin/README.md | 17 ++ .../moonsource_bilimanga/chapter.lua | 90 +++++- .../moonsource_bilimanga/encrypted.png | Bin 0 -> 11621 bytes .../moonsource_bilimanga/source.lua | 273 +++++++++--------- 4 files changed, 236 insertions(+), 144 deletions(-) create mode 100644 moonsource-bilimanga.koplugin/README.md create mode 100644 moonsource-bilimanga.koplugin/moonsource_bilimanga/encrypted.png diff --git a/moonsource-bilimanga.koplugin/README.md b/moonsource-bilimanga.koplugin/README.md new file mode 100644 index 0000000..ed054cd --- /dev/null +++ b/moonsource-bilimanga.koplugin/README.md @@ -0,0 +1,17 @@ +# 哔哩哔哩漫画 + +| 能力 | 是否支持 | 备注 | +| --- | --- | --- | +| 浏览器登录 | 已实现,未实测 | 打开漫画账号页采集适用于该站的 Cookie,再用主站 nav 校验后才保存 | +| 粘贴 Cookie | 已实现,未实测 | 须是完整 Cookie;校验通过才写入。旧的未校验 SESSDATA 仍可继续用,直到重新登录 | +| 账号切换 | 部分支持 | 同账号续期保留本地记录;换号按月读账号边界清理未同步数据 | +| 书架拉取 | 支持 | 需要已登录会话;已用真实账号拉取收藏并写入书库 | +| 书架新增与移出 | 已实现,未实测 | 收藏接口已接,未做写后回读 | +| 搜索 | 支持 | 公开搜索 | +| 分类书城 | 部分支持 | 题材和排序来自站点分类接口。公开分类页已取到 15 本;筛选翻页已核对,设备上的书城页未点过 | +| 详情与目录 | 支持 | 目录顺序与话 ID 来自详情 | +| 免费话 | 部分支持 | 站点会按页加密少数页(免费话也有),这些页解不了,用提示页占位,其余页照常阅读 | +| 已购与大会员话 | 部分支持 | 只认登录后详情里的 is_locked。未锁定才下载,锁定报本章未购买。未用真实大会员会话实测 | +| 阅读进度同步 | 已实现,未实测 | 需要已登录会话,按话记录 | +| 书评、章评、段评 | 不支持 | 当前版本未接 | +| 离线阅读 | 支持 | 已缓存的话可离线打开 | diff --git a/moonsource-bilimanga.koplugin/moonsource_bilimanga/chapter.lua b/moonsource-bilimanga.koplugin/moonsource_bilimanga/chapter.lua index 90d754c..d612dac 100644 --- a/moonsource-bilimanga.koplugin/moonsource_bilimanga/chapter.lua +++ b/moonsource-bilimanga.koplugin/moonsource_bilimanga/chapter.lua @@ -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 { diff --git a/moonsource-bilimanga.koplugin/moonsource_bilimanga/encrypted.png b/moonsource-bilimanga.koplugin/moonsource_bilimanga/encrypted.png new file mode 100644 index 0000000000000000000000000000000000000000..1044e55e615e702caf850e5afd06b1425509dc17 GIT binary patch literal 11621 zcmeI2XIN9)y6;g@EDJ@YC|v=O8UZOv38<7%MCl!s-lao;WFbqD79cd~y@g&vhbX;= z9%+e4?ge zI`V-7^9SN7+0TL+;|}*siQ~6>@a4C2mpcbFXc#Ula?=FU(4PNq2F>|%mua~fepmdf z@xQzM=Y#*J+kck)H`<^4ztaAE@L$C$a<|a_tBn7lru{Qm+V+13U@|fn z-Kf&{v4@n!6mnx3h%V)cLA4K>nJ+Z8*jGKBylQPuo@n^NwST@Kv0xmUpF67j9K8zFST{6))05tvaM4S692(5pCU<2Kq z(m3ewPocMK06>oPzh;|EUd@9|TdXz|HZEw?b7-h~wua3M5620qRlmaozzT9KqzpNbZIl!z)G^h@Mk2#d`z zOUji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$HwYIi;1#9%>sdBfFQUf$4S@h;&O^ zv(rnqLe2Zt$|3!unV$&Or+=_im)_5nhIanZL8!YL@Z+u%p!sAseGbsJ8Hndf@v3Cm z)TTC;ZIn5l5?+SeM6I$iUtXeqjUL)gsl@m@Sb(N*XR%~Ye@7<>f3zaySpv2>?2NHi z*lsNCD#)KJutI~{Eg*u%v^73OGg9(Gq7H8xWcM@7`qT%uy_@zG0Cjy-nfSd9x)|?z z_NY$hu6(rBm=ZVt)?o}P&Wh7-XJfmoYhPr>OPeTLSo^R_u93|2x<_LD4S@Np)c>u# z@K2TCUyZ-Yy8f<9{Hs{;Pyc_gr+xn1pS6EA{t@ 本地待推进度 > 第一章。 +---@param identity BookIdentity +---@param opts table +---@param toc table[] +---@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 or 0) * #toc) + 1) + end + return math.max(1, math.min(#toc, idx or 1)) +end + function BiliManga:openBookAsync(identity, opts, cb) opts = opts or {} - SOURCE_ID = identity.source_id or SOURCE_ID - local cancelled = false - local active = nil - local dialog = nil - - local function startIndex(toc) - if identity.chapter_idx and identity.chapter_idx > 0 then - return math.min(identity.chapter_idx, #toc) - end - return 1 - end - local function finish(path, idx) local ok, err = require("book.store").touch(path, identity, { chapter_idx = idx, @@ -101,105 +101,55 @@ function BiliManga:openBookAsync(identity, opts, cb) }) cb(ok and path or nil, err) end - - local function closeDialog() - if dialog then - pcall(dialog.dismiss) - dialog = nil - end - end - - -- 检查缓存 - local toc = Toc.read(SOURCE_ID, identity.stable_id) + local toc = Toc.read(identity.source_id, identity.stable_id) if toc and #toc > 0 then - local idx = startIndex(toc) + 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 - -- 异步:拉目录 → 下载CBZ + local cancelled, active, dialog = false, nil, nil + local function closeDialog() + if dialog then dialog:close(); dialog = nil end + end require("ui/uimanager"):nextTick(function() - local ProgressDialog = require("ui/widgets/progressdialog") - dialog = ProgressDialog:new{ - title = identity.book and identity.book.title or "", - text = _("正在准备漫画…"), - progress = { min = 0, max = 1 }, + 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() - - toc = Toc.read(SOURCE_ID, identity.stable_id) - if not toc or #toc == 0 then - local retries = 0 - local MAX_RETRIES = 3 - - local function loadTocWithRetry() + dialog:set(_("正在准备漫画…")) + local loaded = false + local toc_job = self:loadTocAsync(identity, function(fresh, err) + loaded = true + active = nil + if cancelled then return end + if not fresh then closeDialog(); cb(nil, err or _("加载目录失败")); return end + local idx = startIndex(identity, opts, fresh) + local materialized = false + local chapter_job = Chapter.materializeAsync(identity, fresh[idx], function(done, total) + if dialog and total > 0 then dialog:update(_("正在准备漫画…"), done, total) end + end, function(path, materialize_err) + materialized = true + active = nil + closeDialog() if cancelled then return end - retries = retries + 1 - if retries > MAX_RETRIES then - closeDialog() - cb(nil, _("加载目录失败")) - return - end - self:refreshTocAsync(identity, function(toc_data, toc_err) - if cancelled then return end - if not toc_data then - require("ui/uimanager"):scheduleIn(2, loadTocWithRetry) - return - end - toc = toc_data - local idx = startIndex(toc) - local chapter_job = Chapter.materializeAsync(identity, toc[idx], - function(done, total) - if cancelled then return end - dialog:update(_("正在准备漫画…"), done, total) - end, - function(path, materialize_err) - if cancelled then return end - closeDialog() - if not path then - cb(nil, materialize_err or _("章节图片解析失败")) - return - end - finish(path, idx) - end - ) - active = chapter_job - end, true) - end - loadTocWithRetry() - else - local idx = startIndex(toc) - local chapter_job = Chapter.materializeAsync(identity, toc[idx], - function(done, total) - if cancelled then return end - dialog:update(_("正在准备漫画…"), done, total) - end, - function(path, materialize_err) - if cancelled then return end - closeDialog() - if not path then - cb(nil, materialize_err or _("章节图片解析失败")) - return - end - finish(path, idx) - end - ) - active = chapter_job - end + if not path then cb(nil, materialize_err or _("章节图片解析失败")); return end + finish(path, idx) + end) + if not materialized then active = chapter_job end + end) + if not loaded then active = toc_job end end) - - return { - cancel = function() - cancelled = true - if active then active.cancel() end - closeDialog() - end, - } + return { cancel = function() + cancelled = true + if active then active.cancel() end + closeDialog() + end } end function BiliManga:prefetchChaptersAsync(identity, toc, from_idx, count, cb) @@ -298,40 +248,103 @@ function BiliManga:putProgressAsync(identity, pos, cb) return { cancel = function() end } end -function BiliManga:syncBooksAsync(opts, cb) - local cfg = require("utils.settings").getSource("bilimanga") - if not cfg.cookie or cfg.cookie == "" then - return SourceBase.defer(cb, { data = {} }) - end +BiliManga.shelf_add = true - Client.listFavoriteAsync(1, 100, function(wire, err) - if err then cb(nil, err); return end - local items = Mapper.favoriteItems(wire) - local books = {} - for i = 1, #items do - local book = Mapper.bookFromItem(items[i]) - if book then - book.read_at = items[i].read_time or 0 - book.inserted_at = items[i].fav_time or 0 +--- 收藏夹即远端书架:Shelf.syncAsync 要的 shelfSyncAsync / addToShelfAsync / removeFromShelfAsync。 +local Favorites = {} +Favorites.__index = Favorites + +local FAVORITE_PAGE = 50 + +--- "2026-11-27 00:40:55"(北京时间)→ 时间戳;空串 / 格式不对为 0。 +---@param text any +---@return integer +local function beijingTime(text) + local y, mo, d, h, mi, s = tostring(text or ""):match("^(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)$") + if not y then return 0 end + local as_local = os.time({ year = tonumber(y), month = tonumber(mo), day = tonumber(d), + hour = tonumber(h), min = tonumber(mi), sec = tonumber(s) }) + local local_offset = os.time(os.date("*t", as_local)) - os.time(os.date("!*t", as_local)) + return as_local + local_offset - 8 * 3600 +end + +--- 整个收藏夹(逐页拉,接口的 total 不可靠,按满页判断还有没有下一页)。 +---@param cb fun(books: Book[]|nil, err: string|nil) +function Favorites:shelfSyncAsync(cb) + local books, page = {}, 1 + local function nextPage() + self.job = Client.listFavoriteAsync(page, FAVORITE_PAGE, function(wire, err) + self.job = nil + if self.cancelled then return end + local items = wire and Mapper.favoriteItems(wire) + if not items then cb(nil, err or _("响应无效")); return end + for _idx, item in ipairs(items) do + local book = Mapper.bookFromItem(item) + if not book then cb(nil, _("响应无效")); return end + book.read_at = beijingTime(item.latest_read_time) books[#books + 1] = book end - end - cb({ data = books }) - end) + if #items < FAVORITE_PAGE then cb(books); return end + page = page + 1 + nextPage() + end) + end + nextPage() + return self +end - return { cancel = function() end } +function Favorites:addToShelfAsync(stable_id, cb) + self.job = Client.addFavoriteAsync(stable_id, cb) + return self +end + +function Favorites:removeFromShelfAsync(stable_id, cb) + self.job = Client.deleteFavoriteAsync(stable_id, cb) + return self +end + +function Favorites:cancel() + self.cancelled = true + if self.job then self.job.cancel() end +end + +function BiliManga:syncBooksAsync(opts, cb) + if not Client.hasAccount() then return SourceBase.skipped(cb, "not_logged_in") end + local remote = setmetatable({ cancelled = false }, Favorites) + local job = require("source.shelf").syncAsync(self.id, remote, nil, opts, cb) + return { cancel = function() + remote:cancel() + job:cancel() + end } end function BiliManga:browseAsync(opts, cb) - if opts and opts.browse_type == "home" then - Client.homeHotAsync(function(wire, err) - if err then cb(nil, err); return end - local books = Mapper.searchResults(wire) - cb({ data = books }) + opts = opts or {} + local limit = opts.limit or 60 + local state = { cancelled = false, job = nil } + state.job = Client.allLabelAsync(function(wire, err) + if state.cancelled then return end + local labels = Mapper.labels(wire) + if err or not labels then cb(nil, err or _("响应无效")); return end + local order, style = Mapper.storeChoice(opts.node, labels) + local groups = Mapper.storeGroups(labels, order, style) + state.job = require("source.browse").pages(function(page, done) + return Client.classPageAsync({ order = order, style_id = style }, page, function(rows, page_err) + if state.cancelled then return end + if page_err then done(nil, page_err); return end + local books = Mapper.books(rows) + if not books then done(nil, _("响应无效")); return end + done(books, nil, #books == 15) + end) + end, limit, groups, function(result, reason) + state.job = nil + if not state.cancelled then cb(result, reason) end end) - return { cancel = function() end } - end - return SourceBase.defer(cb, { data = {} }) + end) + return { cancel = function() + state.cancelled = true + if state.job and state.job.cancel then state.job.cancel() end + end } end return BiliManga