This commit is contained in:
commit
28215d4989
25 files changed
+2822
No files matched your search
@@ -0,0 +1,19 @@
|
||||
# iRead(爱阅读)
|
||||
|
||||
| 能力 | 是否支持 | 备注 |
|
||||
| --- | --- | --- |
|
||||
| 个人 Token 登录 | 支持 | 保存前校验;与 Cookie 互斥 |
|
||||
| 浏览器登录与 Cookie 导入 | 支持 | 手动粘贴或扩展回传;完整扩展握手未实测 |
|
||||
| 设备密码与扫码登录 | 不支持 | 在 iRead 官网完成账号登录 |
|
||||
| 搜索与发现 | 支持 | 游客公共书源、账号配置书源及动态发现入口 |
|
||||
| 发现分页 | 部分支持 | 仅支持已核实的页面占位符;无占位符只取一批,不执行规则 JavaScript |
|
||||
| 书架拉取 | 支持 | 读取个人书架 |
|
||||
| 书架新增与移出 | 已实现,未实测 | 收藏开关写前查架、写后回读,失败保留待同步状态 |
|
||||
| 目录与文字正文 | 支持 | 取决于服务端规则可用性;上游失效或空正文明确失败 |
|
||||
| 漫画与图片正文 | 不支持 | 未实现图片落地,不把空正文作为成功缓存 |
|
||||
| 付费内容 | 未验证 | 取决于上游规则、登录和购买权限,不保证所有付费书源可读 |
|
||||
| 章节与全本缓存 | 支持 | 已缓存章节可离线阅读 |
|
||||
| 阅读进度同步 | 部分支持 | 仅章节位置;同章保留远端原位置,不伪造跨排版精确坐标 |
|
||||
| 笔记与统计同步 | 不支持 | 仅本地保存 |
|
||||
| 书评、章评、段评与回复 | 不支持 | 当前 Web 契约未找到可验证接口,不代表上游网站没有评论 |
|
||||
| 账号切换与凭据续期 | 部分支持 | 新凭据须先退出旧登录并清理本地数据,未同步记录会丢失 |
|
||||
@@ -0,0 +1,15 @@
|
||||
--[[--
|
||||
书源元信息:id / 名称 / 类型,加上启用页展示用的说明与能力清单。`init.lua` 的 `meta()` 直接返回本表。
|
||||
|
||||
@module moonsource_iread._meta
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_iread")
|
||||
|
||||
return {
|
||||
id = "iread",
|
||||
name = _("iRead"),
|
||||
type = "chapter",
|
||||
description = _("iRead 书架与在线阅读"),
|
||||
abilities = { _("搜索与发现"), _("书架同步"), _("进度同步") },
|
||||
}
|
||||
@@ -0,0 +1,385 @@
|
||||
--[[--
|
||||
iRead(iread.ankio.net)HTTP 客户端(仅异步)。
|
||||
|
||||
鉴权是个人 Token(`Authorization: Bearer <64 位十六进制>`,在网站「个人 Token」页创建);接口一律 POST 表单,
|
||||
回包是 `{ code, msg, data }`,`code == 200` 才算成功。注意服务端对无效 Token 不报 401,而是当游客处理,
|
||||
所以书架为空时要另调 /user/home 确认登录态,免得把「Token 失效」当成「书架被清空」。
|
||||
|
||||
书架行(BookDetailModel)就是服务端的书记录:章节、正文、进度接口都要把整行原样带回去,
|
||||
所以这里的请求都以书架行为参数。
|
||||
|
||||
@module koplugin.book.moonsource_iread.client
|
||||
--]]
|
||||
|
||||
local JSON = require("json")
|
||||
local Request = require("http.request")
|
||||
local Text = require("utils.text")
|
||||
local _ = require("l10n").get("moonsource_iread")
|
||||
|
||||
local Client = {}
|
||||
|
||||
local SOURCE_ID = "iread"
|
||||
Client.BASE = "https://iread.ankio.net"
|
||||
|
||||
---@class IReadCredential
|
||||
---@field token string|nil 个人 Token(Bearer)
|
||||
---@field cookie string|nil 网页登录 Cookie 请求头
|
||||
|
||||
--- 已保存的登录凭据,Token 优先;没登录返回 nil。
|
||||
---@return IReadCredential|nil
|
||||
function Client.credential()
|
||||
local cfg = require("utils.settings").getSource(SOURCE_ID)
|
||||
if type(cfg.token) == "string" and cfg.token ~= "" then return { token = cfg.token } end
|
||||
if type(cfg.cookie) == "string" and cfg.cookie ~= "" then return { cookie = cfg.cookie } end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- 发请求用的凭据是不是当前已保存的那份(校验新凭据时的回包不能动已保存的登录态)。
|
||||
---@param cred IReadCredential
|
||||
---@return boolean
|
||||
local function isCurrent(cred)
|
||||
local saved = Client.credential()
|
||||
if not saved then return cred.token == nil and cred.cookie == nil end
|
||||
return saved.token == cred.token and saved.cookie == cred.cookie
|
||||
end
|
||||
|
||||
--- 嵌套表按 PHP 的 `a[b]` 写法摊平成表单键(数组用 0 基下标,PHP 侧才是列表);布尔 / 数字转字符串,nil 丢弃。
|
||||
---@param out table<string, string>
|
||||
---@param key string
|
||||
---@param value any
|
||||
local function flatten(out, key, value)
|
||||
if type(value) == "table" then
|
||||
local is_list = #value > 0
|
||||
for k, v in pairs(value) do
|
||||
flatten(out, key .. "[" .. (is_list and (k - 1) or tostring(k)) .. "]", v)
|
||||
end
|
||||
elseif value ~= nil and value ~= JSON.util.null then
|
||||
out[key] = tostring(value)
|
||||
end
|
||||
end
|
||||
|
||||
---@param form table<string, any>
|
||||
---@return string
|
||||
local function encode(form)
|
||||
local flat = {}
|
||||
for key, value in pairs(form) do flatten(flat, key, value) end
|
||||
return Text.formEncode(flat)
|
||||
end
|
||||
|
||||
--- 解回包:返回 data。401 / 403 视为登录失效。
|
||||
---@param raw string|nil
|
||||
---@param err any
|
||||
---@param cred IReadCredential 发请求时用的凭据
|
||||
---@return any|nil data, string|nil err, table|nil wire 整包(分页接口的 total / hasMore 在 data 外面)
|
||||
function Client.decode(raw, err, cred)
|
||||
if err then return nil, tostring(err) end
|
||||
if not raw then return nil, _("请求失败") end
|
||||
local ok, wire = pcall(JSON.decode, raw, JSON.decode.simple)
|
||||
if not ok or type(wire) ~= "table" then return nil, _("响应无效") end
|
||||
local code = tonumber(wire.code)
|
||||
if code == 200 then
|
||||
return wire.data == nil and {} or wire.data, nil, wire
|
||||
end
|
||||
if (code == 401 or code == 403) and (cred.token or cred.cookie) and isCurrent(cred) then
|
||||
return nil, require("source.auth").expire(SOURCE_ID, _("请按这个做:\n1. 点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → iRead → iRead 账号」。\n2. 用电脑浏览器打开 https://iread.ankio.net/#/system/token ,登录你的账号。\n3. 在页面上新建一个个人 Token,复制它。\n4. 回到月读的输入框,把 Token 贴进去,点「登录」。\n\n如果平时用的是浏览器扩展,不要新建 Token,改做下面这些:\n")
|
||||
.. require("source.cookie_login").relogin(_("iRead"), "https://iread.ankio.net/#/system/token"))
|
||||
end
|
||||
return nil, type(wire.msg) == "string" and wire.msg ~= "" and wire.msg or _("请求失败")
|
||||
end
|
||||
|
||||
---@param path string
|
||||
---@param form table<string, any>
|
||||
---@param cred IReadCredential
|
||||
---@param ratio number 超时倍率
|
||||
---@param cb fun(data: any|nil, err: string|nil, res: table|nil, wire: table|nil)
|
||||
---@param session string|nil 服务端会话 Cookie(搜索结果存在会话里,轮询必须带回)
|
||||
---@return { cancel: fun() }
|
||||
local function post(path, form, cred, ratio, cb, session)
|
||||
local cookie = session and require("source.cookie_login").parse((cred.cookie or "") .. "; " .. session)
|
||||
or cred.cookie
|
||||
return Request.post(Client.BASE .. path, encode(form), {
|
||||
accept = "application/json",
|
||||
headers = { Cookie = cookie, Authorization = cred.token and ("Bearer " .. cred.token),
|
||||
["X-Requested-With"] = "XMLHttpRequest" },
|
||||
content_type = "application/x-www-form-urlencoded",
|
||||
timeout = require("http.timeout").net(ratio),
|
||||
}, function(raw, err, res)
|
||||
local data, decode_err, wire = Client.decode(raw, err, cred)
|
||||
cb(data, decode_err, res, wire)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 读取接口允许游客;已登录时使用用户配置的书源。
|
||||
---@param path string
|
||||
---@param form table<string, any>
|
||||
---@param ratio number
|
||||
---@param cb fun(data: any|nil, err: string|nil, res: table|nil)
|
||||
---@param session string|nil
|
||||
---@return { cancel: fun() }
|
||||
local function call(path, form, ratio, cb, session)
|
||||
local cred = Client.credential() or {}
|
||||
return post(path, form, cred, ratio, function(data, err, res, wire)
|
||||
if not isCurrent(cred) then cb(nil, _("登录状态已变化,请重试")); return end
|
||||
cb(data, err, res, wire)
|
||||
end, session)
|
||||
end
|
||||
|
||||
--- 校验凭据并取账号信息(设置页用,凭据由调用方显式给)。
|
||||
---@param cred IReadCredential
|
||||
---@param cb fun(user: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.userAsync(cred, cb)
|
||||
return post("/user/home", {}, cred, 0.5, function(data, err)
|
||||
if type(data) ~= "table" or type(data.nickname) ~= "string" or data.nickname == "" then
|
||||
cb(nil, err or _("iRead 账号信息为空")); return
|
||||
end
|
||||
if isCurrent(cred) then require("source.auth").ok(SOURCE_ID) end
|
||||
cb(data)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 列表响应必须是连续数组;坏行不能被 ipairs 静默丢掉并参与书架对账。
|
||||
---@param rows any
|
||||
---@param field string
|
||||
---@return boolean
|
||||
local function validRows(rows, field)
|
||||
if type(rows) ~= "table" then return false end
|
||||
local count = 0
|
||||
for key, row in pairs(rows) do
|
||||
if type(key) ~= "number" or key < 1 or key % 1 ~= 0
|
||||
or type(row) ~= "table" or type(row[field]) ~= "string" or row[field] == "" then return false end
|
||||
count = count + 1
|
||||
end
|
||||
return count == #rows
|
||||
end
|
||||
|
||||
--- 个人书架(服务端书记录原行)。空书架要另验登录态。
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.shelfAsync(cb)
|
||||
local cred = Client.credential()
|
||||
if not cred then return require("source.base").defer(cb, nil, _("未登录 iRead")) end
|
||||
local job, cancelled, pending = nil, false, true
|
||||
local first = post("/book/list", {}, cred, 0.5, function(data, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not isCurrent(cred) then cb(nil, _("登录状态已变化,请重试")); return end
|
||||
if not validRows(data, "name") then cb(nil, err or _("响应无效")); return end
|
||||
if #data > 0 then
|
||||
require("source.auth").ok(SOURCE_ID)
|
||||
cb(data); return
|
||||
end
|
||||
job = Client.userAsync(cred, function(user, user_err)
|
||||
if cancelled then return end
|
||||
if not isCurrent(cred) then cb(nil, _("登录状态已变化,请重试")); return end
|
||||
if not user then cb(nil, user_err); return end
|
||||
cb({})
|
||||
end)
|
||||
end)
|
||||
if pending then job = first end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
--- 目录:章节对象数组,顺序即阅读顺序。服务端分页(每页最多 200),翻到 hasMore 为假。
|
||||
---@param row table 书架行(必须含 id 字段)
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.chaptersAsync(row, cb)
|
||||
local all, job, cancelled = {}, nil, false
|
||||
local function page(n)
|
||||
job = call("/book/chapters", { id = row.id, page = n, pageSize = 200 }, 1, function(data, err, _res, wire)
|
||||
if cancelled then return end
|
||||
if not validRows(data, "title") then cb(nil, err or _("响应无效")); return end
|
||||
for _idx, chapter in ipairs(data) do all[#all + 1] = chapter end
|
||||
if #data > 0 and wire and wire.hasMore then
|
||||
-- 服务端一直说 hasMore 时不能无限翻:最多 100 页,超了报错,不交残缺目录。
|
||||
if n >= 100 then cb(nil, _("响应无效")); return end
|
||||
page(n + 1); return
|
||||
end
|
||||
if #all == 0 then cb(nil, _("iRead 目录为空")); return end
|
||||
cb(all)
|
||||
end)
|
||||
end
|
||||
page(1)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
--- 一章正文(纯文本)。index 是 0 基章序号。
|
||||
---@param row table 书架行(必须含 id 字段)
|
||||
---@param index integer
|
||||
---@param cb fun(text: string|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.contentAsync(row, index, cb)
|
||||
return call("/book/chapter", { id = row.id, index = index }, 1,
|
||||
function(data, err)
|
||||
local text = type(data) == "table" and data.content
|
||||
if type(text) ~= "string" or text == "" then cb(nil, err or _("iRead 章节内容为空")); return end
|
||||
cb(text)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 上报阅读位置:服务端用整行覆盖记录,所以带回原行,只改位置字段。
|
||||
---@param row table 书架行(必须含 id 字段)
|
||||
---@param pos { index: integer, title: string|nil, offset: integer }
|
||||
---@param cb fun(ok: boolean|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.saveProgressAsync(row, pos, cb)
|
||||
if not Client.credential() then return require("source.base").defer(cb, nil, _("未登录 iRead")) end
|
||||
local body = { id = row.id, durChapterIndex = pos.index, durChapterPos = pos.offset, durChapterTitle = pos.title or "" }
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local first = call("/book/process", body, 0.5, function(data, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if data == nil then cb(nil, err); return end
|
||||
job = Client.shelfAsync(function(rows, reason)
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, reason); return end
|
||||
for _idx, saved in ipairs(rows) do
|
||||
if saved.name == row.name and tonumber(saved.durChapterIndex) == pos.index
|
||||
and tonumber(saved.durChapterPos) == pos.offset then cb(true); return end
|
||||
end
|
||||
cb(nil, _("iRead 阅读进度更新未生效"))
|
||||
end)
|
||||
end)
|
||||
if pending then job = first end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
local SEARCH_INTERVAL = 2
|
||||
local SEARCH_TRIES = 30
|
||||
|
||||
--- 搜索:官网的协议是「提交 → 轮询」,两步要用同一个会话 Cookie(否则取结果 500)。POST /book/search 返回 task(各书源并行搜),
|
||||
--- 再每 2 秒 POST /book/search/result 取增量 result[] 直到 remaining 为 0。回调完整行数组(同书架行)。
|
||||
---@param form table<string, any> 提交表单(搜索是 bookname,发现是 type / url / id)
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
local function runAsync(form, cb)
|
||||
local cred = Client.credential() or {}
|
||||
local UIManager = require("ui/uimanager")
|
||||
local job, tick, done = nil, nil, false
|
||||
local rows, seen, tries = {}, {}, 0
|
||||
local function finish(data, err)
|
||||
done = true
|
||||
cb(data, err)
|
||||
end
|
||||
local function poll(task, session)
|
||||
if done then return end
|
||||
if not isCurrent(cred) then finish(nil, _("登录状态已变化,请重试")); return end
|
||||
tries = tries + 1
|
||||
local pending = true
|
||||
local inner = post("/book/search/result", { task = task }, cred, 0.5, function(data, err)
|
||||
pending = false
|
||||
if done then return end
|
||||
if not isCurrent(cred) then finish(nil, _("登录状态已变化,请重试")); return end
|
||||
local remaining = type(data) == "table" and tonumber(data.remaining)
|
||||
if not remaining or remaining < 0 or remaining % 1 ~= 0 or not validRows(data.result, "name") then
|
||||
finish(nil, err or _("响应无效")); return
|
||||
end
|
||||
for _idx, row in ipairs(data.result) do
|
||||
if not seen[row.name] then
|
||||
seen[row.name] = true
|
||||
rows[#rows + 1] = row
|
||||
end
|
||||
end
|
||||
if remaining == 0 then finish(rows); return end
|
||||
if tries >= SEARCH_TRIES then finish(nil, _("iRead 搜索超时,请重试")); return end
|
||||
tick = function() poll(task, session) end
|
||||
UIManager:scheduleIn(SEARCH_INTERVAL, tick)
|
||||
end, session)
|
||||
if pending then job = inner end
|
||||
end
|
||||
local pending = true
|
||||
local first = post("/book/search", form, cred, 0.5, function(data, err, res)
|
||||
pending = false
|
||||
if done then return end
|
||||
if not isCurrent(cred) then finish(nil, _("登录状态已变化,请重试")); return end
|
||||
if type(data) ~= "table" or type(data.task) ~= "string" or data.task == "" then
|
||||
finish(nil, err or _("响应无效")); return
|
||||
end
|
||||
local cookies = {}
|
||||
for _idx, value in ipairs(Request.headerAll(res, "Set-Cookie")) do
|
||||
cookies[#cookies + 1] = value:match("^([^;]+)")
|
||||
end
|
||||
poll(data.task, table.concat(cookies, "; "))
|
||||
end)
|
||||
if pending then job = first end
|
||||
return {
|
||||
cancel = function()
|
||||
done = true
|
||||
if tick then UIManager:unschedule(tick) end
|
||||
if job then job.cancel() end
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
---@param keyword string
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.searchAsync(keyword, cb)
|
||||
return runAsync({ bookname = keyword }, cb)
|
||||
end
|
||||
|
||||
--- 书源「发现」页的一个入口(规则里的 url 模板),回包同搜索。
|
||||
---@param source_id integer
|
||||
---@param url string
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.exploreAsync(source_id, url, cb)
|
||||
return runAsync({ type = "explore", url = url, id = source_id }, cb)
|
||||
end
|
||||
|
||||
--- 服务端当次挑出的一个书源的发现规则(每次调用换一个)。
|
||||
---@param cb fun(rules: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.exploreRulesAsync(cb)
|
||||
return call("/kindle/exploreRules", {}, 0.5, function(rules, err)
|
||||
if not validRows(rules, "bookSourceName") then cb(nil, err or _("响应无效")); return end
|
||||
for _idx, rule in ipairs(rules) do
|
||||
if type(rule.id) ~= "number" or rule.id < 1 or rule.id % 1 ~= 0
|
||||
or rule.exploreUrl ~= nil and type(rule.exploreUrl) ~= "table" then
|
||||
cb(nil, _("响应无效")); return
|
||||
end
|
||||
-- 分类里常夹着没有 url 的分隔标题(如 "↑ *** ↑"),丢掉,不当作整包无效
|
||||
local entries = {}
|
||||
for _entry_idx, entry in ipairs(rule.exploreUrl or {}) do
|
||||
if type(entry) == "table" and type(entry.title) == "string" and entry.title ~= ""
|
||||
and type(entry.url) == "string" and entry.url ~= "" then
|
||||
entries[#entries + 1] = entry
|
||||
end
|
||||
end
|
||||
rule.exploreUrl = entries
|
||||
end
|
||||
cb(rules)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 切换收藏状态。调用方必须先读书架判断是否需要切换,并在写后确认状态。
|
||||
--- 已在书库的书(row.id > 0)只传 id;未入库的传完整 hit 数据。
|
||||
---@param row table
|
||||
---@param cb fun(ok: boolean|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Client.favouriteAsync(row, cb)
|
||||
if not Client.credential() then return require("source.base").defer(cb, nil, _("未登录 iRead")) end
|
||||
local form
|
||||
if row.id and tonumber(row.id) > 0 then
|
||||
form = { id = row.id }
|
||||
else
|
||||
form = { id = row.id or 0, sourceId = row.sourceId or 0, bookUrl = row.bookUrl or "",
|
||||
tocUrl = row.tocUrl or "", name = row.name or "", author = row.author or "",
|
||||
coverUrl = row.coverUrl or "", intro = row.intro or "", kind = row.kind or "",
|
||||
originName = row.originName or row.origin or "", latestChapterTitle = row.latestChapterTitle or "",
|
||||
wordCount = row.wordCount or "", totalChapterNum = row.totalChapterNum or 0 }
|
||||
end
|
||||
return call("/book/favourite", form, 0.5, function(data, err) cb(data ~= nil or nil, err) end)
|
||||
end
|
||||
|
||||
return Client
|
||||
@@ -0,0 +1,33 @@
|
||||
return {
|
||||
["登录状态已变化,请重试"] = "The login session changed. Please retry.",
|
||||
["已登录:%1"] = "Signed in: %1",
|
||||
["章节目录保存失败"] = "Failed to save chapter list",
|
||||
["按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"] = "Press F12, open Network, refresh, and copy the entire Cookie request header",
|
||||
["缺少阅读章节位置"] = "Missing Legado chapter position",
|
||||
["iRead"] = "iRead",
|
||||
["iRead 账号"] = "iRead account",
|
||||
["iRead 账号信息为空"] = "iRead account info is empty",
|
||||
["iRead 个人 Token"] = "iRead personal token",
|
||||
["iRead 登录 Cookie"] = "iRead sign-in cookie",
|
||||
["无法确认是否为同一 iRead 账号,请先退出当前账号再登录"] = "Cannot verify that this is the same iRead account. Sign out of the current account before signing in.",
|
||||
["iRead 搜索超时,请重试"] = "iRead search timed out. Please retry.",
|
||||
["iRead 书架更新未生效"] = "The iRead shelf update was not applied.",
|
||||
["iRead 阅读进度更新未生效"] = "The iRead reading progress update was not applied.",
|
||||
["iRead 图片正文暂不支持,请使用网页版阅读"] = "iRead image content is not supported yet. Please use the website to read it.",
|
||||
["请按这个做:\n1. 点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → iRead → iRead 账号」。\n2. 用电脑浏览器打开 https://iread.ankio.net/#/system/token ,登录你的账号。\n3. 在页面上新建一个个人 Token,复制它。\n4. 回到月读的输入框,把 Token 贴进去,点「登录」。\n\n如果平时用的是浏览器扩展,不要新建 Token,改做下面这些:\n"] = "Do this:\n1. Tap Sign in again on this message. If there is no button, open Settings → Library → Account → iRead → iRead account.\n2. On a computer, open https://iread.ankio.net/#/system/token and sign in.\n3. Create a personal token on that page and copy it.\n4. Paste the token into the box in 月读 and tap Sign in.\n\nIf you normally use the browser extension, do not create a token. Do the following instead:\n",
|
||||
["iRead 目录为空"] = "iRead table of contents is empty",
|
||||
["iRead 章节内容为空"] = "iRead chapter is empty",
|
||||
["打开 iread.ankio.net/#/system/token ,新建个人 Token 后粘贴到这里"] = "Open iread.ankio.net/#/system/token, create a personal token, and paste it here",
|
||||
["未登录 iRead"] = "Not signed in to iRead",
|
||||
["未登录 · 点此填写 Token"] = "Not signed in · tap to enter token",
|
||||
["没有读到 Token"] = "No token found",
|
||||
["确定退出 iRead 账号?"] = "Sign out of iRead?",
|
||||
["请先搜索这本书再加入书库"] = "Search for this book first, then add it to your library",
|
||||
["这本书不在 iRead 书架上"] = "This book is not on the iRead shelf",
|
||||
["换一批书源"] = "Switch source",
|
||||
["iRead 暂无发现入口"] = "iRead has no discover entries",
|
||||
["iRead 书架与在线阅读"] = "iRead shelf and online reading",
|
||||
["书架同步"] = "Shelf sync",
|
||||
["搜索与发现"] = "Search and discover",
|
||||
["进度同步"] = "Progress sync",
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
return {
|
||||
["登录状态已变化,请重试"] = "登入狀態已改變,請重試",
|
||||
["已登录:%1"] = "已登入:%1",
|
||||
["章节目录保存失败"] = "章節目錄儲存失敗",
|
||||
["按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie"] = "按鍵盤 F12,點「網路」,重新整理網頁,複製請求標頭裡的整段 Cookie",
|
||||
["缺少阅读章节位置"] = "缺少閱讀章節位置",
|
||||
["iRead"] = "iRead",
|
||||
["iRead 账号"] = "iRead 帳號",
|
||||
["iRead 账号信息为空"] = "iRead 帳號資訊為空",
|
||||
["iRead 个人 Token"] = "iRead 個人 Token",
|
||||
["iRead 登录 Cookie"] = "iRead 登入 Cookie",
|
||||
["无法确认是否为同一 iRead 账号,请先退出当前账号再登录"] = "無法確認是否為同一 iRead 帳號,請先登出目前帳號再登入",
|
||||
["iRead 搜索超时,请重试"] = "iRead 搜尋逾時,請重試",
|
||||
["iRead 书架更新未生效"] = "iRead 書架更新未生效",
|
||||
["iRead 阅读进度更新未生效"] = "iRead 閱讀進度更新未生效",
|
||||
["iRead 图片正文暂不支持,请使用网页版阅读"] = "iRead 圖片正文暫不支援,請使用網頁版閱讀",
|
||||
["请按这个做:\n1. 点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → iRead → iRead 账号」。\n2. 用电脑浏览器打开 https://iread.ankio.net/#/system/token ,登录你的账号。\n3. 在页面上新建一个个人 Token,复制它。\n4. 回到月读的输入框,把 Token 贴进去,点「登录」。\n\n如果平时用的是浏览器扩展,不要新建 Token,改做下面这些:\n"] = "請按這個做:\n1. 點這個提示上的「重新登入」。沒有按鈕就打開「設定 → 書庫 → 帳號 → iRead → iRead 帳號」。\n2. 用電腦瀏覽器打開 https://iread.ankio.net/#/system/token ,登入你的帳號。\n3. 在頁面上新建一個個人 Token,複製它。\n4. 回到月讀的輸入框,把 Token 貼進去,點「登入」。\n\n如果平時用的是瀏覽器擴充功能,不要新建 Token,改做下面這些:\n",
|
||||
["iRead 目录为空"] = "iRead 目錄為空",
|
||||
["iRead 章节内容为空"] = "iRead 章節內容為空",
|
||||
["打开 iread.ankio.net/#/system/token ,新建个人 Token 后粘贴到这里"] = "打開 iread.ankio.net/#/system/token ,新建個人 Token 後貼到這裡",
|
||||
["未登录 iRead"] = "未登入 iRead",
|
||||
["未登录 · 点此填写 Token"] = "未登入 · 點此填寫 Token",
|
||||
["没有读到 Token"] = "沒有讀到 Token",
|
||||
["确定退出 iRead 账号?"] = "確定登出 iRead 帳號?",
|
||||
["请先搜索这本书再加入书库"] = "請先搜尋這本書再加入書庫",
|
||||
["这本书不在 iRead 书架上"] = "這本書不在 iRead 書架上",
|
||||
["换一批书源"] = "換一批書源",
|
||||
["iRead 暂无发现入口"] = "iRead 暫無發現入口",
|
||||
["iRead 书架与在线阅读"] = "iRead 書架與線上閱讀",
|
||||
["书架同步"] = "書架同步",
|
||||
["搜索与发现"] = "搜尋與發現",
|
||||
["进度同步"] = "進度同步",
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
return function()
|
||||
local copy = {}
|
||||
for k, v in pairs(require("moonsource_iread._meta")) do copy[k] = v end
|
||||
return copy
|
||||
end
|
||||
@@ -0,0 +1,78 @@
|
||||
--[[--
|
||||
iRead 数据映射(纯函数)。
|
||||
|
||||
书的身份是书名:服务端按(用户, 书名)唯一,换书源时 bookUrl / sourceId 会变而书名不变。
|
||||
|
||||
@module koplugin.book.moonsource_iread.mapper
|
||||
--]]
|
||||
|
||||
local Text = require("utils.text")
|
||||
|
||||
local Mapper = {}
|
||||
|
||||
local SOURCE_ID = "iread"
|
||||
|
||||
---@param row table
|
||||
---@return string|nil
|
||||
local function cover(row)
|
||||
local url = row.customCoverUrl
|
||||
if type(url) ~= "string" or url == "" then url = row.coverUrl end
|
||||
if type(url) ~= "string" or url == "" then return nil end
|
||||
if url:sub(1, 2) == "//" then url = "https:" .. url end
|
||||
return url
|
||||
end
|
||||
|
||||
--- 书架行 → Book[]。
|
||||
---@param rows table[]
|
||||
---@return Book[]
|
||||
function Mapper.shelfList(rows)
|
||||
local out = {}
|
||||
for _idx, row in ipairs(rows) do
|
||||
if type(row.name) == "string" and row.name ~= "" then
|
||||
out[#out + 1] = {
|
||||
source_id = SOURCE_ID,
|
||||
stable_id = row.name,
|
||||
title = row.name,
|
||||
authors = row.author,
|
||||
category = row.kind,
|
||||
intro = row.intro and Text.stripHtml(row.intro),
|
||||
cover = cover(row),
|
||||
meta = { source = { iread = row } },
|
||||
}
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
--- 书架行里的阅读位置(序号 0 基)。没读过返回 nil。
|
||||
---@param row table|nil
|
||||
---@return { index: integer, title: string|nil, offset: integer }|nil
|
||||
function Mapper.readPos(row)
|
||||
local index = row and tonumber(row.durChapterIndex)
|
||||
if not index then return nil end
|
||||
local offset = tonumber(row.durChapterPos) or 0
|
||||
if index == 0 and offset == 0 and (tonumber(row.durChapterTime) or 0) <= 0 then return nil end
|
||||
local title = row.durChapterTitle
|
||||
return { index = index, title = type(title) == "string" and title ~= "" and title or nil, offset = offset }
|
||||
end
|
||||
|
||||
--- 目录:按服务端顺序,uid 是 0 基章序号(开章接口只要书 id 与序号)。
|
||||
---@param rows table[]
|
||||
---@return BookChapter[]
|
||||
function Mapper.toc(rows)
|
||||
local toc = {}
|
||||
for i, row in ipairs(rows) do
|
||||
toc[i] = { idx = i, uid = tostring(i - 1), title = row.title }
|
||||
end
|
||||
return toc
|
||||
end
|
||||
|
||||
--- 正文:服务端给纯文本;规则偶尔带 HTML 标签,剥掉免得标签原样显示。
|
||||
---@param content string
|
||||
---@return string
|
||||
function Mapper.text(content)
|
||||
if content:find("<%s*/?%a[^>]*>") then return Text.stripHtml(content) end
|
||||
return content
|
||||
end
|
||||
|
||||
return Mapper
|
||||
@@ -0,0 +1,183 @@
|
||||
--[[--
|
||||
iRead 源设置:登录。
|
||||
|
||||
两种进法:远程管理里的「浏览器登录」(浏览器扩展交网页 Cookie),或在 iread.ankio.net 创建「个人 Token」粘贴进来。
|
||||
两条路都先向服务端校验,通过才保存并重载源;Token 优先于 Cookie,换一种登录会清掉另一种。
|
||||
|
||||
@module koplugin.book.moonsource_iread.setting
|
||||
--]]
|
||||
|
||||
local _ = require("l10n").get("moonsource_iread")
|
||||
local T = require("ffi/util").template
|
||||
|
||||
local SOURCE_ID = "iread"
|
||||
local Setting = {}
|
||||
|
||||
---@return boolean
|
||||
local function loggedIn()
|
||||
return require("moonsource_iread.client").credential() ~= nil
|
||||
end
|
||||
|
||||
---@return string, boolean
|
||||
function Setting.rowStatus()
|
||||
local on = loggedIn()
|
||||
return on and _("已登录") or _("未登录 · 点此填写 Token"), on
|
||||
end
|
||||
|
||||
--- 接受裸 Token,或带 `Bearer ` 前缀的整段。
|
||||
---@param input string
|
||||
---@return string
|
||||
function Setting.parseToken(input)
|
||||
return (require("utils.text").trim(input):gsub("^[Bb]earer%s+", ""))
|
||||
end
|
||||
|
||||
---- 保留本站和有效父域 Cookie,不收 ankio.net 下其他站点的登录态。
|
||||
---@param payload table
|
||||
---@return string
|
||||
function Setting.fromPayload(payload)
|
||||
local cookies = {}
|
||||
for _idx, cookie in ipairs(type(payload.cookies) == "table" and payload.cookies or {}) do
|
||||
local host = type(cookie) == "table" and type(cookie.domain) == "string"
|
||||
and cookie.domain:gsub("^%.", ""):lower()
|
||||
if host == "ankio.net" or host == "iread.ankio.net" then cookies[#cookies + 1] = cookie end
|
||||
end
|
||||
return require("source.cookie_login").fromPayload({ cookies = cookies }, "ankio.net")
|
||||
end
|
||||
|
||||
--- 校验凭据,通过才保存(Token 与 Cookie 互斥)并重载源。
|
||||
---@param cred IReadCredential
|
||||
---@param cb fun(user: table|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Setting.save(cred, cb)
|
||||
local client = require("moonsource_iread.client")
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource(SOURCE_ID)
|
||||
local previous_token, previous_cookie = cfg.token, cfg.cookie
|
||||
local previous = client.credential()
|
||||
local cancelled = false
|
||||
local job = client.userAsync(cred, function(user, err)
|
||||
if cancelled then return end
|
||||
if cfg.token ~= previous_token or cfg.cookie ~= previous_cookie then
|
||||
cb(nil, _("登录状态已变化,请重试")); return
|
||||
end
|
||||
if not user then return cb(nil, err or _("登录失败")) end
|
||||
if previous and (cred.token ~= previous.token or cred.cookie ~= previous.cookie) then
|
||||
local cleared = require("source.logout").clear(SOURCE_ID, function()
|
||||
cfg.token, cfg.cookie = nil, nil
|
||||
settings.saveSource(SOURCE_ID, cfg)
|
||||
end)
|
||||
if not cleared then cb(nil, _("准备本地同步失败")); return end
|
||||
end
|
||||
cfg.token, cfg.cookie = cred.token, cred.cookie
|
||||
settings.saveSource(SOURCE_ID, cfg)
|
||||
require("source.auth").ok(SOURCE_ID)
|
||||
require("source.registry").reload()
|
||||
cb(user)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
job.cancel()
|
||||
end }
|
||||
end
|
||||
|
||||
--- 浏览器扩展登录(features/cookie)。
|
||||
Setting.browser = {
|
||||
domain = "iread.ankio.net",
|
||||
scope = "cookie",
|
||||
login = "https://iread.ankio.net/#/system/token",
|
||||
check = "https://iread.ankio.net/#/system/token",
|
||||
icon = "https://iread.ankio.net/static/icons/favicon-32x32.png",
|
||||
isAuthorized = function()
|
||||
return loggedIn() and not require("source.auth").expired(SOURCE_ID)
|
||||
end,
|
||||
apply = function(payload, cb)
|
||||
local cookie = Setting.fromPayload(payload)
|
||||
if cookie == "" then return cb(false, _("没有读到 Cookie")) end
|
||||
return Setting.save({ cookie = cookie }, function(user, err) cb(user ~= nil, err) end)
|
||||
end,
|
||||
}
|
||||
|
||||
---@param cred IReadCredential
|
||||
local function verify(cred)
|
||||
local Toast = require("ui.themes.widgets.toast")
|
||||
require("ui/network/manager"):runWhenConnected(function()
|
||||
local job
|
||||
local waiting = require("ui.themes.widgets.progress_dialog"):new{
|
||||
title = _("iRead"), text = _("正在校验登录…"), dismissable = false,
|
||||
on_cancel = function() if job then job.cancel() end end,
|
||||
}
|
||||
waiting:show()
|
||||
job = Setting.save(cred, function(user, err)
|
||||
waiting:close()
|
||||
if not user then
|
||||
Toast:new{ text = err or _("登录失败"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
Toast:new{ text = T(_("已登录:%1"), user.nickname), timeout = 2 }:show()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
local function showLogin()
|
||||
require("ui.themes.widgets.input"):new{
|
||||
title = _("iRead 个人 Token"),
|
||||
fields = { { text = "", hint = _("打开 iread.ankio.net/#/system/token ,新建个人 Token 后粘贴到这里") } },
|
||||
ok_text = _("登录"),
|
||||
callback = function(value)
|
||||
local token = Setting.parseToken(value or "")
|
||||
if token == "" then
|
||||
require("ui.themes.widgets.toast"):new{ text = _("没有读到 Token"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
verify({ token = token })
|
||||
end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
local function showCookieLogin()
|
||||
require("ui.themes.widgets.input"):new{
|
||||
title = _("iRead 登录 Cookie"),
|
||||
fields = { { text = "", hint = _("按键盘 F12,点「网络」,刷新网页,复制请求标头里的整段 Cookie") } },
|
||||
ok_text = _("登录"),
|
||||
callback = function(value)
|
||||
local cookie = require("source.cookie_login").parse(value or "")
|
||||
if cookie == "" then
|
||||
require("ui.themes.widgets.toast"):new{ text = _("没有读到 Cookie"), timeout = 3 }:show()
|
||||
return
|
||||
end
|
||||
verify({ cookie = cookie })
|
||||
end,
|
||||
}:show()
|
||||
end
|
||||
|
||||
---@return table[]
|
||||
function Setting.rows()
|
||||
local Row = require("ui.themes.widgets.row")
|
||||
local CookieLogin = require("source.cookie_login")
|
||||
local rows = {
|
||||
function(iw)
|
||||
local status, status_on = Setting.rowStatus()
|
||||
return Row.widget({ width = iw, chevron = true, kind = "nav", icon = "account_circle",
|
||||
title = _("iRead 账号"), status = status, status_on = status_on,
|
||||
callback = function() showLogin() end })
|
||||
end,
|
||||
CookieLogin.browserRow(),
|
||||
function(iw)
|
||||
return Row.widget({ width = iw, chevron = true, kind = "nav", icon = "content_paste",
|
||||
title = _("粘贴 Cookie"), callback = showCookieLogin })
|
||||
end,
|
||||
}
|
||||
if not loggedIn() then return rows end
|
||||
rows[#rows + 1] = require("source.logout").row(SOURCE_ID, _("确定退出 iRead 账号?"), function()
|
||||
local settings = require("utils.settings")
|
||||
local cfg = settings.getSource(SOURCE_ID)
|
||||
cfg.token, cfg.cookie = nil, nil
|
||||
settings.saveSource(SOURCE_ID, cfg)
|
||||
end)
|
||||
return rows
|
||||
end
|
||||
|
||||
--- 登录失效提示的「重新登录」直达入口(desktop.sync.promptLogin)。
|
||||
Setting.login = showLogin
|
||||
|
||||
return Setting
|
||||
@@ -0,0 +1,366 @@
|
||||
--[[--
|
||||
iRead(iread.ankio.net)数据源。
|
||||
|
||||
个人 Token 鉴权(setting.lua)。书架是服务端的收藏记录,书城搜索的书加入书库时经 /book/favourite 收藏,本地删除会上行;
|
||||
目录 / 正文按章落盘走公共 source/chapter;进度只到章,开书拉取、关书上报。笔记和统计没有接口。
|
||||
服务端的书记录(bookUrl / sourceId 等)是目录、正文接口的必要参数,随书籍元数据保存;拉进度强制拉新行,写进度用缓存行(只带 id 与位置)。
|
||||
|
||||
@module koplugin.book.moonsource_iread.source
|
||||
--]]
|
||||
|
||||
local Browse = require("source.browse")
|
||||
local Client = require("moonsource_iread.client")
|
||||
local Mapper = require("moonsource_iread.mapper")
|
||||
local Shelf = require("source.shelf")
|
||||
local SourceBase = require("source.base")
|
||||
local Toc = require("source.toc")
|
||||
local _ = require("l10n").get("moonsource_iread")
|
||||
|
||||
local IRead = {}
|
||||
|
||||
---@return BookSourceMeta
|
||||
function IRead.meta()
|
||||
return require("moonsource_iread.loadmeta")()
|
||||
end
|
||||
|
||||
---@class IReadSource : SourceBase
|
||||
---@field rows table<string, table>|nil 书名 → 最近一次书架行
|
||||
---@field found table<string, table>|nil 书名 → 最近一次搜索行(收藏时整行带回)
|
||||
local Source = setmetatable({ shelf_add = true }, { __index = SourceBase })
|
||||
Source.__index = Source
|
||||
|
||||
---@return IReadSource
|
||||
function IRead.new()
|
||||
local meta = IRead.meta()
|
||||
return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, Source)
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Source:configured()
|
||||
return Client.credential() ~= nil
|
||||
end
|
||||
|
||||
---@param rows table[]
|
||||
---@param stable_id string
|
||||
---@return table|nil
|
||||
local function findRow(rows, stable_id)
|
||||
for _idx, row in ipairs(rows) do
|
||||
if row.name == stable_id then return row end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param self IReadSource
|
||||
---@param stable_id string
|
||||
---@return table|nil
|
||||
local function cachedRow(self, stable_id)
|
||||
local row = self.rows and self.rows[stable_id] or self.found and self.found[stable_id]
|
||||
if row then return row end
|
||||
local book = require("book.books_db").get(self.id, stable_id)
|
||||
local stored = book and book.meta and book.meta.source and book.meta.source.iread
|
||||
if type(stored) == "table" and stored.name == stable_id then return stored end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- 拉书架并刷新行缓存。
|
||||
---@param cb fun(rows: table[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:fetchShelf(cb)
|
||||
local cancelled = false
|
||||
local job = Client.shelfAsync(function(rows, err)
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, err); return end
|
||||
local cache = {}
|
||||
for _idx, row in ipairs(rows) do cache[row.name] = row end
|
||||
self.rows = cache
|
||||
cb(rows)
|
||||
end)
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
job.cancel()
|
||||
end }
|
||||
end
|
||||
|
||||
--- 书架远端:Shelf.syncAsync 要的三个冒号方法。收藏接口是开关,加入前先确认远端还没有这本书。
|
||||
local Remote = {}
|
||||
|
||||
function Remote:shelfSyncAsync(cb) return self.source:fetchShelf(cb) end
|
||||
|
||||
--- 收藏接口是开关;写后读回书架才确认,不把 HTTP 成功当成成员关系已改变。
|
||||
local function changeShelf(source, row, added, cb)
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local first = Client.favouriteAsync(row, function(ok, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not ok then cb(nil, err); return end
|
||||
job = source:fetchShelf(function(rows, reason)
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, reason); return end
|
||||
if (findRow(rows, row.name) ~= nil) ~= added then
|
||||
cb(nil, _("iRead 书架更新未生效")); return
|
||||
end
|
||||
cb(true)
|
||||
end)
|
||||
end)
|
||||
if pending then job = first end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
--- 搜索行随元数据持久化,重启或书城缓存命中后仍可推送本地新加书。
|
||||
function Remote:addToShelfAsync(stable_id, cb)
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local first = self.source:fetchShelf(function(rows, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, err); return end
|
||||
if findRow(rows, stable_id) then cb(true); return end
|
||||
local row = cachedRow(self.source, stable_id)
|
||||
if not row then cb(nil, _("请先搜索这本书再加入书库")); return end
|
||||
job = changeShelf(self.source, row, true, cb)
|
||||
end)
|
||||
if pending then job = first end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
--- 远端已经没有这本书就算删除完成(收藏接口是开关,不能盲调)。
|
||||
function Remote:removeFromShelfAsync(stable_id, cb)
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local first = self.source:fetchShelf(function(rows, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, err); return end
|
||||
local row = findRow(rows, stable_id)
|
||||
if not row then cb(true); return end
|
||||
job = changeShelf(self.source, row, false, cb)
|
||||
end)
|
||||
if pending then job = first end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
function Source:syncBooksAsync(opts, cb)
|
||||
if not self:configured() then return SourceBase.skipped(cb, "iread not logged in") end
|
||||
local remote = setmetatable({ source = self }, { __index = Remote })
|
||||
return Shelf.syncAsync(self.id, remote, Mapper.shelfList, opts, cb)
|
||||
end
|
||||
|
||||
--- 书城搜索:回包形状同 listStoreAsync。搜索行同书架行,缓存起来供目录 / 正文接口使用。
|
||||
---@param keyword string
|
||||
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:searchAsync(keyword, cb)
|
||||
return Client.searchAsync(keyword, function(rows, err)
|
||||
if not rows then cb(nil, err); return end
|
||||
self.found = self.found or {}
|
||||
for _idx, row in ipairs(rows) do self.found[row.name] = row end
|
||||
cb({ data = Mapper.shelfList(rows) })
|
||||
end)
|
||||
end
|
||||
|
||||
--- 书城浏览:服务端每次随机给一个书源的「发现」入口(榜单 / 分类),入口就是 chip,节点是「书源 id:入口序号」;
|
||||
--- 书源规则缓存在实例上,「换一批」重取规则。结果行与搜索同形,进 self.found 供加书架。
|
||||
---@param opts { node?: string, limit?: integer }
|
||||
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
|
||||
---@return { cancel: fun() }
|
||||
function Source:browseAsync(opts, cb)
|
||||
local limit = opts.limit or 60
|
||||
local handle = {}
|
||||
function handle.cancel()
|
||||
handle.cancelled = true
|
||||
if handle.inner then handle.inner.cancel() end
|
||||
end
|
||||
local function explore(rule, node)
|
||||
local chips, picked = {}, nil
|
||||
for idx, entry in ipairs(rule.exploreUrl) do
|
||||
if entry.url and entry.url ~= "" then
|
||||
local id = rule.id .. ":" .. idx
|
||||
chips[#chips + 1] = { id = id, title = entry.title }
|
||||
if id == node then picked = entry end
|
||||
end
|
||||
end
|
||||
local categories = Browse.group(chips, node)
|
||||
categories.title = _("分类")
|
||||
local groups = { categories, { chips = { { id = "next", title = _("换一批书源") } } } }
|
||||
if not picked then
|
||||
cb({ data = {}, groups = groups })
|
||||
return
|
||||
end
|
||||
local paginated = picked.url:find("{{page}}", 1, true) ~= nil
|
||||
handle.inner = Browse.pages(function(page, done)
|
||||
local url = paginated and picked.url:gsub("{{page}}", tostring(page)) or picked.url
|
||||
return Client.exploreAsync(rule.id, url, function(rows, err)
|
||||
if handle.cancelled then return end
|
||||
if not rows then done(nil, err); return end
|
||||
self.found = self.found or {}
|
||||
for _idx, row in ipairs(rows) do self.found[row.name] = row end
|
||||
done(Mapper.shelfList(rows), nil, paginated)
|
||||
end)
|
||||
end, limit, groups, cb)
|
||||
end
|
||||
local sid = tonumber((opts.node or ""):match("^(%d+):"))
|
||||
local cached = sid and self.explore_rules and self.explore_rules[sid]
|
||||
if cached then
|
||||
explore(cached, opts.node)
|
||||
return handle
|
||||
end
|
||||
local pending = true
|
||||
local first = Client.exploreRulesAsync(function(rules, err)
|
||||
pending = false
|
||||
if handle.cancelled then return end
|
||||
if not rules then cb(nil, err); return end
|
||||
local rule
|
||||
for _idx, candidate in ipairs(rules) do
|
||||
if type(candidate.exploreUrl) == "table" and #candidate.exploreUrl > 0 then rule = candidate end
|
||||
end
|
||||
if not rule then cb(nil, _("iRead 暂无发现入口")); return end
|
||||
self.explore_rules = self.explore_rules or {}
|
||||
self.explore_rules[rule.id] = rule
|
||||
explore(rule, nil)
|
||||
end)
|
||||
if pending then handle.inner = first end
|
||||
return handle
|
||||
end
|
||||
|
||||
--- 取书架行再交给 fn(row)(返回它发起的任务,取消时一并取消);fresh 为真强制重拉书架
|
||||
--- (拉云端进度要读最新行)。
|
||||
---@param identity BookIdentity
|
||||
---@param fresh boolean
|
||||
---@param cb function
|
||||
---@param fn fun(row: table): { cancel: fun() }|nil
|
||||
---@return { cancel: fun() }
|
||||
function Source:withRow(identity, fresh, cb, fn)
|
||||
local cached = not fresh and cachedRow(self, identity.stable_id)
|
||||
-- 搜索命中尚未入库时 id 为 0,服务端按「请传入书库id」拒绝,必须回书架取真实行
|
||||
if cached and (tonumber(cached.id) or 0) > 0 then return fn(cached) or SourceBase.defer(function() end) end
|
||||
local cancelled, job, pending = false, nil, true
|
||||
local first = self:fetchShelf(function(rows, err)
|
||||
pending = false
|
||||
if cancelled then return end
|
||||
if not rows then cb(nil, err); return end
|
||||
local row = findRow(rows, identity.stable_id)
|
||||
if not row then cb(nil, _("这本书不在 iRead 书架上")); return end
|
||||
job = fn(row) or job
|
||||
end)
|
||||
if pending then job = first end
|
||||
return { cancel = function()
|
||||
cancelled = true
|
||||
if job then job.cancel() end
|
||||
end }
|
||||
end
|
||||
|
||||
function Source:getProgressAsync(identity, cb)
|
||||
if not self:configured() then return SourceBase.defer(cb, nil, nil, { empty = true }) end
|
||||
return self:withRow(identity, true, cb, function(row)
|
||||
local read = Mapper.readPos(row)
|
||||
if not read then cb(nil, nil, { empty = true }); return nil end
|
||||
return self:loadTocAsync(identity, function(toc, reason)
|
||||
if not toc then cb(nil, reason); return end
|
||||
local idx = math.min(read.index + 1, #toc)
|
||||
cb({
|
||||
chapter_idx = idx,
|
||||
chapter_title = read.title or toc[idx].title,
|
||||
fraction = (idx - 1) / #toc,
|
||||
extra = { chapter_uid = toc[idx].uid, chapter_idx = idx },
|
||||
})
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
function Source:putProgressAsync(identity, pos, cb)
|
||||
if not self:configured() then return SourceBase.defer(cb, nil, _("未登录 iRead")) end
|
||||
-- 上报只带书 id 与位置字段,缓存行够用;写后 saveProgressAsync 自己读回书架确认。
|
||||
return self:withRow(identity, false, cb, function(row)
|
||||
local idx = tonumber(pos.chapter_idx) or tonumber(identity.chapter_idx)
|
||||
if not idx then cb(nil, _("缺少阅读章节位置")); return nil end
|
||||
local toc = Toc.read(self.id, identity.stable_id)
|
||||
local chapter = toc and toc[idx]
|
||||
local read = Mapper.readPos(row)
|
||||
return Client.saveProgressAsync(row, {
|
||||
index = idx - 1,
|
||||
title = chapter and chapter.title,
|
||||
offset = read and read.index == idx - 1 and read.offset or 0,
|
||||
}, cb)
|
||||
end)
|
||||
end
|
||||
|
||||
--- 书库里已有的元数据。
|
||||
function Source:getDetailAsync(identity, cb)
|
||||
return SourceBase.defer(cb, identity.book or require("book.books_db").get(self.id, identity.stable_id))
|
||||
end
|
||||
|
||||
--- 强制拉目录并写回;失败由调用方继续用旧缓存。
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
||||
---@return { cancel: fun() }|nil
|
||||
local function fetchTocAsync(self, identity, cb)
|
||||
return self:withRow(identity, false, cb, function(row)
|
||||
return Client.chaptersAsync(row, function(rows, err)
|
||||
if not rows then cb(nil, err); return end
|
||||
local toc = Mapper.toc(rows)
|
||||
if not Toc.put(self.id, identity.stable_id, toc) then cb(nil, _("章节目录保存失败")); return end
|
||||
cb(toc)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param identity BookIdentity
|
||||
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
|
||||
---@param force boolean|nil 真=跳过缓存强制拉
|
||||
---@return { cancel: fun() }|nil
|
||||
function Source:loadTocAsync(identity, cb, force)
|
||||
if not force then
|
||||
local cached = Toc.read(self.id, identity.stable_id)
|
||||
if cached and #cached > 0 then
|
||||
return SourceBase.defer(cb, cached)
|
||||
end
|
||||
end
|
||||
return fetchTocAsync(self, identity, cb)
|
||||
end
|
||||
|
||||
Source.refreshTocAsync = fetchTocAsync
|
||||
|
||||
---@return ChapterFetchContent
|
||||
function Source:contentFetcher()
|
||||
return function(identity, chapter, done)
|
||||
return self:withRow(identity, false, done, function(row)
|
||||
return Client.contentAsync(row, tonumber(chapter.uid), function(text, err)
|
||||
if not text then done(nil, err); return end
|
||||
if text:find("<[Ii][Mm][Gg][%s>]") then
|
||||
done(nil, _("iRead 图片正文暂不支持,请使用网页版阅读")); return
|
||||
end
|
||||
local content = Mapper.text(text)
|
||||
if content == "" then done(nil, _("iRead 章节内容为空")); return end
|
||||
done({ title = chapter.title, html = require("utils.text").chapterHtml(chapter.title, content) })
|
||||
end)
|
||||
end)
|
||||
end
|
||||
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 = self:contentFetcher(),
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
|
||||
return require("source.chapter").prefetchAsync(identity, identity.book, toc, from_idx, count, {
|
||||
fetchContent = self:contentFetcher(),
|
||||
}, cb)
|
||||
end
|
||||
|
||||
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
|
||||
return require("source.chapter").cacheAllAsync(self, identity, self:contentFetcher(), on_progress, cb, 0, range)
|
||||
end
|
||||
|
||||
return IRead
|
||||
@@ -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