Files
2026-11-28 13:44:23 +08:00

216 lines
7.4 KiB
Lua

--[[--
识典古籍书源。
阅读公开整理本,不登录。书架、进度、评论走要签名的接口,这里不接。
分类和搜索只有官网首屏。
@module moonsource_shidian.source
--]]
local Browse = require("source.browse")
local Client = require("moonsource_shidian.client")
local Mapper = require("moonsource_shidian.mapper")
local SourceBase = require("source.base")
local Toc = require("source.toc")
local _ = require("l10n").get("moonsource_shidian")
-- 书库页「经部」。其余分类从这一页的树来,不在本地造一份分类表。
local DEFAULT_CATE = "10000"
local FETCH_INTERVAL = 1
local Shidian = {}
---@return BookSourceMeta
function Shidian.meta()
return require("moonsource_shidian.loadmeta")()
end
---@class ShidianSource : SourceBase
---@field client ShidianClient
local Source = setmetatable({}, { __index = SourceBase })
Source.__index = Source
---@return ShidianSource
function Shidian.new()
local meta = Shidian.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
function Source:syncBooksAsync(_opts, cb)
return SourceBase.skipped(cb, "shidian has no shelf")
end
---@param identity BookIdentity
---@return { id: string, title: string|nil, seed: string|nil, cate: string|nil }
local function hint(identity)
local book = identity.book or {}
local meta = book.meta or {}
local own = type(meta.source) == "table" and meta.source.shidian or nil
own = type(own) == "table" and own or {}
return { id = identity.stable_id, title = book.title, seed = own.seed, cate = own.cate }
end
---@param keyword string
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
---@return { cancel: fun() }|nil
function Source:searchAsync(keyword, cb)
return self.client:searchAsync(keyword, function(data, err)
if not data then cb(nil, err); return end
local books, map_err = Mapper.search(data)
if not books then cb(nil, map_err); return end
cb({ data = books })
end)
end
---@param opts { node?: string, limit?: integer }|nil
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
---@return { cancel: fun() }
function Source:browseAsync(opts, cb)
opts = opts or {}
local node = opts.node
if type(node) ~= "string" or node == "" or node == "all" then node = DEFAULT_CATE end
local groups
return Browse.pages(function(_page, done)
return self.client:libraryAsync(node, function(data, err)
if not data then done(nil, err); return end
local books, filters, map_err = Mapper.library(data, node)
if not books then done(nil, map_err); return end
groups = filters
done(books, nil, false)
end)
end, opts.limit or 60, {}, function(result, err)
if result then result.groups = groups end
cb(result, err)
end)
end
---@param identity BookIdentity
---@param cb fun(book: Book|nil, err: string|nil)
---@return { cancel: fun() }
function Source:getDetailAsync(identity, cb)
return self.client:resolveAsync(hint(identity), function(page, err)
if not page then cb(nil, err); return end
local book = Mapper.book(page)
if not book then cb(nil, _("响应无效")); return end
local existing = require("book.books_db").get(self.id, identity.stable_id)
if existing then book.deleted = existing.deleted end
cb(require("book.store").rememberDetail(book))
end)
end
---@param identity BookIdentity
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
---@return { cancel: fun() }
function Source:refreshTocAsync(identity, cb)
local handle = {}
local pending = true
local inner = self.client:resolveAsync(hint(identity), function(page, err)
pending = false
if handle.cancelled then return end
if not page then cb(nil, err); return end
local toc, toc_err = Mapper.toc(page)
if not toc then cb(nil, toc_err); return end
if not Toc.put(self.id, identity.stable_id, toc) then
cb(nil, _("章节目录保存失败"))
return
end
cb(toc)
end)
if pending then handle.inner = inner end
function handle.cancel()
handle.cancelled = true
if handle.inner then handle.inner.cancel() end
end
return handle
end
---@param identity BookIdentity
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
---@param force boolean|nil
---@return { cancel: fun() }
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[1] 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
---@return ChapterFetchContent
local function contentFetcher(self)
return function(ref, chapter, done)
return self.client:contentAsync(ref.stable_id, chapter.uid, function(page, err)
if not page then done(nil, err or _("这一章没有正文")); return end
local html, html_err = Mapper.chapterHtml(page)
if not html then done(nil, html_err); return end
done({
title = chapter.title,
book_title = ref.book and ref.book.title or nil,
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
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
---@param identity BookIdentity
---@return BookCoverRequest|nil
function Source:coverRequest(identity)
local url = identity.book and identity.book.cover
if type(url) ~= "string" or url == "" then return nil end
return { url = url, headers = { ["User-Agent"] = Client.UA, Referer = "https://www.shidianguji.com/" } }
end
return Shidian