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

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

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

No files matched your search

@@ -0,0 +1,508 @@
--[[--
掌阅网页接口。
登录、书架、目录、进度、正文走 PC 站(api-pc.zhangyue.com,正文在 pc.ireader.com/nsr)。
设备参数 p29/p2/p3/p4/p33 来自 PC 站运行时配置,每次请求都带,和网页 axios 拦截器一致。
书城搜索与频道是手机站 m.zhangyue.com 的页面,不走这套 JSON。
插件保存扫码回包里的 usr(账号名)和 zysid(session_id),不依赖浏览器 Cookie。
@module moonsource_ireader.client
--]]
local JSON = require("json")
local Mapper = require("moonsource_ireader.mapper")
local Request = require("http.request")
local Text = require("utils.text")
local _ = require("l10n").get("moonsource_ireader")
local Client = {}
local SOURCE_ID = "ext.ireader"
local API = "https://api-pc.zhangyue.com"
local WEB = "https://pc.ireader.com"
local STORE = "https://m.zhangyue.com"
local SUCCESS = 0
local LOGIN_EXPIRED = 600001
local HAS_IN_SHELF = 100011
local FEE_WHOLE = 10
local DEVICE = {
p29 = "zye5b814",
p2 = "104583",
p3 = "101010010",
p4 = "501603",
p33 = "com.chaozh.iReaderFree",
}
---@type table<string, number>
local fee_unit = {}
---@return { usr: string, zysid: string, name: string|nil, avatar: string|nil }|nil
function Client.session()
local cfg = require("utils.settings").getSource(SOURCE_ID)
local usr = type(cfg.usr) == "string" and cfg.usr or ""
local zysid = type(cfg.zysid) == "string" and cfg.zysid or ""
if usr == "" or zysid == "" then return nil end
return { usr = usr, zysid = zysid, name = cfg.name, avatar = cfg.avatar }
end
---@param session table|nil
---@param params table|nil
---@return string
local function query(session, params)
local form = {
usr = session and session.usr or "",
zysid = session and session.zysid or "",
}
for key, value in pairs(DEVICE) do form[key] = value end
for key, value in pairs(params or {}) do form[key] = value end
return Text.formEncode(form)
end
---@param path string
---@param session table|nil
---@param params table|nil
---@return string
local function url(path, session, params)
local base = path
if path:sub(1, 5) == "/nsr/" then
base = WEB .. path
elseif not path:find("^https?://") then
base = API .. path
end
return base .. "?" .. query(session, params)
end
--- 解 PC 站统一封套。code 0 的 body 可能再套一层 JSON 字符串。
---@param wire table
---@param session table|nil
---@return any|nil
---@return string|nil
local function bodyOf(wire, session)
local current = Client.session()
local active = session and current and current.usr == session.usr and current.zysid == session.zysid
local code = tonumber(wire.code)
if code == SUCCESS or (code == nil and type(wire.bookContent) == "string") then
if type(wire.bookContent) == "string" then return wire end
local body = wire.body
if type(body) == "string" then
local ok, inner = pcall(JSON.decode, body)
if ok and inner ~= nil then body = inner end
end
if active then require("source.auth").ok(SOURCE_ID) end
if body == nil then return true end
return body
end
if code == LOGIN_EXPIRED then
local message = _("掌阅登录已失效,请点「重新登录」,用掌阅 App 扫码。")
return nil, active and require("source.auth").expire(SOURCE_ID, message) or message
end
if code == 100001 and not session then return nil, _("未登录掌阅") end
return nil, type(wire.msg) == "string" and wire.msg ~= "" and wire.msg or _("请求失败")
end
---@param raw string|nil
---@param err any
---@return table|nil
---@return string|nil
local function envelope(raw, err)
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
return wire
end
---@param path string
---@param params table|nil
---@param session table|nil
---@param cb fun(body: any, err: string|nil)
---@return HttpJob
local function get(path, params, session, cb)
return Request.get(url(path, session, params), {
accept = "application/json",
cache_ttl = 0,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
local wire, reason = envelope(raw, err)
if not wire then cb(nil, reason); return end
local body, message = bodyOf(wire, session)
cb(body, message)
end)
end
function Client.get(path, params, cb)
return get(path, params, Client.session(), cb)
end
---@param path string
---@param params table|nil
---@param payload string
---@param content_type string
---@param cb fun(body: any, err: string|nil)
---@return HttpJob
local function post(path, params, payload, content_type, cb)
local session = Client.session()
return Request.post(url(path, session, params), payload, {
accept = "application/json",
content_type = content_type,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
local wire, reason = envelope(raw, err)
if not wire then cb(nil, reason); return end
local body, message = bodyOf(wire, session)
cb(body, message)
end)
end
---@param fields { name: string, value: string }[]
---@return string body
---@return string content_type
local function multipart(fields)
local boundary = "----moonireader"
local parts = {}
for _idx, field in ipairs(fields) do
parts[#parts + 1] = "--" .. boundary
.. '\r\nContent-Disposition: form-data; name="' .. field.name .. '"\r\n\r\n' .. field.value
end
parts[#parts + 1] = "--" .. boundary .. "--\r\n"
return table.concat(parts, "\r\n"), "multipart/form-data; boundary=" .. boundary
end
---@param cb fun(code: string|nil, err: string|nil)
---@return HttpJob
function Client.qrCreateAsync(cb)
return get("/bookstore/open/user/qrcode/get", nil, nil, function(body, err)
local code = Mapper.qrCode(body)
if not code then cb(nil, err or _("获取二维码失败")); return end
cb(code)
end)
end
--- 轮询不把「还没扫」当成失败。回包原样交给调用方。
---@param code string
---@param cb fun(wire: table|nil, err: string|nil)
---@return HttpJob
function Client.qrPollAsync(code, cb)
return Request.get(url("/bookstore/open/user/qrcode/result", nil, { code = code }), {
accept = "application/json",
cache_ttl = 0,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
local wire, reason = envelope(raw, err)
cb(wire, reason)
end)
end
---@param cb fun(body: table|nil, err: string|nil)
---@return HttpJob
function Client.shelfAsync(cb)
return Client.get("/bookstore/open/cloud/bookshelf/list", nil, function(body, err)
if type(body) ~= "table" or type(body.data) ~= "table" then cb(nil, err or _("响应无效")); return end
cb(body)
end)
end
---@param session table
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return HttpJob
function Client.checkSessionAsync(session, cb)
return get("/bookstore/open/cloud/bookshelf/list", nil, session, function(body, err)
if type(body) ~= "table" or type(body.data) ~= "table" then cb(nil, err or _("响应无效")); return end
cb(true)
end)
end
--- 新书插到未置顶书的最前:order 取当前最小再减一,和网页 insert 一致。
---@param rows table[]|nil
---@return integer order
---@return integer version
local function shelfCursor(rows, version)
local order = nil
for _idx, row in ipairs(type(rows) == "table" and rows or {}) do
local value = tonumber(row.order)
if row.top ~= 1 and value and (not order or value < order) then order = value end
end
return (order or 999999) - 1, tonumber(version) or 0
end
Client.shelfCursor = shelfCursor
---@param stable_id string
---@param title string
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return { cancel: fun() }
function Client.addAsync(stable_id, title, cb)
local session = Client.session()
if not session then return require("source.base").defer(cb, nil, _("未登录掌阅")) end
local handle, pending = {}, true
local initial = Client.shelfAsync(function(shelf, err)
pending = false
if handle.cancelled then return end
if not shelf then cb(nil, err); return end
local current = Client.session()
if not current or current.usr ~= session.usr or current.zysid ~= session.zysid then
cb(nil, _("登录状态已变化,请重试"))
return
end
local order, version = shelfCursor(shelf.data, shelf.current_version)
local data = JSON.encode({ {
dtype = 0,
order = order,
folder = "",
md5 = "",
top = 0,
top_sort = 0,
book_id = tostring(stable_id),
book_name = title,
book_type = 24,
cover_name = "",
is_local_book = false,
book_file_id = "",
book_file_size = 0,
cover_file_id = "",
cover_file_size = 0,
folder_hierarchy = "[]",
} })
local payload, content_type = multipart({
{ name = "usr", value = session.usr },
{ name = "current_version", value = tostring(version) },
{ name = "type", value = "1" },
{ name = "data", value = data },
})
handle.job = Request.post(url("/bookstore/open/cloud/bookshelf/upsert", session), payload, {
accept = "application/json",
content_type = content_type,
timeout = require("http.timeout").net(0.5),
}, function(raw, post_err)
if handle.cancelled then return end
local wire, reason = envelope(raw, post_err)
if not wire then cb(nil, reason); return end
local code = tonumber(wire.code)
if code == SUCCESS or code == HAS_IN_SHELF then
local active = Client.session()
if active and active.usr == session.usr and active.zysid == session.zysid then
require("source.auth").ok(SOURCE_ID)
end
cb(true)
return
end
local _ignored, message = bodyOf(wire, session)
cb(nil, message)
end)
end)
if pending then handle.job = initial end
return { cancel = function()
handle.cancelled = true
if handle.job and handle.job.cancel then handle.job.cancel() end
end }
end
---@param book_id string
---@param cb fun(body: table|nil, err: string|nil)
---@return HttpJob
function Client.detailAsync(book_id, cb)
return Client.get("/bookstore/open/book/detail", { book_id = book_id }, function(body, err)
if type(body) == "table" and body.fee_unit ~= nil then
fee_unit[tostring(book_id)] = tonumber(body.fee_unit)
end
cb(body, err)
end)
end
---@param book_id string
---@param cb fun(rows: table[]|nil, err: string|nil)
---@return { cancel: fun() }
function Client.chaptersAsync(book_id, cb)
local handle, rows = {}, {}
local function nextPage(page)
local pending = true
local initial = Client.get("/bookstore/open/book/chapter/web/list", {
book_id = book_id, page = page, size = 50,
}, function(body, err)
pending = false
if handle.cancelled then return end
if type(body) ~= "table" or type(body.list) ~= "table" then cb(nil, err or _("响应无效")); return end
local list = body.list
local total = type(body.page) == "table" and tonumber(body.page.totalPage)
if not total or total < page or total % 1 ~= 0 or (#list == 0 and page < total) then
cb(nil, _("响应无效"))
return
end
for _idx, row in ipairs(list) do
if type(row) ~= "table" or (row.id == nil and row.chapter_id == nil) then
cb(nil, _("响应无效"))
return
end
rows[#rows + 1] = row
end
if page < total and #list > 0 then
nextPage(page + 1)
return
end
cb(rows)
end)
if pending then handle.job = initial end
end
nextPage(1)
return { cancel = function()
handle.cancelled = true
if handle.job and handle.job.cancel then handle.job.cancel() end
end }
end
---@param book_id string
---@param chapter_id string
---@param cb fun(html: string|nil, err: string|nil, kind: "unpaid"|nil)
---@return HttpJob
function Client.contentAsync(book_id, chapter_id, cb)
local whole = fee_unit[tostring(book_id)] == FEE_WHOLE
return Client.get("/nsr/chapter-content", {
book_id = book_id,
chapter_id = chapter_id,
is_whole_book = whole and "true" or "false",
}, function(body, err)
if type(body) ~= "table" then cb(nil, err or _("响应无效")); return end
-- 免费章的 charging_info 是 JSON null,LuaJSON 解成函数,不能当成未购买。
-- 未购买才是计费表,而且 is_buy 不为真。已购章也带这张表,正文仍在 bookContent。
local info = body.charging_info
local bought = type(info) == "table" and (info.is_buy == true or tonumber(info.is_buy) == 1)
if type(info) == "table" and not bought then cb(nil, _("本章未购买"), "unpaid"); return end
if type(body.bookContent) ~= "string" or body.bookContent == "" then
cb(nil, _("响应无效"))
return
end
local html = Mapper.content(body.bookContent)
cb(html, not html and _("掌阅正文含未知字码,请更新插件后重试") or nil)
end)
end
---@param book_id string
---@param cb fun(item: table|nil, err: string|nil)
---@return HttpJob
function Client.progressAsync(book_id, cb)
return Client.get("/bookstore/open/cloud/book/getProgress", { bookIds = book_id }, function(body, err)
if type(body) ~= "table" then cb(nil, err or _("响应无效")); return end
local key = tostring(book_id)
cb(body[key] or body[tonumber(key)])
end)
end
---@param book_id string
---@param chapter_uid string
---@param total integer
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return HttpJob
function Client.putProgressAsync(book_id, chapter_uid, total, cb)
local session = Client.session()
if not session then return require("source.base").defer(cb, nil, _("未登录掌阅")) end
local stored = math.max(0, (tonumber(chapter_uid) or 1) - 1)
local now = os.time() * 1000
local payload = JSON.encode({
bookId = tostring(book_id),
deviceName = "掌阅网页",
updateTime = now,
usr = session.usr,
readProcess = {
bookOverStatus = 0,
totalCount = total,
lastReadDate = now,
readingSource = "web",
chapterId = tostring(stored),
},
})
return post("/bookstore/open/cloud/book/progress", nil, payload, "application/json", function(body, err)
cb(body and true or nil, err)
end)
end
local STORE_HEADERS = {
["User-Agent"] = "Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 Chrome/120.0.0.0 Mobile Safari/537.36",
}
---@param book_id string
---@param cb fun(result: IReaderReviewPage|nil, err: string|nil)
---@return HttpJob
function Client.reviewsAsync(book_id, cb)
local target = STORE .. "/comment/" .. Text.urlEncode(book_id)
local cancelled = false
local job = Request.get(target, {
accept = "text/html",
headers = STORE_HEADERS,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
if cancelled then return end
if err or not raw then cb(nil, err and tostring(err) or _("请求失败")); return end
local result = Mapper.reviews(raw)
if not result then cb(nil, _("响应无效")); return end
cb(result)
end)
return { cancel = function()
cancelled = true
if job and job.cancel then job.cancel() end
end }
end
---@param keyword string
---@param cb fun(html: string|nil, err: string|nil)
---@return HttpJob
function Client.searchAsync(keyword, cb)
return Request.get(STORE .. "/search?" .. Text.formEncode({ keyWord = keyword }), {
accept = "text/html",
headers = STORE_HEADERS,
timeout = require("http.timeout").net(0.5),
}, function(raw, err) cb(raw, err and tostring(err) or nil) end)
end
---@param channel_id string
---@param cb fun(html: string|nil, err: string|nil)
---@return HttpJob
function Client.channelAsync(channel_id, cb)
return Request.get(STORE .. "/channel/" .. channel_id, {
accept = "text/html",
headers = STORE_HEADERS,
timeout = require("http.timeout").net(0.5),
}, function(raw, err) cb(raw, err and tostring(err) or nil) end)
end
---@param parent string
---@param category string
---@param cb fun(page: table|nil, err: string|nil)
---@return HttpJob
function Client.categoriesAsync(parent, category, cb)
return Request.get(STORE .. "/category/" .. parent .. "/" .. category .. "?currentPage=1", {
accept = "text/html", headers = STORE_HEADERS,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
local page = raw and Mapper.categories(raw)
if not page then cb(nil, err and tostring(err) or _("响应无效")); return end
cb(page)
end)
end
---@param opts table
---@param page integer
---@param cb fun(books: Book[]|nil, err: string|nil, more: boolean|nil)
---@return HttpJob
function Client.catalogAsync(opts, page, cb)
return Request.get(STORE .. "/category/" .. opts.parent .. "/" .. opts.category .. "?" .. Text.formEncode({
isAjax = 1, currentPage = page, pageSize = 20, order = opts.order, status = opts.status,
}), {
accept = "application/json", headers = STORE_HEADERS,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
local wire, reason = envelope(raw, err)
if not wire then cb(nil, reason); return end
if tonumber(wire.code) ~= SUCCESS then cb(nil, wire.msg or _("请求失败")); return end
local body = wire.body
if type(wire.html) ~= "string" or type(body) ~= "table"
or tonumber(body.current_page) ~= page or not tonumber(body.page_total) then
cb(nil, _("响应无效"))
return
end
local books = Mapper.catalog(wire.html)
if #books == 0 and Text.trim(wire.html) ~= "" then cb(nil, _("响应无效")); return end
cb(books, nil, page < tonumber(body.page_total))
end)
end
return Client