--[[-- 潇湘书院数据源。 分类、搜索、账号、正文、书架、进度走新站 JSON;详情、目录、旧榜单走电脑/移动官网 SSR(client.lua)。 登录态是新站 Cookie(阅文通行证):App 扫码、浏览器扩展授权,或粘贴 Cookie(setting.lua)。 书架双向同步(Shelf.syncAsync),进度按章:读书架行的 ccid,写 readingProcess/add。没买的 VIP 章开章给临时提示页,目录标「未购」。 @module moonsource_xiaoxiang --]] local Client = require("moonsource_xiaoxiang.client") local Mapper = require("moonsource_xiaoxiang.mapper") local SourceBase = require("source.base") local Browse = require("source.browse") local Shelf = require("source.shelf") local Toc = require("source.toc") local _ = require("l10n").get("moonsource_xiaoxiang") local Xiaoxiang = {} -- 接口按 IP 限流,预取与整本缓存逐章间隔请求。 local FETCH_INTERVAL = 1 ---@return BookSourceMeta function Xiaoxiang.meta() return require("moonsource_xiaoxiang.loadmeta")() end ---@class XiaoxiangSource : SourceBase local Source = setmetatable({ shelf_add = true }, { __index = SourceBase }) Source.__index = Source ---@return XiaoxiangSource function Xiaoxiang.new() local meta = Xiaoxiang.meta() return setmetatable({ id = meta.id, name = meta.name, type = meta.type }, Source) end --- 搜索 / 目录 / 榜单匿名可用,书架、进度和已购 VIP 章要登录 Cookie。 ---@return boolean function Source:configured() return Client.cookie() ~= nil end --- 书架远端:Shelf.syncAsync 要的三个冒号方法。 local Remote = {} function Remote:shelfSyncAsync(cb) return Client.shelfAsync(cb) end function Remote:addToShelfAsync(stable_id, cb) return Client.shelfAddAsync(stable_id, cb) end function Remote:removeFromShelfAsync(stable_id, cb) return Client.shelfRemoveAsync(stable_id, cb) end function Source:syncBooksAsync(opts, cb) if not self:configured() then return SourceBase.skipped(cb, "xiaoxiang not logged in") end return Shelf.syncAsync(self.id, Remote, Mapper.shelfList, opts, cb) end --- 进度只到章:读书架行里这本书最近读到的章(ccid)。 function Source:getProgressAsync(identity, cb) if not self:configured() then return SourceBase.defer(cb, nil, _("未登录潇湘")) end local cookie = Client.cookie() local cancelled, job = false, nil local pending = true local initial = Client.shelfAsync(function(rows, err) pending = false if cancelled then return end if Client.cookie() ~= cookie then cb(nil, _("账号已变化,请重新登录")); return end if not rows then cb(nil, err); return end local uid = Mapper.progress(rows)[tostring(identity.stable_id)] if not uid then cb(nil, nil, { empty = true }); return end job = self:loadTocAsync(identity, function(toc, reason) if cancelled then return end if Client.cookie() ~= cookie then cb(nil, _("账号已变化,请重新登录")); return end if not toc or #toc == 0 then cb(nil, reason or _("潇湘目录为空")); return end local idx = Toc.index(self.id, identity.stable_id, uid) if not idx then cb(nil, nil, { empty = true }); return end cb({ chapter_idx = idx, chapter_title = toc[idx].title, fraction = (idx - 1) / #toc, extra = { chapter_uid = uid, chapter_idx = idx }, }) end) end) if pending then job = initial end return { cancel = function() cancelled = true if job and job.cancel then job.cancel() end end } end function Source:putProgressAsync(identity, pos, cb) if not self:configured() then return SourceBase.defer(cb, nil, _("未登录潇湘")) end local cookie = Client.cookie() local cancelled, job = false, nil local pending = true local initial = self:loadTocAsync(identity, function(toc, err) pending = false if cancelled then return end if Client.cookie() ~= cookie then cb(nil, _("账号已变化,请重新登录")); return end if not toc then cb(nil, err); return end local idx = tonumber(pos.chapter_idx) or tonumber(identity.chapter_idx) local chapter = idx and toc[idx] if not chapter then cb(nil, _("缺少潇湘章节位置")); return end job = Client.progressAsync(identity.stable_id, chapter.uid, function(wire, perr) if not cancelled then cb(wire and true, perr) end end) end) if pending then job = initial end return { cancel = function() cancelled = true if job and job.cancel then job.cancel() end end } end ---@param keyword string ---@param cb fun(res: { data: Book[] }|nil, err: string|nil) ---@return { cancel: fun() }|nil function Source:searchAsync(keyword, cb) return Client.searchAsync(keyword, function(books, err) if not books then cb(nil, err) return end cb({ data = books }) end) end -- 手机站 /rank/ 的榜单,顺序同网页。 local RANKS = { { id = "xxyuepiao", title = "潇湘票榜" }, { id = "bestsales", title = "畅销榜" }, { id = "finish", title = "完结榜" }, { id = "reading", title = "阅读榜" }, { id = "collections", title = "收藏榜" }, } ---@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) opts = opts or {} local legacy = opts.node == "rank" for _idx, row in ipairs(RANKS) do if row.id == opts.node then legacy = true end end if not legacy then return require("moonsource_xiaoxiang.browse").run(opts, cb) end local rank = RANKS[1] for _idx, row in ipairs(RANKS) do if row.id == opts.node then rank = row end end local chips = {} for _idx, row in ipairs(RANKS) do chips[#chips + 1] = { id = row.id, title = _(row.title) } end local groups = { Browse.group(chips, rank.id) } groups[1].title = _("排行榜") groups[1].chips[#groups[1].chips + 1] = { id = "cate:-1:-1:-1:-1:10:0:-1", title = _("分类库") } return Client.rankAsync(rank.id, function(books, err) if not books then cb(nil, err); return end if opts.limit then for index = #books, opts.limit + 1, -1 do books[index] = nil end end cb({ data = books, groups = groups }) end, rank.title) end function Source:getDetailAsync(identity, cb) return Client.detailAsync(identity.stable_id, cb) end --- 强制拉目录并写回;失败由调用方继续用旧缓存。 ---@param identity BookIdentity ---@param cb fun(toc: BookChapter[]|nil, err: string|nil) ---@return { cancel: fun() }|nil local function fetchTocAsync(self, identity, cb) return Client.chaptersAsync(identity.stable_id, function(toc, err) if not toc then cb(nil, err); return end if #toc == 0 then cb(nil, _("潇湘目录为空")); return end if not Toc.put(self.id, identity.stable_id, toc) then cb(nil, _("章节目录保存失败")); return end cb(toc) 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 and cached[1].toc_version == Mapper.TOC_VERSION then return SourceBase.defer(cb, cached) end end return fetchTocAsync(self, identity, cb) end Source.refreshTocAsync = fetchTocAsync 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 = Chapter.vipFetcher(self, Client.contentAsync, true), }, cb) end function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb) local Chapter = require("source.chapter") return Chapter.prefetchAsync(identity, identity.book, toc, from_idx, count, { fetchContent = Chapter.vipFetcher(self, Client.contentAsync, false), interval_seconds = FETCH_INTERVAL, }, cb) end function Source:cacheAllChaptersAsync(identity, on_progress, cb, range) local Chapter = require("source.chapter") return Chapter.cacheAllAsync(self, identity, Chapter.vipFetcher(self, Client.contentAsync, false), on_progress, cb, FETCH_INTERVAL, range) end return Xiaoxiang