--[[-- 中文维基文库书源。 公开文献,不登录。没有书架、进度和评论接口。 分类来自首页 Portal。一部作品的章节是它页面上的子页面链接。 @module moonsource_wikisource.source --]] local Browse = require("source.browse") local Client = require("moonsource_wikisource.client") local Mapper = require("moonsource_wikisource.mapper") local SourceBase = require("source.base") local Toc = require("source.toc") local _ = require("l10n").get("moonsource_wikisource") local FETCH_INTERVAL = 1 local Wikisource = {} ---@return BookSourceMeta function Wikisource.meta() return require("moonsource_wikisource.loadmeta")() end ---@class WikisourceSource : SourceBase ---@field client WikisourceClient local Source = setmetatable({}, { __index = SourceBase }) Source.__index = Source ---@return WikisourceSource function Wikisource.new() local meta = Wikisource.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, "wikisource has no shelf") end ---@param keyword string ---@param cb fun(res: { data: Book[] }|nil, err: string|nil) ---@return { cancel: fun() } function Source:searchAsync(keyword, cb) local handle = 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) if not handle then handle = { cancel = function() end } end ---@cast handle { cancel: fun() } return handle 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 limit = opts.limit or 60 local handle, live = {}, nil function handle.cancel() handle.cancelled = true if live and live.cancel then live.cancel() end if handle.inner and handle.inner.cancel then handle.inner.cancel() end end local request = self.client:portalsAsync(function(portals, err) if handle.cancelled then return end if not portals then cb(nil, err); return end local node = opts.node if type(node) ~= "string" or node == "" or node == "all" then node = portals[1].id end local label = node for _i, portal in ipairs(portals) do if portal.id == node then label = portal.title end end handle.inner = Browse.pages(function(_page, done) local acc = {} local function step(cont, hops) if handle.cancelled then return end live = self.client:linksAsync(node, cont, function(data, link_err) if handle.cancelled then return end if not data then done(nil, link_err); return end local books, next_token, map_err = Mapper.portal(data, label) if not books then done(nil, map_err); return end for _i, item in ipairs(books) do acc[#acc + 1] = item end if next_token and #acc < limit and hops < 8 then step(next_token, hops + 1) return end done(acc, nil, false) end) end step(nil, 1) end, limit, Mapper.groups(portals, node), cb) end) if request then handle.inner = request end return handle end ---@param identity BookIdentity ---@param cb fun(book: Book|nil, err: string|nil) ---@return { cancel: fun() } function Source:getDetailAsync(identity, cb) local prev = identity.book return self.client:parseAsync(identity.stable_id, function(page, err) if not page then cb(nil, err); return end local book = Mapper.detail(page, identity.stable_id, prev) 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 = {} function handle.cancel() handle.cancelled = true if handle.inner and handle.inner.cancel then handle.inner.cancel() end end handle.inner = self.client:parseAsync(identity.stable_id, function(page, err) 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) 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 and active.cancel then active.cancel() end end } end ---@return ChapterFetchContent local function contentFetcher(self) return function(_ref, chapter, done) return self.client:parseAsync(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 return Wikisource