✨ (source): 接入月读书源契约:搜索、发现、详情、目录、正文
This commit is contained in:
1 parent
f73f9c4f39
commit
3a7cb92270
10 files changed
+503
No files matched your search
@@ -0,0 +1,287 @@
|
||||
--[[--
|
||||
开源阅读书源:导入 Legado 书源 JSON,在本地直接执行书源规则(搜索 / 发现 / 详情 / 目录 / 正文)。
|
||||
|
||||
不依赖阅读 App。含 JS / WebView 的规则不支持,该书源对应步骤会报错。没有书架、进度、评论同步。
|
||||
|
||||
@module moonsource_legadorule.source
|
||||
--]]
|
||||
|
||||
local Browse = require("source.browse")
|
||||
local Mapper = require("moonsource_legadorule.mapper")
|
||||
local Sources = require("moonsource_legadorule.sources")
|
||||
local SourceBase = require("source.base")
|
||||
local Toc = require("source.toc")
|
||||
local _ = require("l10n").get("moonsource_legadorule")
|
||||
|
||||
local LegadoRule = {}
|
||||
|
||||
local SEARCH_PARALLEL = 4
|
||||
local FETCH_INTERVAL = 1
|
||||
|
||||
---@return BookSourceMeta
|
||||
function LegadoRule.meta()
|
||||
return require("moonsource_legadorule.loadmeta")()
|
||||
end
|
||||
|
||||
---@class LegadoRuleSource : SourceBase
|
||||
local Source = setmetatable({}, { __index = SourceBase })
|
||||
Source.__index = Source
|
||||
|
||||
function LegadoRule.new()
|
||||
local meta = LegadoRule.meta()
|
||||
return setmetatable({ id = meta.id, name = meta.name, type = meta.type, parsers = {} }, Source)
|
||||
end
|
||||
|
||||
function Source:configured()
|
||||
return Sources.enabledCount() > 0
|
||||
end
|
||||
|
||||
function Source:capabilities()
|
||||
return { scrape = false, edit = false, stats_pull = false, shelf_cache = false, shelf_push = false }
|
||||
end
|
||||
|
||||
--- 书源对应的解析器(同一书源复用,Cookie 罐与页面缓存随之保留)。
|
||||
function Source:parser(entry)
|
||||
local cached = self.parsers[entry.key]
|
||||
if cached and cached.version == Sources.version then return cached.parser end
|
||||
local Parser = require("moonsource_legadorule.parser")
|
||||
local parser = Parser.new(Sources.rule(entry))
|
||||
self.parsers[entry.key] = { version = Sources.version, parser = parser }
|
||||
return parser
|
||||
end
|
||||
|
||||
local function inert()
|
||||
return { cancel = function() end }
|
||||
end
|
||||
|
||||
--- 多个并发任务的合并取消句柄。
|
||||
local function group()
|
||||
local g = { cancelled = false, handles = {} }
|
||||
function g.cancel()
|
||||
g.cancelled = true
|
||||
for _idx, h in ipairs(g.handles) do h.cancel() end
|
||||
end
|
||||
return g
|
||||
end
|
||||
|
||||
function Source:searchAsync(keyword, cb)
|
||||
local entries = Sources.enabled()
|
||||
if #entries == 0 then return SourceBase.defer(cb, nil, _("还没有导入书源")) end
|
||||
local g = group()
|
||||
local results, errors, nextIndex, running, finished = {}, {}, 1, 0, 0
|
||||
local function maybeDone()
|
||||
if g.cancelled or finished < #entries then return end
|
||||
local books = {}
|
||||
for i = 1, #entries do
|
||||
for _idx, b in ipairs(results[i] or {}) do books[#books + 1] = b end
|
||||
end
|
||||
if #books == 0 and errors[1] then
|
||||
cb(nil, errors[1])
|
||||
else
|
||||
cb({ data = books })
|
||||
end
|
||||
end
|
||||
local function launch()
|
||||
while running < SEARCH_PARALLEL and nextIndex <= #entries do
|
||||
local i = nextIndex
|
||||
nextIndex = nextIndex + 1
|
||||
running = running + 1
|
||||
local entry = entries[i]
|
||||
local settled = false
|
||||
local handle = self:parser(entry):search(keyword, 1, function(books, err)
|
||||
if settled or g.cancelled then return end
|
||||
settled = true
|
||||
running, finished = running - 1, finished + 1
|
||||
if books then
|
||||
local rows = {}
|
||||
for _idx, b in ipairs(books) do
|
||||
rows[#rows + 1] = Mapper.book(self.id, entry.key, b, entry.name)
|
||||
end
|
||||
results[i] = rows
|
||||
else
|
||||
errors[#errors + 1] = entry.name .. ": " .. tostring(err)
|
||||
end
|
||||
launch()
|
||||
maybeDone()
|
||||
end)
|
||||
g.handles[#g.handles + 1] = handle
|
||||
end
|
||||
end
|
||||
launch()
|
||||
return g
|
||||
end
|
||||
|
||||
local function defaultNode()
|
||||
for _idx, entry in ipairs(Sources.enabled()) do
|
||||
local items = Sources.explore(Sources.rule(entry))
|
||||
if #items > 0 then return entry.key .. "|" .. items[1].url end
|
||||
end
|
||||
end
|
||||
|
||||
---@return string|nil key, string|nil url
|
||||
local function splitNode(node)
|
||||
if type(node) ~= "string" then return nil end
|
||||
return node:match("^([^|]+)|(.+)$")
|
||||
end
|
||||
|
||||
local function groupsFor(key, url)
|
||||
local sourceChips, categoryChips = {}, {}
|
||||
for _idx, entry in ipairs(Sources.enabled()) do
|
||||
local items = Sources.explore(Sources.rule(entry))
|
||||
if #items > 0 then
|
||||
sourceChips[#sourceChips + 1] = { id = entry.key .. "|" .. items[1].url, title = entry.name }
|
||||
if entry.key == key then
|
||||
for _i, item in ipairs(items) do
|
||||
categoryChips[#categoryChips + 1] = { id = key .. "|" .. item.url, title = item.title }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local groups = { { title = _("书源"), chips = sourceChips, active = nil } }
|
||||
for _idx, chip in ipairs(sourceChips) do
|
||||
if chip.id:match("^([^|]+)|") == key then groups[1].active = chip.id end
|
||||
end
|
||||
if #categoryChips > 1 then
|
||||
groups[#groups + 1] = { title = _("分类"), chips = categoryChips, active = key .. "|" .. url }
|
||||
end
|
||||
return groups
|
||||
end
|
||||
|
||||
function Source:browseAsync(opts, cb)
|
||||
opts = opts or {}
|
||||
local node = opts.node
|
||||
local key, url = splitNode(node)
|
||||
if not key then
|
||||
node = defaultNode()
|
||||
key, url = splitNode(node)
|
||||
end
|
||||
local entry = key and Sources.find(key)
|
||||
if not entry or not entry.enabled then return SourceBase.defer(cb, nil, _("没有可浏览的书源")) end
|
||||
local parser = self:parser(entry)
|
||||
local paged = url:find("{{", 1, true) and url:find("page", 1, true) or url:find("<[^>]*,[^>]*>")
|
||||
local lastFirst
|
||||
local g = group()
|
||||
local handle = Browse.pages(function(page, page_cb)
|
||||
if not paged and page > 1 then page_cb({}, nil, false) return inert() end
|
||||
local h = parser:explore(url, page, function(books, err)
|
||||
if not books then page_cb(nil, err) return end
|
||||
local rows = {}
|
||||
for _idx, b in ipairs(books) do rows[#rows + 1] = Mapper.book(self.id, key, b, entry.name) end
|
||||
local first = rows[1] and rows[1].stable_id
|
||||
local more = paged and #rows > 0 and first ~= lastFirst
|
||||
lastFirst = first
|
||||
page_cb(rows, nil, more)
|
||||
end)
|
||||
return h
|
||||
end, opts.limit or 60, groupsFor(key, url), function(result, err)
|
||||
if result then result.groups = groupsFor(key, url) end
|
||||
cb(result, err)
|
||||
end)
|
||||
g.handles[1] = handle
|
||||
return g
|
||||
end
|
||||
|
||||
--- identity.stable_id → 书源条目、解析器、书地址(换过源的书读新书源)。
|
||||
function Source:resolve(identity)
|
||||
local key, bookUrl = Sources.alias(identity.stable_id)
|
||||
if not key then key, bookUrl = Mapper.splitStableId(identity.stable_id) end
|
||||
local entry = key and Sources.find(key)
|
||||
if not entry then return nil, _("书源已被删除") end
|
||||
return entry, self:parser(entry), bookUrl
|
||||
end
|
||||
|
||||
function Source:getDetailAsync(identity, cb)
|
||||
local entry, parser, bookUrl = self:resolve(identity)
|
||||
if not entry then return SourceBase.defer(cb, nil, parser) end
|
||||
return parser:detail(bookUrl, nil, function(book, err)
|
||||
if not book then cb(nil, err) return end
|
||||
local row = Mapper.book(self.id, entry.key, book, entry.name)
|
||||
cb(require("book.store").rememberDetail({
|
||||
source_id = self.id,
|
||||
stable_id = identity.stable_id,
|
||||
title = row.title,
|
||||
authors = row.authors,
|
||||
intro = row.intro,
|
||||
cover = row.cover,
|
||||
}))
|
||||
end)
|
||||
end
|
||||
|
||||
function Source:loadTocAsync(identity, cb, force)
|
||||
if force then return self:refreshTocAsync(identity, cb) end
|
||||
local cancelled, active = false, nil
|
||||
active = Toc.readAsync(self.id, identity.stable_id, function(cached, err)
|
||||
if cancelled then return end
|
||||
if cached and #cached > 0 then cb(cached) return end
|
||||
if err then cb(nil, err) return end
|
||||
active = self:refreshTocAsync(identity, cb)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
function Source:refreshTocAsync(identity, cb)
|
||||
local entry, parser, bookUrl = self:resolve(identity)
|
||||
if not entry then return SourceBase.defer(cb, nil, parser) end
|
||||
local cancelled, active, pending = false, nil, true
|
||||
local request = parser:detail(bookUrl, nil, function(book, err)
|
||||
if cancelled then return end
|
||||
if not book then pending = false cb(nil, err) return end
|
||||
local inner = parser:chapters(book, function(chapters, cerr)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not chapters then cb(nil, cerr) return end
|
||||
active = Toc.parseAsync(self.id, identity.stable_id, function()
|
||||
local toc = Mapper.toc(chapters)
|
||||
if #toc == 0 then return nil, _("章节列表为空") end
|
||||
return toc
|
||||
end, cb)
|
||||
end)
|
||||
if pending then active = inner end
|
||||
end)
|
||||
if pending and not active then active = request end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if active then active.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
---@return ChapterFetchContent
|
||||
local function contentFetcher(self)
|
||||
return function(ref, ch, done)
|
||||
local entry, parser, bookUrl = self:resolve(ref)
|
||||
if not entry then return SourceBase.defer(done, nil, parser) end
|
||||
local url, nextUrl = Mapper.splitOrigin(ch.origin)
|
||||
local chapter = { title = ch.title, url = url, baseUrl = url, isVolume = false }
|
||||
local book = { bookUrl = bookUrl, tocUrl = bookUrl, name = "" }
|
||||
return parser:content(chapter, book, nextUrl, function(res, err)
|
||||
if not res then done(nil, err) return end
|
||||
done({ title = res.title ~= "" and res.title or ch.title, text = res.text })
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function Source:openBookAsync(identity, opts, cb)
|
||||
local Chapter = require("source.chapter")
|
||||
local open = opts and opts.chapter_idx and Chapter.openAsync or Chapter.openWithUi
|
||||
return open(self, identity, identity.book, opts, {
|
||||
loadToc = function(ref, done) return self:loadTocAsync(ref, done) end,
|
||||
fetchContent = contentFetcher(self),
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
||||
return require("source.chapter").prefetchAsync(identity, identity.book, toc, from_idx, count, {
|
||||
fetchContent = contentFetcher(self),
|
||||
interval_seconds = FETCH_INTERVAL,
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
|
||||
return require("source.chapter").cacheAllAsync(self, identity, contentFetcher(self), on_progress, cb,
|
||||
FETCH_INTERVAL, range)
|
||||
end
|
||||
|
||||
return LegadoRule
|
||||
Reference in new issue
Block a user