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 0000000..1044e55 Binary files /dev/null and b/moonsource-bilimanga.koplugin/moonsource_bilimanga/encrypted.png differ diff --git a/moonsource-bilimanga.koplugin/moonsource_bilimanga/source.lua b/moonsource-bilimanga.koplugin/moonsource_bilimanga/source.lua index 55bf707..f49a30a 100644 --- a/moonsource-bilimanga.koplugin/moonsource_bilimanga/source.lua +++ b/moonsource-bilimanga.koplugin/moonsource_bilimanga/source.lua @@ -5,7 +5,8 @@ local Chapter = require("moonsource_bilimanga.chapter") local Toc = require("source.toc") local _ = require("moonsource_bilimanga.l10n") -local BiliManga = {} +local BiliManga = setmetatable({}, { __index = SourceBase }) +BiliManga.__index = BiliManga local SOURCE_ID function BiliManga.meta() @@ -15,10 +16,7 @@ end function BiliManga.new() local meta = BiliManga.meta() SOURCE_ID = meta.id - local source = setmetatable({ id = meta.id, name = meta.name, type = meta.type }, { - __index = SourceBase, - }) - return source + return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, BiliManga) end function BiliManga:configured() @@ -45,7 +43,7 @@ function BiliManga:getDetailAsync(identity, cb) return end - return Client.comicDetailLiteAsync(identity.stable_id, function(info, err) + return Client.comicDetailAsync(identity.stable_id, function(info, err) if err then cb(nil, err); return end local book = Mapper.book(info) require("book.store").rememberDetail(book) @@ -80,20 +78,22 @@ function BiliManga:refreshTocAsync(identity, cb) end) end +--- 起始章:调用方指定的章 > 本地待推进度 > 第一章。 +---@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