✨ 初始提交:月读第三方书源插件集合

每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 12:08:03 +08:00
commit 730b2d6d9f
27 files changed
+17665

No files matched your search

@@ -0,0 +1,463 @@
--[[--
QQ阅读 HTTP 客户端(仅异步)。
接口在 https://book.qq.com/api/ ,code 0 成功,-4 是登录失效(HTTP 仍是 200)。
目录和免费章匿名可读。付费章同样 code 0;authStatus=1 已购、2 会员,其余状态不能缓存预览。
encrypt / fontEncrypt 为真是 Fock 加密 + 乱序字体(已购章),解密见 fock.lua / scramble.lua / glyph.lua。
书架分页 pageNo / pageSize / hasNext,置顶和普通列表拼在一起。
进度只到章:POST bookshelf/update/reading/process,只传 bid 和 lastReadChapterId。
@module koplugin.book.moonsource_qqreader.client
--]]
local Fock = require("moonsource_qqreader.fock")
local JSON = require("json")
local logger = require("utils.log")
local Mapper = require("moonsource_qqreader.mapper")
local Request = require("http.request")
local SourceBase = require("source.base")
local Text = require("utils.text")
local _ = require("l10n").get("moonsource_qqreader")
local Client = {}
local SOURCE_ID = "ext.qqreader"
local WEB = "https://book.qq.com"
local API = WEB .. "/api"
local UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
.. "Chrome/120.0.0.0 Safari/537.36"
local PAGE_SIZE = 100
local AUTH_FAIL = -4
---@return string|nil
function Client.cookie()
local cookie = require("utils.settings").getSource(SOURCE_ID).cookie
return type(cookie) == "string" and cookie ~= "" and cookie or nil
end
---@param cookie string|nil
---@param referer string|nil
---@return table
local function headers(cookie, referer)
return {
["User-Agent"] = UA,
Referer = referer or WEB .. "/book-shelf",
}
end
---@param params table|nil
---@return string
local function query(params)
local parts = {}
for key, value in pairs(params or {}) do
parts[#parts + 1] = Text.urlEncode(key) .. "=" .. Text.urlEncode(value)
end
table.sort(parts)
return table.concat(parts, "&")
end
--- 书号 / 章号在官方请求里是数字。不是整数就原样当字符串。
---@param id string|number
---@return string|number
local function jsonId(id)
local text = tostring(id)
local n = tonumber(text)
if n and n == math.floor(n) and tostring(math.floor(n)) == text then return n end
return text
end
--- 登录态失效只标记当前已保存的 Cookie。
---@param cookie string|nil
---@param msg string
---@return string
local function expired(cookie, msg)
if cookie and cookie ~= "" and cookie == Client.cookie() then
return require("source.auth").expire(SOURCE_ID, msg)
end
return msg
end
---@return string
local function loginGone()
return _("点这个提示上的「重新登录」。没有按钮就打开「设置 → 书库 → 账号 → QQ阅读 → QQ阅读账号」。二维码出来后,打开手机上的 QQ阅读或起点 App,点扫一扫,对准屏幕,在手机上点确认。")
.. "\n\n" .. _("扫了还是进不去,就改用浏览器,按下面做:") .. "\n"
.. require("source.cookie_login").relogin(_("QQ阅读"), "https://book.qq.com/book-shelf")
end
---@param raw string|nil
---@param err any
---@param cookie string|nil
---@return table|nil wire
---@return string|nil err
local function decode(raw, err, cookie)
if not raw then return nil, err and tostring(err) or _("请求失败") end
local ok, wire = pcall(JSON.decode, raw)
if not ok or type(wire) ~= "table" then return nil, _("响应无效") end
if tonumber(wire.code) == 0 then
if cookie and cookie == Client.cookie() then require("source.auth").ok(SOURCE_ID) end
return wire
end
local msg = type(wire.msg) == "string" and wire.msg ~= "" and wire.msg or _("请求失败")
if tonumber(wire.code) == AUTH_FAIL then msg = expired(cookie, loginGone()) end
return nil, msg
end
---@param method string
---@param path string
---@param params table|nil
---@param body string|nil
---@param cookie string|nil
---@param cb fun(wire: table|nil, err: string|nil)
---@param referer string|nil
---@return HttpJob
local function call(method, path, params, body, cookie, cb, referer)
local url = API .. path
if params then url = url .. "?" .. query(params) end
local opts = {
accept = "application/json",
headers = headers(cookie, referer),
jar = require("http.cookie_jar").session(SOURCE_ID, cookie),
timeout = require("http.timeout").net(1),
}
local function done(raw, err, res)
local status = res and tonumber(res.code)
if not raw and cookie and (status == 302 or status == 401) then
cb(nil, expired(cookie, loginGone()))
return
end
cb(decode(raw, err, cookie))
end
if method == "POST" then
opts.headers.Origin = WEB
opts.content_type = "application/json"
return Request.post(url, body, opts, done)
end
return Request.get(url, opts, done)
end
---@param cookie string
---@param cb fun(ok: boolean|nil, err: string|nil, account: { id: string }|nil)
---@return { cancel: fun() }
function Client.loginCheckAsync(cookie, cb)
if type(cookie) ~= "string" or cookie == "" then
return SourceBase.defer(cb, nil, _("没有读到 Cookie"))
end
return call("GET", "/user/homepage", nil, nil, cookie, function(wire, err)
if not wire or type(wire.data) ~= "table" then
cb(nil, err or loginGone())
return
end
local guid = cookie:match("^ywguid=([^;]+)") or cookie:match(";%s*ywguid=([^;]+)")
local uid = cookie:match("^uid=([^;]+)") or cookie:match(";%s*uid=([^;]+)")
local id = guid or uid
if not id or not id:match("^%d+$") or (guid and uid and guid ~= uid) then
cb(nil, _("QQ阅读账号标识缺失")); return
end
cb(true, nil, { id = id })
end)
end
--- 拉书架。每页拼接置顶和普通列表;hasNext 为真但没有新书时明确失败。
---@param cookie string
---@param cb fun(rows: table[]|nil, err: string|nil)
---@return { cancel: fun() }
function Client.shelfAsync(cookie, cb)
if type(cookie) ~= "string" or cookie == "" then
return SourceBase.defer(cb, nil, _("未登录 QQ阅读"))
end
local cancelled, job, books, seen = false, nil, {}, {}
local function add(list)
local n = 0
for _idx, row in ipairs(type(list) == "table" and list or {}) do
if type(row) ~= "table" or not row.bid then return nil end
local id = tostring(row.bid)
if id and not seen[id] then
seen[id] = true
books[#books + 1] = row
n = n + 1
end
end
return n
end
local function page(n)
if cancelled then return end
local pending = true
local inner = call("GET", "/bookshelf/query", { pageNo = n, pageSize = PAGE_SIZE }, nil, cookie, function(wire, err)
pending = false
if cancelled then return end
if not wire then cb(nil, err); return end
local data = wire.data
if type(data) ~= "table" or type(data.bookList) ~= "table"
or (data.topBookList ~= nil and type(data.topBookList) ~= "table") then
cb(nil, _("QQ阅读书架响应无效")); return
end
local top, normal = add(data.topBookList), add(data.bookList)
if not top or not normal then cb(nil, _("QQ阅读书架响应无效")); return end
local added = top + normal
local more = data.hasNext == true or tonumber(data.hasNext) == 1
if more and added == 0 then cb(nil, _("QQ阅读书架分页没有进展")); return end
if not more then cb(books) else page(n + 1) end
end)
if pending then job = inner end
end
page(1)
return { cancel = function()
cancelled = true
if job then job.cancel() end
end }
end
---@param book_id string
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return { cancel: fun() }
function Client.addAsync(book_id, cb)
local cookie = Client.cookie()
if not cookie then return SourceBase.defer(cb, nil, _("未登录 QQ阅读")) end
return call("POST", "/bookshelf/add", nil, JSON.encode({ bid = jsonId(book_id) }), cookie, function(wire, err)
cb(wire and true or nil, err)
end)
end
---@param book_id string
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return { cancel: fun() }
function Client.removeAsync(book_id, cb)
local cookie = Client.cookie()
if not cookie then return SourceBase.defer(cb, nil, _("未登录 QQ阅读")) end
return call("POST", "/bookshelf/delete", nil, JSON.encode({ bids = { jsonId(book_id) } }), cookie, function(wire, err)
cb(wire and true or nil, err)
end)
end
--- 上报读到哪一章。不传 lastReadOffset:官方更新也不传。
---@param book_id string
---@param chapter_id string
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return { cancel: fun() }
function Client.updateReadAsync(book_id, chapter_id, cb)
local cookie = Client.cookie()
if not cookie then return SourceBase.defer(cb, nil, _("未登录 QQ阅读")) end
return call("POST", "/bookshelf/update/reading/process", nil, JSON.encode({
bid = jsonId(book_id),
lastReadChapterId = jsonId(chapter_id),
}), cookie, function(wire, err)
cb(wire and true or nil, err)
end)
end
--- 官网 JSON 搜索;匿名,每页十条,hasNext 决定后续页。
---@param keyword string
---@param cb fun(books: Book[]|nil, err: string|nil, more: boolean|nil)
---@param page integer|nil
---@return HttpJob
function Client.searchAsync(keyword, cb, page)
return call("GET", "/booksearch/query", { keyWord = keyword, pageNo = page or 1, pageSize = 10 },
nil, nil, function(wire, err)
if not wire then cb(nil, err); return end
local data = wire.data
if type(data) ~= "table" or type(data.bookList) ~= "table" then
cb(nil, _("QQ阅读搜索响应无效")); return
end
local books = {}
for _idx, row in ipairs(data.bookList) do
if type(row) ~= "table" or not row.bid or type(row.title) ~= "string" then
cb(nil, _("QQ阅读搜索响应无效")); return
end
books[#books + 1] = Mapper.book(row)
end
cb(books, nil, data.hasNext == true or tonumber(data.hasNext) == 1)
end)
end
--- 官网榜单 / 分类 SSR 书卡 → Book[]。
---@param path string
---@param cb fun(books: Book[]|nil, err: string|nil)
---@return HttpJob
function Client.listAsync(path, cb)
return Request.get(WEB .. path, {
accept = "text/html",
headers = headers(nil, WEB .. path),
timeout = require("http.timeout").net(1),
}, function(raw, err)
if not raw then cb(nil, err and tostring(err) or _("请求失败")); return end
if not raw:find('class="book-rank main"', 1, true)
and not raw:find('class="book-cate main"', 1, true) then
cb(nil, _("QQ阅读列表响应无效")); return
end
local books = Mapper.searchHtml(raw)
if #books == 0 and raw:find("/book-detail/%d+") then cb(nil, _("QQ阅读列表响应无效")); return end
cb(books)
end)
end
---@param filters string 官网分类路径的八段条件
---@param cb fun(data: table|nil, err: string|nil)
---@return HttpJob
function Client.categoryAsync(filters, cb)
return call("GET", "/category/" .. filters, nil, nil, nil, function(wire, err)
if not wire then cb(nil, err); return end
local data = wire.data
if type(data) ~= "table" or type(data.books) ~= "table" or type(data.categoryList) ~= "table"
or type(data.params) ~= "table" or not tonumber(data.total) then
cb(nil, _("QQ阅读分类响应无效")); return
end
for _idx, key in ipairs({ "categoryId2", "categoryId3", "finishStatus", "priceStatus", "words", "updateTime", "orderBy", "pageNo" }) do
if not tonumber(data.params[key]) then cb(nil, _("QQ阅读分类响应无效")); return end
end
for _idx, category in ipairs(data.categoryList) do
if type(category) ~= "table" or type(category.name) ~= "string" or type(category.subCategories) ~= "table" then
cb(nil, _("QQ阅读分类响应无效")); return
end
end
for _idx, row in ipairs(data.books) do
if type(row) ~= "table" or not row.bid or type(row.title) ~= "string" then
cb(nil, _("QQ阅读分类响应无效")); return
end
end
cb(data)
end, WEB .. "/book-cate/" .. filters)
end
---@param book_id string
---@param cb fun(data: table|nil, err: string|nil)
---@param cursor string|nil
---@return HttpJob
function Client.bookCommentsAsync(book_id, cb, cursor)
return call("GET", "/bookcomment/list", { bid = book_id, cursor = cursor }, nil, nil, function(wire, err)
if not wire then cb(nil, err); return end
local data = wire.data
if type(data) ~= "table" or type(data.comments) ~= "table" or not tonumber(data.commentCount) then
cb(nil, _("QQ阅读书评响应无效")); return
end
for _idx, row in ipairs(data.comments) do
if type(row) ~= "table" or type(row.content) ~= "string" or type(row.user) ~= "table"
or (row.createTime ~= nil and not tonumber(row.createTime)) then
cb(nil, _("QQ阅读书评响应无效")); return
end
end
cb(data)
end)
end
---@param book_id string
---@param cb fun(info: table|nil, err: string|nil)
---@return HttpJob
function Client.detailAsync(book_id, cb)
return call("GET", "/book/detail", { bid = book_id }, nil, Client.cookie(), function(wire, err)
if not wire then cb(nil, err); return end
local info = wire.data
if type(info) ~= "table" or not info.title then cb(nil, _("QQ阅读书籍信息为空")); return end
cb(info)
end)
end
---@param book_id string
---@param cb fun(rows: table|nil, err: string|nil)
---@return HttpJob
function Client.catalogAsync(book_id, cb)
return call("GET", "/book/detail/chapters", { bid = book_id }, nil, Client.cookie(), function(wire, err)
if not wire then cb(nil, err); return end
if type(wire.data) ~= "table" then cb(nil, _("QQ阅读目录为空")); return end
cb(wire.data)
end)
end
---@param book_id string
---@param chapter_id string
---@param fuid string|nil 加密章要带 piracy 给的 fuid,密文跟它绑定
---@param cb fun(info: table|nil, err: string|nil)
---@return HttpJob
local function readAsync(book_id, chapter_id, fuid, cookie, cb)
return call("GET", "/book/read", { bid = book_id, cid = chapter_id, fuid = fuid }, nil, cookie,
function(wire, err)
if not wire then cb(nil, err); return end
if type(wire.data) ~= "table" then cb(nil, _("QQ阅读章节内容为空")); return end
cb(wire.data)
end)
end
--- 一章正文。未授权的付费章只有预览;已购章是 Fock 加密 + 乱序字体,解出后还原成纯文本。
--- 密钥池与 fuid 在 user/index/piracy(网页的 fkp,`window.onkeyfocus("<密钥池>", <版本>)` 的 base64,只取参数不执行)。
---@param book_id string
---@param chapter_id string
---@param cb fun(text: string|nil, err: string|nil, kind: "unpaid"|nil) kind = "unpaid":这章没买
---@return table handle { cancel }
function Client.contentAsync(book_id, chapter_id, cb)
local current, cancelled, cookie = nil, false, Client.cookie()
local function request(start, done)
local pending = true
local job = start(function(...)
pending = false
if cancelled then return end
current = nil
done(...)
end)
if pending then current = job end
end
local function finish(text)
if type(text) ~= "string" or Text.trim(text) == "" then cb(nil, _("QQ阅读章节内容为空")); return end
cb(text)
end
local function decrypt(info, pirate)
local keypool, version = Text.base64Decode(tostring(pirate.fkp or ""))
:match('onkeyfocus%("([^"]+)",%s*(%d+)%)')
local font = type(info.fontResponse) == "table" and info.fontResponse or {}
if not (keypool and version and type(info.content) == "string" and pirate.fuid) then
logger.warn("qqreader decrypt inputs missing: keypool", keypool ~= nil, "version", version ~= nil,
"content", type(info.content), "fuid", pirate.fuid ~= nil)
cb(nil, _("QQ阅读章节解密失败")); return
end
local function run(fixed_font)
request(function(done)
return Fock.chapterAsync(info, fixed_font, chapter_id, keypool, version, pirate.fuid, done)
end,
function(text, derr)
if not text then
logger.warn("qqreader fock decrypt failed:", derr)
cb(nil, _("QQ阅读章节解密失败")); return
end
finish(text)
end)
end
if not Mapper.fontEncrypted(info) then run(nil); return end
if type(font.fixedFontTtf) ~= "string" then
logger.warn("qqreader decrypt: fixedFontTtf missing")
cb(nil, _("QQ阅读章节解密失败")); return
end
request(function(done)
return Request.get(font.fixedFontTtf, { timeout = require("http.timeout").net(1) }, done)
end, function(ttf, ferr)
if not ttf or #ttf < 4 then
logger.warn("qqreader font download failed:", ferr, ttf and #ttf)
cb(nil, _("QQ阅读章节解密失败")); return
end
run(ttf)
end)
end
request(function(done) return readAsync(book_id, chapter_id, nil, cookie, done) end, function(info, err)
if not info then cb(nil, err); return end
if Mapper.locked(info) then cb(nil, _("QQ阅读 VIP 章节未购买"), "unpaid"); return end
if not Mapper.encrypted(info) then finish(info.content); return end
request(function(done) return call("GET", "/user/index/piracy", nil, nil, cookie, done) end, function(wire, perr)
if not wire then cb(nil, perr); return end
local pirate = type(wire.data) == "table" and wire.data or {}
if type(pirate.fuid) ~= "string" and type(pirate.fuid) ~= "number" then
cb(nil, _("QQ阅读章节解密失败")); return
end
pirate.fuid = type(pirate.fuid) == "number" and string.format("%.0f", pirate.fuid) or pirate.fuid
if pirate.fuid == "" then cb(nil, _("QQ阅读章节解密失败")); return end
request(function(done) return readAsync(book_id, chapter_id, pirate.fuid, cookie, done) end, function(enc, rerr)
if not enc then cb(nil, rerr); return end
if Mapper.locked(enc) then cb(nil, _("QQ阅读 VIP 章节未购买"), "unpaid"); return end
if not Mapper.encrypted(enc) then finish(enc.content); return end
decrypt(enc, pirate)
end)
end)
end)
return { cancel = function()
cancelled = true
if current then current:cancel() end
end }
end
return Client