Files

175 lines
6.2 KiB
Lua

--[[--
努努书坊书源:频道 / 分类浏览 + 按章在线阅读。
站点没有站内搜索(页面里只有 Google 自定义检索),也没有书架、进度、评论接口,
所以只接:频道与分类浏览、详情、目录、正文与整本缓存。
页面编码 GBK / GB2312 由 client 统一转 UTF-8。
@module moonsource_kanunu.source
--]]
local Browse = require("source.browse")
local Client = require("moonsource_kanunu.client")
local Mapper = require("moonsource_kanunu.mapper")
local SourceBase = require("source.base")
local Toc = require("source.toc")
local _ = require("l10n").get("moonsource_kanunu")
local Kanunu = {}
-- 站点响应慢且在 Cloudflare 后面,逐章请求之间留间隔。
local FETCH_INTERVAL = 2
---@return BookSourceMeta
function Kanunu.meta()
return require("moonsource_kanunu.loadmeta")()
end
---@class KanunuSource : SourceBase
---@field client KanunuClient
local Source = setmetatable({}, { __index = SourceBase })
Source.__index = Source
---@return KanunuSource
function Kanunu.new()
local meta = Kanunu.meta()
return setmetatable({ id = meta.id, name = meta.name, type = meta.type, client = Client.new() }, Source)
end
function Source:configured()
return true
end
function Source:capabilities()
return { scrape = false, edit = false, stats_pull = false, shelf_cache = false, shelf_push = false }
end
--- 分页路径形式不统一(`2.html` / `29-2.html`),只按上一页的「下一页」链接走。
function Source:browseAsync(opts, cb)
opts = opts or {}
local node = Mapper.node(opts.node) or Mapper.DEFAULT_NODE
local pages, groups = {}, {}
return Browse.pages(function(page, page_cb)
local path = pages[page] or node
return self.client:get(path, function(html, err)
if not html then page_cb(nil, err) return end
local books = Mapper.list(html, path)
if not books then page_cb(nil, _("响应无效")) return end
groups = Mapper.groups(html, node) or groups
pages[page + 1] = Mapper.nextPath(html, path)
page_cb(books, nil, pages[page + 1] ~= nil)
end)
end, opts.limit or 60, {}, function(result, err)
if result then result.groups = groups end
cb(result, err)
end)
end
function Source:getDetailAsync(identity, cb)
return self.client:get("/" .. identity.stable_id, function(html, err)
if not html then cb(nil, err) return end
local row = Mapper.book(html, "/" .. identity.stable_id)
if not row then
cb(nil, _("无法解析书籍信息"))
return
end
local book = {
source_id = self.id,
stable_id = identity.stable_id,
title = row.title,
authors = row.authors,
intro = row.intro,
cover = row.cover,
}
cb(require("book.store").rememberDetail(book))
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 cancelled, active, pending = false, nil, true
local request = self.client:get("/" .. identity.stable_id, function(html, err)
pending = false
if cancelled then return end
if not html then cb(nil, err) return end
active = Toc.parseAsync(self.id, identity.stable_id, function()
local toc = Mapper.toc(html, "/" .. identity.stable_id)
if #toc == 0 then return nil, _("章节列表为空") end
return toc
end, cb)
end)
if pending 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)
return self.client:get(ch.origin, function(page, err)
if not page then done(nil, err) return end
local html = Mapper.contentHtml(page)
if not html then done(nil, _("这一章没有正文")) return end
done({ title = ch.title, html = html, mark = Mapper.CONTENT_MARK })
end)
end
end
--- 离线保留旧缓存;在线重取未标记的旧版正文,失败不覆盖它。
---@param path string
---@return boolean
local function reuse(path)
if not require("ui/network/manager"):isConnected() then return true end
local file = assert(io.open(path, "rb"))
local head, err = file:read(64)
file:close()
assert(head, err)
return head:find(Mapper.CONTENT_MARK, 1, true) ~= nil
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),
reuse = reuse,
}, 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,
reuse = reuse,
}, cb)
end
--- 缓存整本章节正文;已落盘章节由公共实现自动跳过。
---@param identity BookIdentity
---@param on_progress fun(done: integer, total: integer)|nil
---@param cb fun(ok: boolean, cached: integer, err: string|nil, total: integer, failed: integer)
---@return { cancel: fun() }
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
return require("source.chapter").cacheAllAsync(self, identity, contentFetcher(self), on_progress, cb,
FETCH_INTERVAL, range, reuse)
end
return Kanunu