This commit is contained in:
commit
d9495b4122
21 files changed
+1720
No files matched your search
@@ -0,0 +1,5 @@
|
||||
local _ = require("gettext")
|
||||
return {
|
||||
fullname = _("月读书源:维基文库"),
|
||||
description = _("给月读增加「维基文库」书源。需要同时安装月读,与维基媒体基金会无关。"),
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
--[[--
|
||||
KOReader 只认带 main.lua 的插件目录;书源本身不需要任何插件钩子,月读读取旁边的 moon_source.lua。
|
||||
--]]
|
||||
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
||||
|
||||
return WidgetContainer:extend{
|
||||
name = "moonsource_wikisource",
|
||||
is_doc_only = false,
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
--[[--
|
||||
月读第三方书源描述:中文维基文库。契约见月读 examples/moonsource-demo.koplugin/moon_source.lua。
|
||||
--]]
|
||||
|
||||
return {
|
||||
api = 1,
|
||||
provides = {
|
||||
source = true,
|
||||
},
|
||||
meta = function() return require("moonsource_wikisource.source").meta() end,
|
||||
new = function() return require("moonsource_wikisource.source").new() end,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
# 维基文库
|
||||
|
||||
| 能力 | 是否支持 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| 登录 | 不支持 | 公开阅读不需要账号,插件不保存 Cookie |
|
||||
| 书架拉取、新增、删除 | 不支持 | 没有用户书架接口 |
|
||||
| 搜索 | 部分支持 | 主命名空间,每次 20 条。带「/」的命中归到斜线前面的那一页 |
|
||||
| 分类书库 | 部分支持 | 分类是首页上的 Portal。每类按接口顺序取书,书城一次大约 60 本 |
|
||||
| 详情、目录、正文 | 部分支持 | 目录是作品页里指向子页面的链接,按页面上的顺序。没有子页面时整页是一章。已经有分章时略过名为全覽的汇总页 |
|
||||
| 已购内容 | 不支持 | 公开文献,没有购买接口 |
|
||||
| 书评、章评、段评、回复 | 不支持 | 当前 Web 阅读协议未找到可验证的评论接口。讨论页是维基讨论,没有接到阅读器 |
|
||||
| 进度、笔记、统计同步 | 不支持 | 只留在本机 |
|
||||
| 离线与整本缓存 | 部分支持 | 打开过的章节留在本机。整本缓存按章请求;断网后接着读下一章没有实测 |
|
||||
| 账号切换 | 不支持 | 没有账号 |
|
||||
@@ -0,0 +1,15 @@
|
||||
--[[--
|
||||
中文维基文库元信息。
|
||||
|
||||
@module moonsource_wikisource._meta
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_wikisource")
|
||||
|
||||
return {
|
||||
id = "wikisource",
|
||||
name = _("维基文库"),
|
||||
type = "chapter",
|
||||
description = _("维基文库,按章阅读公开文献"),
|
||||
abilities = { _("搜索"), _("分类书库"), _("在线阅读"), _("整本缓存") },
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
--[[--
|
||||
中文维基文库 HTTP。只打 api.php,带可识别的 User-Agent。
|
||||
|
||||
首页的 Portal 列表在这次进程里留一份。书目和正文不缓存成库。
|
||||
|
||||
@module moonsource_wikisource.client
|
||||
--]]
|
||||
|
||||
local Mapper = require("moonsource_wikisource.mapper")
|
||||
local Request = require("http.request")
|
||||
local SourceBase = require("source.base")
|
||||
local _ = require("l10n").get("moonsource_wikisource")
|
||||
|
||||
local BASE = "https://zh.wikisource.org/w/api.php"
|
||||
local UA = "MoonWikisource/0.1 (https://moon.ankio.net)"
|
||||
local HEADERS = {
|
||||
["User-Agent"] = UA,
|
||||
Accept = "application/json",
|
||||
}
|
||||
local PAGE_SIZE = "50"
|
||||
|
||||
---@class WikisourceClient
|
||||
---@field portals { id: string, title: string }[]|nil
|
||||
---@field page { title: string, html: string }|nil
|
||||
local Client = {}
|
||||
Client.__index = Client
|
||||
Client.UA = UA
|
||||
|
||||
---@return WikisourceClient
|
||||
function Client.new()
|
||||
return setmetatable({}, Client)
|
||||
end
|
||||
|
||||
---@param s string
|
||||
---@return string
|
||||
local function encode(s)
|
||||
return (s:gsub("[^%w%-_%.~]", function(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end))
|
||||
end
|
||||
|
||||
---@param params table<string, string>
|
||||
---@return string
|
||||
local function url(params)
|
||||
local parts = { "format=json", "formatversion=2" }
|
||||
for key, value in pairs(params) do
|
||||
parts[#parts + 1] = encode(key) .. "=" .. encode(value)
|
||||
end
|
||||
return BASE .. "?" .. table.concat(parts, "&")
|
||||
end
|
||||
|
||||
---@param params table<string, string>
|
||||
---@param cb fun(data: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
function Client:getJson(params, cb)
|
||||
local pending = true
|
||||
local inner = Request.get(url(params), {
|
||||
headers = HEADERS, timeout = require("http.timeout").net(1),
|
||||
}, function(body, err)
|
||||
pending = false
|
||||
if not body then cb(nil, err or _("网络请求失败")); return end
|
||||
if body:sub(1, 1) ~= "{" then cb(nil, _("响应无效")); return end
|
||||
local ok, data = pcall(require("json").decode, body)
|
||||
if not ok or type(data) ~= "table" or type(data.error) == "table" then
|
||||
cb(nil, _("响应无效"))
|
||||
return
|
||||
end
|
||||
cb(data)
|
||||
end)
|
||||
if pending then return inner end
|
||||
end
|
||||
|
||||
---@param cb fun(portals: { id: string, title: string }[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
function Client:portalsAsync(cb)
|
||||
if self.portals then return SourceBase.defer(cb, self.portals) end
|
||||
return self:getJson({
|
||||
action = "parse", page = "Wikisource:首页", prop = "wikitext",
|
||||
}, function(data, err)
|
||||
if not data then cb(nil, err); return end
|
||||
local text = type(data.parse) == "table" and data.parse.wikitext or nil
|
||||
local portals, map_err = Mapper.portals(text)
|
||||
if not portals then cb(nil, map_err); return end
|
||||
self.portals = portals
|
||||
cb(portals)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param portal string
|
||||
---@param cont string|nil
|
||||
---@param cb fun(data: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
function Client:linksAsync(portal, cont, cb)
|
||||
if type(portal) ~= "string" or portal == "" then
|
||||
return SourceBase.defer(cb, nil, _("响应无效"))
|
||||
end
|
||||
local params = {
|
||||
action = "query", prop = "links", titles = portal,
|
||||
plnamespace = "0", pllimit = PAGE_SIZE,
|
||||
}
|
||||
if type(cont) == "string" and cont ~= "" then params.plcontinue = cont end
|
||||
return self:getJson(params, cb)
|
||||
end
|
||||
|
||||
---@param keyword string
|
||||
---@param cb fun(data: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
function Client:searchAsync(keyword, cb)
|
||||
if type(keyword) ~= "string" or keyword == "" then
|
||||
return SourceBase.defer(cb, nil, _("响应无效"))
|
||||
end
|
||||
return self:getJson({
|
||||
action = "query", list = "search", srsearch = keyword,
|
||||
srnamespace = "0", srlimit = "20",
|
||||
}, cb)
|
||||
end
|
||||
|
||||
---@param title string
|
||||
---@param cb fun(page: { title: string, html: string }|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
function Client:parseAsync(title, cb)
|
||||
if type(title) ~= "string" or title == "" then
|
||||
return SourceBase.defer(cb, nil, _("响应无效"))
|
||||
end
|
||||
local cached = self.page
|
||||
if cached and cached.title == title then return SourceBase.defer(cb, cached) end
|
||||
return self:getJson({
|
||||
action = "parse", page = title, prop = "text",
|
||||
disableeditsection = "1", disabletoc = "1",
|
||||
}, function(data, err)
|
||||
if not data then cb(nil, err); return end
|
||||
local parse = data.parse
|
||||
local html = type(parse) == "table" and parse.text or nil
|
||||
local name = type(parse) == "table" and parse.title or nil
|
||||
if type(html) ~= "string" or type(name) ~= "string" or name == "" then
|
||||
cb(nil, _("响应无效"))
|
||||
return
|
||||
end
|
||||
local page = { title = name, html = html }
|
||||
self.page = page
|
||||
cb(page)
|
||||
end)
|
||||
end
|
||||
|
||||
return Client
|
||||
@@ -0,0 +1,13 @@
|
||||
return {
|
||||
["分类"] = "Category",
|
||||
["分类书库"] = "Categories",
|
||||
["响应无效"] = "Invalid response",
|
||||
["在线阅读"] = "Read online",
|
||||
["整本缓存"] = "Cache the book",
|
||||
["搜索"] = "Search",
|
||||
["章节目录保存失败"] = "Failed to save the chapter list",
|
||||
["网络请求失败"] = "Network request failed",
|
||||
["维基文库"] = "Wikisource",
|
||||
["维基文库,按章阅读公开文献"] = "Chinese Wikisource, read public texts chapter by chapter",
|
||||
["这一章没有正文"] = "This chapter has no text",
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
return {
|
||||
["分类"] = "分類",
|
||||
["分类书库"] = "分類書庫",
|
||||
["响应无效"] = "回應無效",
|
||||
["在线阅读"] = "線上閱讀",
|
||||
["整本缓存"] = "整本快取",
|
||||
["搜索"] = "搜尋",
|
||||
["章节目录保存失败"] = "章節目錄儲存失敗",
|
||||
["网络请求失败"] = "網路請求失敗",
|
||||
["维基文库"] = "維基文庫",
|
||||
["维基文库,按章阅读公开文献"] = "維基文庫,按章閱讀公開文獻",
|
||||
["这一章没有正文"] = "這一章沒有正文",
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
return function()
|
||||
local copy = {}
|
||||
for k, v in pairs(require("moonsource_wikisource._meta")) do copy[k] = v end
|
||||
return copy
|
||||
end
|
||||
@@ -0,0 +1,225 @@
|
||||
--[[--
|
||||
中文维基文库的页面映射:纯函数,无网络。
|
||||
|
||||
分类是首页上的 Portal 链接。书是主命名空间页面,子页面归到第一个「/」前面的那一页。
|
||||
目录按作品页 HTML 里的链接顺序,不用标题排序。
|
||||
|
||||
@module moonsource_wikisource.mapper
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_wikisource")
|
||||
|
||||
local SOURCE_ID = "wikisource"
|
||||
|
||||
local Mapper = {}
|
||||
|
||||
Mapper.CONTENT_MARK = "moon:wikisource:1"
|
||||
Mapper.SOURCE_ID = SOURCE_ID
|
||||
|
||||
---@param s string
|
||||
---@return string
|
||||
local function trim(s)
|
||||
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
|
||||
end
|
||||
|
||||
---@param s string
|
||||
---@return string
|
||||
local function decode(s)
|
||||
s = s:gsub("&", "&")
|
||||
s = s:gsub("%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
|
||||
return (s:gsub("_", " "))
|
||||
end
|
||||
|
||||
---@param html string
|
||||
---@return string
|
||||
local function plain(html)
|
||||
local text = html:gsub("<[^>]+>", "")
|
||||
text = text:gsub(""", '"'):gsub("'", "'"):gsub("&", "&")
|
||||
text = text:gsub("<", "<"):gsub(">", ">")
|
||||
return trim(text)
|
||||
end
|
||||
|
||||
---@param title string
|
||||
---@param category string
|
||||
---@param intro string
|
||||
---@return Book
|
||||
local function book(title, category, intro)
|
||||
return {
|
||||
source_id = SOURCE_ID,
|
||||
stable_id = title,
|
||||
title = title,
|
||||
authors = "",
|
||||
category = category,
|
||||
intro = intro,
|
||||
cover = "",
|
||||
}
|
||||
end
|
||||
|
||||
--- 子页面归成书。主命名空间标题里的冒号是书名的一部分,不能丢掉。
|
||||
---@param title string
|
||||
---@return string
|
||||
local function workTitle(title)
|
||||
local parent = title:match("^(.-)/")
|
||||
if parent and parent ~= "" then return parent end
|
||||
return title
|
||||
end
|
||||
|
||||
---@param wikitext string|nil
|
||||
---@return { id: string, title: string }[]|nil
|
||||
---@return string|nil
|
||||
function Mapper.portals(wikitext)
|
||||
if type(wikitext) ~= "string" or wikitext == "" then return nil, _("响应无效") end
|
||||
local out, seen = {}, {}
|
||||
for body in wikitext:gmatch("%[%[Portal:([^%[%]]-)%]%]") do
|
||||
local name, label = body:match("^(.-)|(.*)$")
|
||||
if not name then name, label = body, body end
|
||||
name = trim(name):match("^([^#]+)") or ""
|
||||
name = trim(name)
|
||||
label = trim(label)
|
||||
if label:find("{", 1, true) or label:find("[", 1, true) then label = name end
|
||||
if name ~= "" and not seen[name] then
|
||||
seen[name] = true
|
||||
out[#out + 1] = { id = "Portal:" .. name, title = label ~= "" and label or name }
|
||||
end
|
||||
end
|
||||
if not out[1] then return nil, _("响应无效") end
|
||||
return out
|
||||
end
|
||||
|
||||
---@param portals { id: string, title: string }[]
|
||||
---@param node string
|
||||
---@return StoreChipGroup[]
|
||||
function Mapper.groups(portals, node)
|
||||
local chips, active = {}, nil
|
||||
for _i, portal in ipairs(portals) do
|
||||
chips[#chips + 1] = { id = portal.id, title = portal.title }
|
||||
if portal.id == node then active = node end
|
||||
end
|
||||
return { { title = _("分类"), chips = chips, active = active } }
|
||||
end
|
||||
|
||||
---@param root table|nil
|
||||
---@param category string
|
||||
---@return Book[]|nil
|
||||
---@return string|nil
|
||||
---@return string|nil
|
||||
function Mapper.portal(root, category)
|
||||
if type(root) ~= "table" then return nil, nil, _("响应无效") end
|
||||
local pages = root.query and root.query.pages or nil
|
||||
local page = type(pages) == "table" and pages[1] or nil
|
||||
if type(page) ~= "table" or page.missing then return nil, nil, _("响应无效") end
|
||||
local links = page.links
|
||||
if links == nil then links = {} end
|
||||
if type(links) ~= "table" then return nil, nil, _("响应无效") end
|
||||
local books, seen = {}, {}
|
||||
for _i, link in ipairs(links) do
|
||||
local title = type(link) == "table" and link.title or nil
|
||||
if type(title) == "string" and title ~= "" and link.ns == 0 then
|
||||
local id = workTitle(title)
|
||||
if not seen[id] then
|
||||
seen[id] = true
|
||||
books[#books + 1] = book(id, category, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
local cont = root["continue"]
|
||||
local token = type(cont) == "table" and cont.plcontinue or nil
|
||||
if type(token) ~= "string" or token == "" then token = nil end
|
||||
return books, token
|
||||
end
|
||||
|
||||
---@param root table|nil
|
||||
---@return Book[]|nil
|
||||
---@return string|nil
|
||||
function Mapper.search(root)
|
||||
local hits = type(root) == "table" and root.query and root.query.search or nil
|
||||
if type(hits) ~= "table" then return nil, _("响应无效") end
|
||||
local books, seen = {}, {}
|
||||
for _i, hit in ipairs(hits) do
|
||||
local title = type(hit) == "table" and hit.title or nil
|
||||
if type(title) == "string" and title ~= "" then
|
||||
local id = workTitle(title)
|
||||
if not seen[id] then
|
||||
seen[id] = true
|
||||
local intro = type(hit.snippet) == "string" and plain(hit.snippet) or ""
|
||||
books[#books + 1] = book(id, "", intro)
|
||||
end
|
||||
end
|
||||
end
|
||||
return books
|
||||
end
|
||||
|
||||
---@param page table|nil
|
||||
---@param stable_id string
|
||||
---@param prev Book|nil
|
||||
---@return Book|nil
|
||||
function Mapper.detail(page, stable_id, prev)
|
||||
if type(page) ~= "table" or type(page.title) ~= "string" or page.title == "" then return nil end
|
||||
if type(stable_id) ~= "string" or stable_id == "" then return nil end
|
||||
prev = type(prev) == "table" and prev or {}
|
||||
return {
|
||||
source_id = SOURCE_ID,
|
||||
stable_id = stable_id,
|
||||
title = page.title,
|
||||
authors = type(prev.authors) == "string" and prev.authors or "",
|
||||
category = type(prev.category) == "string" and prev.category or "",
|
||||
intro = type(prev.intro) == "string" and prev.intro or "",
|
||||
cover = "",
|
||||
}
|
||||
end
|
||||
|
||||
---@param href string
|
||||
---@return string|nil
|
||||
local function wikiTitle(href)
|
||||
local path = href:match("/wiki/([^%?#]+)")
|
||||
if not path or path == "" then return nil end
|
||||
return decode(path)
|
||||
end
|
||||
|
||||
--- 作品页上指向自己子页面的链接,按出现顺序。已有分章时略过「全覽」。
|
||||
---@param page table|nil
|
||||
---@return BookChapter[]|nil
|
||||
---@return string|nil
|
||||
function Mapper.toc(page)
|
||||
if type(page) ~= "table" or type(page.title) ~= "string" or page.title == ""
|
||||
or type(page.html) ~= "string" then
|
||||
return nil, _("响应无效")
|
||||
end
|
||||
local parent = page.title
|
||||
local prefix = parent .. "/"
|
||||
local chapters, seen, overview = {}, {}, nil
|
||||
for href in page.html:gmatch('href="([^"]+)"') do
|
||||
local title = wikiTitle(href)
|
||||
if title and title:sub(1, #prefix) == prefix and not seen[title] then
|
||||
seen[title] = true
|
||||
local rest = title:sub(#prefix + 1)
|
||||
local chapter = {
|
||||
uid = title,
|
||||
title = rest ~= "" and rest or title,
|
||||
}
|
||||
if rest == "全覽" or rest == "全览" then
|
||||
overview = chapter
|
||||
else
|
||||
chapters[#chapters + 1] = chapter
|
||||
end
|
||||
end
|
||||
end
|
||||
if not chapters[1] and overview then chapters[1] = overview end
|
||||
if not chapters[1] then
|
||||
chapters[1] = { uid = parent, title = parent }
|
||||
end
|
||||
for i, chapter in ipairs(chapters) do chapter.idx = i end
|
||||
return chapters
|
||||
end
|
||||
|
||||
---@param page table|nil
|
||||
---@return string|nil
|
||||
---@return string|nil
|
||||
function Mapper.chapterHtml(page)
|
||||
local html = type(page) == "table" and page.html or nil
|
||||
if type(html) ~= "string" or html == "" then return nil, _("响应无效") end
|
||||
if plain(html) == "" then return nil, _("这一章没有正文") end
|
||||
return "<!--" .. Mapper.CONTENT_MARK .. "-->" .. html
|
||||
end
|
||||
|
||||
return Mapper
|
||||
@@ -0,0 +1,219 @@
|
||||
--[[--
|
||||
中文维基文库书源。
|
||||
|
||||
公开文献,不登录。没有书架、进度和评论接口。
|
||||
分类来自首页 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
|
||||
@@ -0,0 +1,4 @@
|
||||
-- 版本号由 tools/package.sh 打包时把占位符换成发布版本;源码运行显示 0.0.0-dev。
|
||||
local VERSION = "@VERSION@"
|
||||
if VERSION:find("^@") then return "0.0.0-dev" end
|
||||
return VERSION
|
||||
Reference in new issue
Block a user