✨ (bilimanga): 新增哔哩哔哩漫画书源并收进 src/
附协议分析笔记,沿用统一的 update.lua 与打包脚本。 Co-authored-by: Copilot <[email protected]>
This commit is contained in:
commit
f9d07c95fb
17 files changed
+2455
No files matched your search
@@ -0,0 +1,337 @@
|
||||
local SourceBase = require("source.base")
|
||||
local Client = require("moonsource_bilimanga.client")
|
||||
local Mapper = require("moonsource_bilimanga.mapper")
|
||||
local Chapter = require("moonsource_bilimanga.chapter")
|
||||
local Toc = require("source.toc")
|
||||
local _ = require("moonsource_bilimanga.l10n")
|
||||
|
||||
local BiliManga = {}
|
||||
local SOURCE_ID
|
||||
|
||||
function BiliManga.meta()
|
||||
return require("moonsource_bilimanga.loadmeta")()
|
||||
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
|
||||
end
|
||||
|
||||
function BiliManga:configured()
|
||||
return Client.hasAccount()
|
||||
end
|
||||
|
||||
function BiliManga:searchAsync(keyword, cb)
|
||||
return Client.searchAsync(keyword, 1, 20, function(wire, err)
|
||||
if err then cb(nil, err); return end
|
||||
local books = Mapper.searchResults(wire)
|
||||
if not books or #books == 0 then
|
||||
cb(nil, _("无搜索结果"))
|
||||
return
|
||||
end
|
||||
cb({ data = books })
|
||||
end)
|
||||
end
|
||||
|
||||
function BiliManga:getDetailAsync(identity, cb)
|
||||
local existing = identity.book
|
||||
or require("book.books_db").get(identity.source_id or SOURCE_ID, identity.stable_id)
|
||||
if existing then
|
||||
cb(require("book.store").rememberDetail(existing))
|
||||
return
|
||||
end
|
||||
|
||||
return Client.comicDetailLiteAsync(identity.stable_id, function(info, err)
|
||||
if err then cb(nil, err); return end
|
||||
local book = Mapper.book(info)
|
||||
require("book.store").rememberDetail(book)
|
||||
cb(book)
|
||||
end)
|
||||
end
|
||||
|
||||
function BiliManga:loadTocAsync(identity, cb, force)
|
||||
local tid = identity.source_id or SOURCE_ID
|
||||
if not force then
|
||||
local cached = Toc.read(tid, identity.stable_id)
|
||||
if cached and #cached > 0 then
|
||||
return SourceBase.defer(cb, cached)
|
||||
end
|
||||
end
|
||||
return self:refreshTocAsync(identity, cb)
|
||||
end
|
||||
|
||||
function BiliManga:refreshTocAsync(identity, cb)
|
||||
return Client.comicDetailAsync(identity.stable_id, function(info, err)
|
||||
if err then cb(nil, err); return end
|
||||
local chapters = Mapper.chapters(info)
|
||||
if not chapters or #chapters == 0 then
|
||||
cb(nil, _("加载目录失败"))
|
||||
return
|
||||
end
|
||||
Toc.put(identity.source_id or SOURCE_ID, identity.stable_id, chapters)
|
||||
if identity.book and not identity.book._toc_fetched then
|
||||
info._toc_fetched = true
|
||||
end
|
||||
cb(chapters)
|
||||
end)
|
||||
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,
|
||||
book = identity.book,
|
||||
})
|
||||
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)
|
||||
if toc and #toc > 0 then
|
||||
local idx = startIndex(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
|
||||
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 },
|
||||
}
|
||||
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()
|
||||
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
|
||||
end)
|
||||
|
||||
return {
|
||||
cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
closeDialog()
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
function BiliManga:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
||||
return require("source.chapter").prefetchAsync(
|
||||
identity, identity.book, toc, from_idx, count, {
|
||||
fetchContent = Chapter.fetcher(self),
|
||||
interval_seconds = 1.0,
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function BiliManga:cacheAllChaptersAsync(identity, on_progress, cb, range)
|
||||
return require("source.chapter").cacheAllAsync(
|
||||
self, identity,
|
||||
Chapter.fetcher(self),
|
||||
on_progress, cb, 1.0, range)
|
||||
end
|
||||
|
||||
function BiliManga:getProgressAsync(identity, cb)
|
||||
local cfg = require("utils.settings").getSource("bilimanga")
|
||||
if not cfg.cookie or cfg.cookie == "" then
|
||||
return SourceBase.defer(cb, nil, nil, { empty = true })
|
||||
end
|
||||
|
||||
local remote = Client.newSync and Client.newSync(false) or { cancelled = false, cancel = function() end }
|
||||
Client.getUserInfoAsync(function(wire, err)
|
||||
if remote.cancelled then return end
|
||||
if err then cb(nil, err); return end
|
||||
|
||||
Client.listHistoryAsync(1, 50, function(hist_wire, hist_err)
|
||||
if remote.cancelled then return end
|
||||
if hist_err then cb(nil, hist_err); return end
|
||||
|
||||
local item = Mapper.historyItem(hist_wire, identity.stable_id)
|
||||
if not item then
|
||||
cb(nil, nil, { empty = true })
|
||||
return
|
||||
end
|
||||
|
||||
local last_ep_id = item.last_ep_id or item.ep_id
|
||||
if not last_ep_id then
|
||||
cb(nil, nil, { empty = true })
|
||||
return
|
||||
end
|
||||
|
||||
local function afterToc(toc, toc_err)
|
||||
if remote.cancelled then return end
|
||||
if not toc then cb(nil, toc_err); return end
|
||||
|
||||
for i, ch in ipairs(toc) do
|
||||
if ch.uid == tostring(last_ep_id) then
|
||||
cb({
|
||||
chapter_idx = i,
|
||||
chapter_title = ch.title,
|
||||
fraction = (i - 1) / #toc,
|
||||
})
|
||||
return
|
||||
end
|
||||
end
|
||||
cb(nil, nil, { empty = true })
|
||||
end
|
||||
|
||||
local existing = Toc.read(identity.source_id or SOURCE_ID, identity.stable_id)
|
||||
if existing and #existing > 0 then
|
||||
afterToc(existing)
|
||||
else
|
||||
self:refreshTocAsync(identity, afterToc)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
return {
|
||||
cancel = function()
|
||||
remote.cancelled = true
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
function BiliManga:putProgressAsync(identity, pos, cb)
|
||||
local cfg = require("utils.settings").getSource("bilimanga")
|
||||
if not cfg.cookie or cfg.cookie == "" then
|
||||
cb(nil, _("未登录"))
|
||||
return
|
||||
end
|
||||
|
||||
self:loadTocAsync(identity, function(toc, toc_err)
|
||||
if toc_err then cb(nil, toc_err); return end
|
||||
local idx = pos.chapter_idx or identity.chapter_idx or 1
|
||||
if not toc[idx] then cb(nil, _("章节不存在")); return end
|
||||
|
||||
Client.addHistoryAsync(identity.stable_id, toc[idx].uid, function(wire, err)
|
||||
if err then cb(nil, err); return end
|
||||
cb(true)
|
||||
end)
|
||||
end)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
books[#books + 1] = book
|
||||
end
|
||||
end
|
||||
cb({ data = books })
|
||||
end)
|
||||
|
||||
return { cancel = function() 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 })
|
||||
end)
|
||||
return { cancel = function() end }
|
||||
end
|
||||
return SourceBase.defer(cb, { data = {} })
|
||||
end
|
||||
|
||||
return BiliManga
|
||||
Reference in new issue
Block a user