✨ (reader): 整本阅读并按 CFI 回写进度

EPUB 和 TXT 下载后整本打开。阅读位置换算成官网 CFI 才上传。
This commit is contained in:
Garfield committed 2026-11-27 00:51:41 +08:00
1 parent 285d4cc487
commit e108b75c57
2 files changed
+798

No files matched your search

@@ -0,0 +1,311 @@
--[[--
LinguoLand 书源核心。
LinguoLand 的书是整本 EPUB 或 TXT,地址在 meta.source.storage_ref。
书架列表不带这份元数据,开书时从书库行补上,下载到工作目录再交给阅读器。
登录:邮箱密码、邮箱验证码、手机短信,或浏览器 localStorage 里的 JWT。
书架:书城书可加入、可删除,经公共书架同步。自己导入的 EPUB 没有 catalog,不进这套。
进度:拉取只取全书比例。上传只在本地 xpointer 能换算成官网 EPUB CFI 时发生。
@module moonsource_linguoland.source
--]]
local Browse = require("source.browse")
local Cfi = require("moonsource_linguoland.cfi")
local Client = require("moonsource_linguoland.client")
local Mapper = require("moonsource_linguoland.mapper")
local SourceBase = require("source.base")
local Toc = require("source.toc")
local _ = require("l10n").get("moonsource_linguoland")
local Linguoland = {}
---@return BookSourceMeta
function Linguoland.meta()
return require("moonsource_linguoland.loadmeta")()
end
---@class LinguolandSource : SourceBase
local Source = setmetatable({ shelf_add = true }, { __index = SourceBase })
Source.__index = Source
---@return LinguolandSource
function Linguoland.new()
local meta = Linguoland.meta()
return setmetatable({
id = meta.id,
name = meta.name,
type = meta.type,
}, Source)
end
--- 未登录仍可搜索和读书城。书架和进度需要登录。
---@return boolean
function Source:configured()
return Client.isLoggedIn()
end
function Source:syncBooksAsync(opts, cb)
if not self:configured() then return SourceBase.skipped(cb, "linguoland not logged in") end
return require("source.shelf").syncAsync(self.id, Client, nil, opts, cb)
end
---@param keyword string
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
function Source:searchAsync(keyword, cb)
local cancelled, job = false, nil
job = Client.searchAsync(keyword, function(books, err)
if cancelled then return end
if not books then cb(nil, err or _("请求失败")); return end
cb({ data = books })
end)
return { cancel = function()
cancelled = true
if job and job.cancel then job:cancel() end
end }
end
--- 书城浏览:支持按合集分类筛选
---@param opts { node?: string, limit?: integer }|nil
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
function Source:browseAsync(opts, cb)
local collectionId = opts and opts.node or ""
local cancelled, job, pending = false, nil, true
local function fetchBooks(done)
if collectionId == "" then
job = Client.getCatalogAsync(function(books, err)
if cancelled then return end
if not books then done(nil, err); return end
done(books)
end)
else
job = Client.getCollectionBooksAsync(tonumber(collectionId), function(books, err)
if cancelled then return end
if not books then done(nil, err); return end
done(books)
end)
end
end
local function handleResult(books, err)
pending = false
if cancelled then return end
if err then cb(nil, err); return end
if not books then cb(nil, _("请求失败")); return end
cb({ data = books })
end
if collectionId == "" then
local initial = Client.getCollectionsAsync(function(collections, err)
pending = false
if cancelled then return end
if not collections then cb(nil, err); return end
local groups = { Browse.group(Mapper.collectionsToGroups(collections), collectionId or "") }
job = Browse.pages(function(page, done)
fetchBooks(function(books, fetchErr)
done(books, fetchErr, false)
end)
end, opts and opts.limit or 60, groups, handleResult)
end)
if pending then job = initial end
else
job = Browse.pages(function(page, done)
fetchBooks(function(books, fetchErr)
done(books, fetchErr, false)
end)
end, opts and opts.limit or 60, {}, handleResult)
end
return { cancel = function()
cancelled = true
if job and job.cancel then job:cancel() end
end }
end
function Source:getDetailAsync(identity, cb)
local row = identity.book or require("book.books_db").get(self.id, identity.stable_id)
if row then cb(row); return end
cb(nil, _("暂无书籍详情"))
end
--- 书架列表行没有 meta。有地址就用手里的,没有再读库。
---@param identity BookIdentity
---@return string url
---@return table|nil book
local function fileOf(self, identity)
local function pick(row)
if type(row and row._epub_url) == "string" and row._epub_url ~= "" then return row._epub_url end
local source = row and row.meta and row.meta.source
if type(source) == "table" and type(source.storage_ref) == "string" and source.storage_ref ~= "" then
return source.storage_ref
end
end
local book = identity.book
local url = pick(book)
if url then return url, book end
local row = require("book.books_db").get(self.id, identity.stable_id)
url = pick(row)
if url and book and row and row.meta then book.meta = row.meta end
return url or "", row or book
end
---@param url string
---@param format string|nil
---@return string
local function fileExt(url, format)
local path = url:match("^[^?#]+") or url
local ext = path:match("%.([%a%d]+)$")
if ext then
ext = ext:lower()
if ext == "epub" or ext == "txt" or ext == "pdf" or ext == "fb2" or ext == "html" then return ext end
end
if type(format) == "string" and format ~= "" then return format:lower() end
return "epub"
end
---@param identity BookIdentity
---@return integer|nil
local function shelfEntryId(identity)
local book = identity.book
local source = book and book.meta and book.meta.source
return source and tonumber(source.shelf_entry_id)
end
function Source:openBookAsync(identity, _opts, cb)
local url, book = fileOf(self, identity)
local lfs = require("libs/libkoreader-lfs")
local saved = book and book.path
if type(saved) == "string" and saved ~= "" and lfs.attributes(saved, "mode") == "file" then
return SourceBase.defer(cb, saved)
end
if not url:find("^https?://") then
return SourceBase.defer(cb, nil, _("书籍文件地址无效"))
end
if not require("ui/network/manager"):isConnected() then
return SourceBase.defer(cb, nil, _("网络不可用,请先连接 Wi-Fi"))
end
local Paths = require("utils.paths")
local format = book and book.meta and book.meta.source and book.meta.source.format
Paths.ensureBookWork(identity.stable_id, self.id)
local dest = Paths.bookWorkDir(identity.stable_id, self.id) .. "/book." .. fileExt(url, format)
if lfs.attributes(dest, "mode") == "file" then
local ok, err = require("book.store").touch(dest, identity)
return SourceBase.defer(cb, ok and dest or nil, err)
end
local cancelled, dialog, job = false, nil, nil
require("ui/uimanager"):nextTick(function()
if cancelled then return end
dialog = require("ui.themes.widgets.progress_dialog"):new{
title = book and book.title or identity.stable_id,
dismissable = false,
}
dialog:show()
dialog:set(_("正在下载…"))
job = require("http.request").download({
url = url,
timeout = require("http.timeout").net(6),
on_progress = function(bytes, total)
if dialog and total and total > 0 then dialog:bytes(bytes, total) end
end,
}, dest, function(ok, err)
if dialog then dialog:close(); dialog = nil end
if cancelled then return end
if not ok then cb(nil, err or _("下载失败")); return end
local registered, rerr = require("book.store").touch(dest, identity)
cb(registered and dest or nil, rerr)
end)
end)
return { cancel = function()
cancelled = true
if job and job.cancel then job:cancel() end
if dialog then dialog:close() end
end }
end
--- 官网进度是全书比例。不带章内比、不把 EPUB CFI 放进 locator:
--- CFI 和 KOReader 的 xpointer 对不上,放进去每次开书都是假冲突。
---@param identity BookIdentity
---@param progress { percent: number }
---@return ProgressPosition
local function positionFromPercent(source_id, identity, progress)
local fraction = progress.percent or 0
local pos = { fraction = fraction }
local toc = Toc.read(source_id, identity.stable_id)
if toc and #toc > 0 then
local idx = math.floor(fraction * #toc) + 1
if idx < 1 then idx = 1 end
if idx > #toc then idx = #toc end
pos.chapter_idx = idx
pos.chapter_title = toc[idx] and toc[idx].title or nil
end
return pos
end
function Source:getProgressAsync(identity, cb)
if not self:configured() then
return SourceBase.defer(cb, nil, nil, { empty = true })
end
local entry_id = shelfEntryId(identity)
if not entry_id then
return SourceBase.defer(cb, nil, nil, { empty = true })
end
local cancelled, job = false, nil
job = Client.getProgressAsync(entry_id, function(progress, err)
if cancelled then return end
if err then cb(nil, err); return end
if not progress then cb(nil, nil, { empty = true }); return end
cb(positionFromPercent(self.id, identity, progress))
end)
return { cancel = function()
cancelled = true
if job and job.cancel then job:cancel() end
end }
end
--- 书架条目和本地文件。列表行没有 meta 时回书库补。
---@param identity BookIdentity
---@return integer|nil, string|nil
local function progressTarget(self, identity)
local book = identity.book
local source = book and book.meta and book.meta.source
local entry = source and tonumber(source.shelf_entry_id)
local path = book and book.path
if entry and type(path) == "string" and path ~= "" then return entry, path end
local row = require("book.books_db").get(self.id, identity.stable_id)
if row then
if not entry then
local stored = row.meta and row.meta.source
entry = stored and tonumber(stored.shelf_entry_id)
end
if type(path) ~= "string" or path == "" then path = row.path end
end
if type(path) ~= "string" or path == "" then path = nil end
return entry, path
end
function Source:putProgressAsync(identity, pos, cb)
local entry, path = progressTarget(self, identity)
if not entry then
return SourceBase.defer(cb, nil, _("缺少书架条目 ID"), { permanent = true })
end
local locator = pos and pos.locator
if type(locator) ~= "string" or not locator:find("DocFragment", 1, true) then
return SourceBase.defer(cb, nil, _("没有可换算的阅读位置"), { permanent = true })
end
local lfs = require("libs/libkoreader-lfs")
if not path or lfs.attributes(path, "mode") ~= "file" then
return SourceBase.defer(cb, nil, _("书籍文件还没下载"))
end
if not path:lower():match("%.epub$") then
return SourceBase.defer(cb, nil, _("这个格式没有 CFI"), { permanent = true })
end
local cfi = Cfi.fromFile(path, locator)
if type(cfi) ~= "string" then
return SourceBase.defer(cb, nil, _("位置换算失败"), { permanent = true })
end
return Client.putProgressAsync(entry, cfi, pos.fraction, cb)
end
return Linguoland