222 lines
8.0 KiB
Lua
222 lines
8.0 KiB
Lua
--[[--
|
|
识典古籍 HTTP。只请求官网直接返回的 loader JSON。
|
|
|
|
/api/ancientlib 要 a_bogus,不签、不绕。书库和搜索只有首屏;
|
|
打开一本书时用公开页上能找到的章节号,换到带全书目录的章节页。
|
|
|
|
@module moonsource_shidian.client
|
|
--]]
|
|
|
|
local Mapper = require("moonsource_shidian.mapper")
|
|
local Request = require("http.request")
|
|
local _ = require("l10n").get("moonsource_shidian")
|
|
|
|
local BASE = "https://www.shidianguji.com"
|
|
local UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
local HEADERS = {
|
|
["User-Agent"] = UA,
|
|
Accept = "application/json",
|
|
Referer = BASE .. "/",
|
|
}
|
|
local LIBRARY_QUERY = "__loader=__session%2F%28lang%24%29%2Flibrary.%28cateId%24%29%2Fpage&__ssrDirect=true"
|
|
local SEARCH_QUERY = "__loader=__session%2F%28lang%24%29%2Fsearch.%28query%24%29%2Fpage&__ssrDirect=true"
|
|
local BOOK_QUERY = "__loader=__session%2F%28lang%24%29%2Fbook%2F%24&__ssrDirect=true"
|
|
|
|
---@class ShidianClient
|
|
---@field page table|nil
|
|
local Client = {}
|
|
Client.__index = Client
|
|
Client.UA = UA
|
|
|
|
---@return ShidianClient
|
|
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 id string|nil
|
|
---@return boolean
|
|
local function idOk(id)
|
|
return type(id) == "string" and id:match("^[%w_]+$") ~= nil
|
|
end
|
|
|
|
---@param url string
|
|
---@param cb fun(data: table|nil, err: string|nil)
|
|
---@return { cancel: fun() }|nil
|
|
function Client:getJson(url, cb)
|
|
local pending = true
|
|
local inner = Request.get(url, {
|
|
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" then cb(nil, _("响应无效")); return end
|
|
cb(data)
|
|
end)
|
|
if pending then return inner end
|
|
end
|
|
|
|
---@param data table
|
|
---@return boolean
|
|
local function spider(data)
|
|
return data.isSpider == true and type(data.bookInfo) ~= "table"
|
|
end
|
|
|
|
---@param cate string
|
|
---@param cb fun(data: table|nil, err: string|nil)
|
|
---@return { cancel: fun() }|nil
|
|
function Client:libraryAsync(cate, cb)
|
|
if not idOk(cate) then return require("source.base").defer(cb, nil, _("响应无效")) end
|
|
return self:getJson(BASE .. "/library/" .. cate .. "?" .. LIBRARY_QUERY, cb)
|
|
end
|
|
|
|
---@param keyword string
|
|
---@param cb fun(data: table|nil, err: string|nil)
|
|
---@return { cancel: fun() }|nil
|
|
function Client:searchAsync(keyword, cb)
|
|
if keyword == "" then return require("source.base").defer(cb, nil, _("响应无效")) end
|
|
return self:getJson(BASE .. "/search/" .. encode(keyword) .. "?" .. SEARCH_QUERY, cb)
|
|
end
|
|
|
|
--- 章节页。没有这本书时 cb(nil);网络、坏 JSON、被当成爬虫时带错误。
|
|
---@param book_id string
|
|
---@param chapter_id string
|
|
---@param cb fun(page: table|nil, err: string|nil)
|
|
---@return { cancel: fun() }|nil
|
|
function Client:chapterAsync(book_id, chapter_id, cb)
|
|
if not idOk(book_id) or not idOk(chapter_id) then
|
|
return require("source.base").defer(cb, nil, _("响应无效"))
|
|
end
|
|
local url = BASE .. "/book/" .. book_id .. "/chapter/" .. chapter_id .. "?" .. BOOK_QUERY
|
|
return self:getJson(url, function(data, err)
|
|
if not data then cb(nil, err); return end
|
|
if spider(data) then cb(nil, _("识典古籍把这次请求当成了爬虫,没有返回正文")); return end
|
|
local info = data.bookInfo
|
|
if type(info) ~= "table" or info.bookId ~= book_id then cb(nil); return end
|
|
self.page = data
|
|
cb(data)
|
|
end)
|
|
end
|
|
|
|
---@param book_id string
|
|
---@param chapter_id string
|
|
---@param cb fun(page: table|nil, err: string|nil)
|
|
---@return { cancel: fun() }|nil
|
|
function Client:contentAsync(book_id, chapter_id, cb)
|
|
local page = self.page
|
|
if type(page) == "table" and page.ssrChapterId == chapter_id
|
|
and type(page.bookInfo) == "table" and page.bookInfo.bookId == book_id then
|
|
return require("source.base").defer(cb, page)
|
|
end
|
|
return self:chapterAsync(book_id, chapter_id, cb)
|
|
end
|
|
|
|
--- 用公开页能找到的任意一章换出带目录的章节页。
|
|
--- 顺序:书目上的章节号、书号_1、该书分类首屏、书名搜索。都没有就失败,不猜签名接口。
|
|
---@param hint { id: string, title: string|nil, seed: string|nil, cate: string|nil }
|
|
---@param cb fun(page: table|nil, err: string|nil)
|
|
---@return { cancel: fun() }
|
|
function Client:resolveAsync(hint, cb)
|
|
local handle = {}
|
|
local function stopped() return handle.cancelled end
|
|
local function fail(err)
|
|
if not stopped() then cb(nil, err or _("这本书的公开页面没有给出可打开的章节")) end
|
|
end
|
|
local function ok(page)
|
|
if not stopped() then cb(page) end
|
|
end
|
|
-- 回调可能当场发生。当场发生时不要把已经结束的请求写回 inner,盖掉下一步。
|
|
local function spawn(make)
|
|
local done = false
|
|
local inner = make(function(next_cb)
|
|
return function(...)
|
|
done = true
|
|
return next_cb(...)
|
|
end
|
|
end)
|
|
if not done then handle.inner = inner end
|
|
end
|
|
|
|
local function trySearch()
|
|
if type(hint.title) ~= "string" or hint.title == "" then
|
|
return fail()
|
|
end
|
|
spawn(function(wrap)
|
|
return self:searchAsync(hint.title, wrap(function(data, err)
|
|
if stopped() then return end
|
|
if not data then return fail(err) end
|
|
local seed = Mapper.searchSeed(data, hint.id)
|
|
if not seed then return fail() end
|
|
spawn(function(wrap_chapter)
|
|
return self:chapterAsync(hint.id, seed, wrap_chapter(function(page, chapter_err)
|
|
if stopped() then return end
|
|
if page then return ok(page) end
|
|
fail(chapter_err)
|
|
end))
|
|
end)
|
|
end))
|
|
end)
|
|
end
|
|
|
|
local function tryCate()
|
|
if not idOk(hint.cate) or hint.cate == "all" then return trySearch() end
|
|
spawn(function(wrap)
|
|
return self:libraryAsync(hint.cate, wrap(function(data, err)
|
|
if stopped() then return end
|
|
if not data then return fail(err) end
|
|
local seed = Mapper.librarySeed(data, hint.id)
|
|
if not seed then return trySearch() end
|
|
spawn(function(wrap_chapter)
|
|
return self:chapterAsync(hint.id, seed, wrap_chapter(function(page, chapter_err)
|
|
if stopped() then return end
|
|
if page then return ok(page) end
|
|
if chapter_err then return fail(chapter_err) end
|
|
trySearch()
|
|
end))
|
|
end)
|
|
end))
|
|
end)
|
|
end
|
|
|
|
local function tryPlain()
|
|
spawn(function(wrap)
|
|
return self:chapterAsync(hint.id, hint.id .. "_1", wrap(function(page, err)
|
|
if stopped() then return end
|
|
if page then return ok(page) end
|
|
if err then return fail(err) end
|
|
tryCate()
|
|
end))
|
|
end)
|
|
end
|
|
|
|
if not idOk(hint.id) then return require("source.base").defer(cb, nil, _("响应无效")) end
|
|
if idOk(hint.seed) then
|
|
spawn(function(wrap)
|
|
return self:chapterAsync(hint.id, hint.seed, wrap(function(page, err)
|
|
if stopped() then return end
|
|
if page then return ok(page) end
|
|
if err then return fail(err) end
|
|
tryPlain()
|
|
end))
|
|
end)
|
|
else
|
|
tryPlain()
|
|
end
|
|
function handle.cancel()
|
|
handle.cancelled = true
|
|
if handle.inner then handle.inner.cancel() end
|
|
end
|
|
return handle
|
|
end
|
|
|
|
return Client
|