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

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

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

No files matched your search

@@ -0,0 +1,558 @@
--[[--
番茄小说官方数据源。
正文走 App reading API:registerkey → batch_full → 解密(reading/)。书架双向(加架 / 移出架经番茄网页接口,Shelf.syncAsync 编排),搜索走免登录端点,进度双向,无笔记、统计。
封面用 fanqie.lua 的 mobileCover 稳定路径(网页签名 thumb_url 会过期)。19 位设备 id 按 uint64 精确编码;Cookie 没变不重写设置。
@module moonsource_fanqie
--]]
local Base = require("source.base")
local Browse = require("source.browse")
local Assets = require("source.assets")
local Cbz = require("source.cbz")
local Comic = require("moonsource_fanqie.comic")
local Paths = require("utils.paths")
local ReviewCache = require("book.review_cache")
local _ = require("l10n").get("moonsource_fanqie")
local T = require("ffi/util").template
local M = {}
---@class FanqieSource : SourceBase
---@field settings FanqieSettings
---@field client FanqieClient
-- 书架双向(shelfActionAsync add/delete);换账号要重推本地成员。
local Source = setmetatable({ shelf_add = true }, { __index = Base })
Source.__index = Source
function M.meta()
return require("moonsource_fanqie.loadmeta")()
end
function M.new()
local Settings = require("moonsource_fanqie.settings")
local Client = require("moonsource_fanqie.client")
local settings = Settings:new()
local meta = M.meta()
return setmetatable({
id = meta.id,
name = meta.name,
type = meta.type,
settings = settings,
client = Client:new(settings),
}, Source)
end
function Source:configured()
return self.settings:is_cookie_configured()
end
---@class FanqieAsyncHandle : CancelHandle
---@field job CancelHandle|nil
---@field cancelled boolean
---@return FanqieAsyncHandle
local function handle()
---@type FanqieAsyncHandle
local h
h = {
job = nil,
cancelled = false,
cancel = function()
h.cancelled = true
if h.job and h.job.cancel then
h.job.cancel()
end
end,
}
return h
end
---@return string
local function errMsg(err)
if type(err) == "table" then
return tostring(err.message or err.err or _("番茄请求失败"))
end
return tostring(err or _("番茄请求失败"))
end
--- 秒;毫秒值折成秒。无效为 nil。
---@param value any
---@return integer|nil
local function shelfTime(value)
local n = tonumber(value) or 0
if n > 1e11 then n = n / 1000 end
if n > 0 then return math.floor(n) end
end
--- 分类口径见 source.category_setting:开 = 只用自定义分组,关 = 不写分类(番茄书架不带题材)。
--- 书架行带最后打开时间(last_operate_time)和加入时间(add_shelf_time)。
local function shelfBook(row)
local book = {
source_id = "ext.fanqie",
stable_id = tostring(row.book_id),
title = row.book_name,
authors = row.author_name,
intro = require("utils.text").stripHtml(row.abstract),
cover = row.thumb_url,
read_at = shelfTime(row.last_operate_time),
inserted_at = shelfTime(row.add_shelf_time),
}
book.category = require("source.category_setting").apply("ext.fanqie", nil, row._group_name)
return book
end
---@param wire table fetchShelfDetailAsync 的回包
---@return Book[]
local function shelfBooks(wire)
local books = {}
for _idx, row in ipairs(wire.data.detail_list) do
local book = shelfBook(row)
-- 书架快照就是分类口径:没进组要清掉旧分类。详情更新不带这个标记。
book.category_authoritative = true
books[#books + 1] = book
end
return books
end
--- 书架远端:Shelf.syncAsync 要的三个冒号方法。
---@param self FanqieSource
---@param force boolean|nil
local function shelfRemote(self, force)
local remote = {}
function remote.shelfSyncAsync(_remote, cb)
return self.client:fetchShelfDetailAsync(force, function(wire, err)
if not wire then
cb(nil, errMsg(err))
elseif type(wire.data) ~= "table" or type(wire.data.detail_list) ~= "table" then
cb(nil, _("番茄书架响应不完整,保留本地书架"))
else
require("utils.paths").ensureLayout("ext.fanqie")
cb(wire)
end
end)
end
function remote.addToShelfAsync(_remote, stable_id, cb)
return self.client:shelfActionAsync("add", stable_id, cb)
end
function remote.removeFromShelfAsync(_remote, stable_id, cb)
return self.client:shelfActionAsync("delete", stable_id, cb)
end
return remote
end
---@param opts { force?: boolean, dirty_only?: boolean, on_progress?: fun(text: string|nil, done: integer|nil, total: integer|nil) }|nil
---@param cb fun(result: SyncResult|nil, err: any)
---@return CancelHandle|nil
---@param self FanqieSource
function Source:syncBooksAsync(opts, cb)
opts = opts or {}
if not self:configured() then
if opts.dirty_only then return Base.skipped(cb, "fanqie not logged in") end
return Base.defer(cb, nil, _("还没登录番茄小说。打开「设置 → 书库 → 账号 → 番茄小说 → 番茄账号与登录」,用番茄小说 App 或抖音扫屏幕上的二维码。"))
end
-- 加入时间来自书架 add_shelf_time,否则整架一次同步的 inserted_at 相同,「最近添加」只能按书号排。
opts.inserted_at_from_row = true
return require("source.shelf").syncAsync(self.id, shelfRemote(self, opts.force), shelfBooks, opts, cb)
end
--- 书城搜索:免登录端点,字段比书架少(没有字数 / 最新章)。
---@param keyword string
---@param cb fun(res: { data: Book[] }|nil, err: string|nil)
---@return { cancel: fun() }
function Source:searchAsync(keyword, cb)
return self.client:searchAsync(keyword, function(wire, err)
if not wire then
cb(nil, errMsg(err))
return
end
local FanQie = require("moonsource_fanqie.fanqie")
local books = {}
local list = type(wire.data) == "table" and wire.data.ret_data
for _idx, row in ipairs(type(list) == "table" and list or {}) do
if row.book_id then
books[#books + 1] = {
source_id = self.id,
stable_id = tostring(row.book_id),
title = row.title,
authors = row.author or "",
category = row.category,
intro = row.abstract,
cover = FanQie.mobileCover(row.thumb_url) or FanQie.mobileCover(row.audio_thumb_uri),
}
end
end
cb({ data = books })
end)
end
--- 书城浏览:网页榜单,榜单(阅读榜 / 新书榜)× 频道(男 / 女)× 分类;节点是「频道:榜单:分类」,缺省男频阅读榜首个分类。
--- 页面只带前 10 本(分页接口要签名),分类列表取自页面本身。
---@param opts { node?: string }
---@param cb fun(res: { data: Book[], groups: StoreChipGroup[] }|nil, err: string|nil)
---@return { cancel: fun() }
function Source:browseAsync(opts, cb)
local gender, kind, category = (opts.node or "1:2:"):match("^(%d):(%d):(%d*)$")
return self.client:rankAsync(gender .. "_" .. kind .. (category ~= "" and "_" .. category or ""), function(html, err)
local rank = html and require("moonsource_fanqie.rank").state(html)
if not rank then
cb(nil, err and errMsg(err) or _("加载失败"))
return
end
local cats, first = {}, nil
for _idx, cat in ipairs(rank.rankCategoryTypeList[gender == "1" and "male" or "female"] or {}) do
first = first or cat.id
cats[#cats + 1] = { id = gender .. ":" .. kind .. ":" .. cat.id, title = cat.name }
end
local node = gender .. ":" .. kind .. ":" .. (category ~= "" and category or first or "")
cb({
data = require("moonsource_fanqie.rank").books(rank, self.id),
groups = {
Browse.group({ { id = gender .. ":2:", title = _("阅读榜") }, { id = gender .. ":1:", title = _("新书榜") } },
gender .. ":" .. kind .. ":"),
Browse.group({ { id = "1:" .. kind .. ":", title = _("男频") }, { id = "0:" .. kind .. ":", title = _("女频") } },
gender .. ":" .. kind .. ":"),
Browse.group(cats, node),
},
})
end)
end
function Source:getDetailAsync(identity, cb)
local h = handle()
h.job = self.client:fetchBookDetailAsync(identity.stable_id, function(row, err)
if h.cancelled then return end
if not row then cb(nil, errMsg(err)); return end
local BookDB = require("book.books_db")
local book = shelfBook(row)
local existing = BookDB.get(self.id, identity.stable_id)
if existing then book.deleted = existing.deleted end
if not BookDB.upsertRemote(book) then
cb(nil, _("书籍信息保存失败"))
return
end
cb(BookDB.get(self.id, identity.stable_id))
end)
return h
end
--- 刷新目录时按 uid 并入已缓存的章内锚点。TocDB.put 只保留同 uid 的 words/paid,不会带回 extra。
---@param previous BookChapter[]|nil
---@param toc BookChapter[]
local function mergeCachedAnchors(previous, toc)
if not previous or #previous == 0 then return end
local Content = require("moonsource_fanqie.content")
local by_uid = {}
for _idx, chapter in ipairs(previous) do
if chapter.uid then
by_uid[tostring(chapter.uid)] = chapter
end
end
for _idx, chapter in ipairs(toc) do
local old = chapter.uid and by_uid[tostring(chapter.uid)]
if old then
Content.merge_anchors(chapter, old.anchors)
end
end
end
--- 正文刚下完时,把章内锚点写进已缓存目录(失败不影响开章)。
---@param identity BookIdentity
---@param chapter BookChapter
---@param html string
local function persistAnchorsFromHtml(identity, chapter, html)
local Toc = require("source.toc")
local Content = require("moonsource_fanqie.content")
local source_id = identity.source_id or "ext.fanqie"
local list = Toc.read(source_id, identity.stable_id)
if not list then return end
local target
for _idx, item in ipairs(list) do
if item.uid == tostring(chapter.uid) or item.idx == chapter.idx then
target = item
break
end
end
if not target then return end
if Content.merge_anchors(target, Content.html_anchors(html, (target.depth or 1) + 1)) then
Toc.put(source_id, identity.stable_id, list)
end
end
--- 强制拉目录并写回;失败由调用方继续用旧缓存。
---@param identity BookIdentity
---@param cb fun(toc: BookChapter[]|nil, err: string|nil)
---@return { cancel: fun() }
function Source:refreshTocAsync(identity, cb)
local Toc = require("source.toc")
local Content = require("moonsource_fanqie.content")
local h = handle()
h.job = self.client:fetchChapterDirectoryAsync(identity.stable_id, function(wire, err)
if h.cancelled then return end
if not wire then
cb(nil, errMsg(err))
return
end
local rows = Content.readable_chapters(Content.normalize_chapters(wire))
local toc = {}
for _idx, row in ipairs(rows) do
local w = tonumber(row.wordCount or row.word_count or row.words or row.chapterWordCount)
toc[#toc + 1] = {
idx = #toc + 1, uid = tostring(row.itemId), title = row.title,
words = w and w > 0 and w or nil,
depth = row.depth,
volume = row.volume,
anchors = row.anchors,
}
end
if #toc == 0 then
cb(nil, _("番茄目录为空"))
return
end
mergeCachedAnchors(Toc.read(self.id, identity.stable_id), toc)
if not Toc.put(self.id, identity.stable_id, toc) then
cb(nil, _("章节目录保存失败"))
return
end
cb(toc)
end)
return h
end
function Source:loadTocAsync(identity, cb)
local Toc = require("source.toc")
local cached = Toc.read(self.id, identity.stable_id)
if cached and #cached > 0 then
return Base.defer(cb, cached)
end
return self:refreshTocAsync(identity, cb)
end
local function comicPath(identity, chapter)
return Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/" .. chapter.uid .. ".cbz"
end
---@param url string
---@param cb fun(data: string|nil, err: any)
---@return { cancel: fun() }|nil
local function downloadImage(url, cb)
return require("http.request").get(url, {
accept = "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
headers = { ["Referer"] = "https://fanqienovel.com/" },
timeout = require("http.timeout").net(3),
allow_redirects = true,
}, function(raw, err)
if raw and raw ~= "" then cb(raw) else cb(nil, err) end
end)
end
local function fetch(self, identity, chapter, cb)
local Content = require("moonsource_fanqie.content")
Paths.ensureBookWork(identity.stable_id, identity.source_id)
-- 已打好的漫画章节不再联网(也免去正文接口风控)
local archive = comicPath(identity, chapter)
if Cbz.ready(archive) then
return Base.defer(cb, { file = archive })
end
local h = handle()
h.job = self.client:officialGetContentAsync(identity.stable_id, chapter.uid, function(result, err)
if h.cancelled then return end
if not result then
cb(nil, errMsg(err))
return
end
local title = result.title ~= "" and result.title or chapter.title
local urls, key = Comic.parse(result.content)
if not urls then
-- 正文里的远程插图落到书籍工作目录;下不下来的图保留原地址,不拖垮整章。
h.job = Assets.localizeAsync(Content.decode_pua_content(result.content),
Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/images", downloadImage,
function(html)
if h.cancelled then return end
persistAnchorsFromHtml(identity, chapter, html)
cb({ title = title, html = html })
end)
return
end
h.job = Comic.materializeAsync(urls, key, comicPath(identity, chapter), function(path, err_msg)
if h.cancelled then return end
if path then cb({ title = title, file = path }) else cb(nil, err_msg) end
end)
end)
return h
end
-- 番茄正文接口有设备风控,预取与整本下载都按这个间隔逐章请求。
local FETCH_INTERVAL = 6
---@return ChapterFetchContent
local function contentFetcher(self)
return function(ref, ch, done) return fetch(self, ref, ch, done) 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 = contentFetcher(self),
}, cb)
end
function Source:prefetchChaptersAsync(identity, toc, from_idx, count, cb)
return require("source.chapter").prefetchAsync(identity, identity.book, toc, from_idx, count, {
fetchContent = contentFetcher(self),
interval_seconds = FETCH_INTERVAL,
}, cb)
end
--- 缓存整本章节正文;已落盘章节由公共实现自动跳过。
---@param identity BookIdentity
---@param on_progress fun(done: integer, total: integer)|nil
---@param cb fun(ok: boolean, cached: integer, err: string|nil, total: integer, failed: integer)
---@return { cancel: fun() }
function Source:cacheAllChaptersAsync(identity, on_progress, cb, range)
return require("source.chapter").cacheAllAsync(self, identity, contentFetcher(self), on_progress, cb,
FETCH_INTERVAL, range)
end
function Source:getProgressAsync(identity, cb)
local Toc = require("source.toc")
local h = handle()
h.job = self.client:fetchReadProgressAsync(function(wire, err)
if h.cancelled then return end
if not wire then
cb(nil, errMsg(err))
return
end
local row
for _idx, item in ipairs(wire.data or {}) do
if tostring(item.book_id) == identity.stable_id then
row = item
break
end
end
if not row or not row.item_id then
cb(nil, nil, { empty = true })
return
end
h.job = self:loadTocAsync(identity, function(toc, reason)
if h.cancelled then return end
if not toc then
cb(nil, reason)
return
end
local idx = Toc.index(self.id, identity.stable_id, row.item_id)
if not idx then
cb(nil, nil, { empty = true })
return
end
-- 官方网页只写章节:read_progress 为 0 或章节 order,并不是章内万分比。
cb({
chapter_idx = idx,
fraction = (idx - 1) / #toc,
extra = { chapter_uid = tostring(row.item_id), chapter_idx = idx },
})
end)
end)
return h
end
function Source:putProgressAsync(identity, pos, cb)
local h = handle()
h.job = self:loadTocAsync(identity, function(toc, err)
if h.cancelled then 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
h.job = self.client:updateReadProgressAsync(
identity.stable_id, chapter.uid, idx - 1,
0,
function(wire, reason)
if h.cancelled then return end
if not wire or tonumber(wire.code or 0) ~= 0 then
cb(nil, reason and errMsg(reason) or _("番茄进度上传失败"))
return
end
cb(true)
end)
end)
return h
end
--- 阅读页是否显示他人段评(设置页开关,默认关)。
---@return boolean
function Source:paragraphReviewsEnabled()
return self.settings:get("show_paragraph_reviews", false) == true
end
--- 章节序号 → 番茄 item_id,目录来自缓存或在线拉取。
local function withChapterUid(self, identity, cb, start)
local h = handle()
h.job = self:loadTocAsync(identity, function(toc, err)
if h.cancelled then return end
local chapter = toc and toc[tonumber(identity.chapter_idx)]
if not chapter then
cb(nil, err or _("缺少番茄章节位置"))
return
end
h.job = start(chapter.uid, cb)
end)
return h
end
--- 本章有评论的段落(未定位)。
---@param identity BookIdentity
---@param cb fun(hotspots: FanqieReviewHotspot[]|nil, err: string|nil)
---@return CancelHandle
function Source:paragraphHotspotsAsync(identity, cb)
return ReviewCache.hotspotsAsync(identity, function(done)
return withChapterUid(self, identity, done, function(uid, finish)
return require("moonsource_fanqie.reviews").hotspotsAsync(self.settings, uid, finish)
end)
end, cb)
end
--- 段落按段末文字定位到章节 HTML(子进程里跑,不碰实例状态)。
---@param hotspots FanqieReviewHotspot[]
---@param html string
---@return table[]
function Source:locateHotspots(hotspots, html)
return require("moonsource_fanqie.reviews").locate(hotspots, html)
end
--- 某段的一页评论;range 是段落序号,max_idx 是游标偏移(首页 0)。
---@param identity BookIdentity
---@param range string
---@param max_idx integer
---@param cb fun(page: WechatReviewPage|nil, err: string|nil)
---@return CancelHandle
function Source:paragraphReviewPageAsync(identity, range, max_idx, cb)
return ReviewCache.pageAsync(identity, range, max_idx, function(done)
return withChapterUid(self, identity, done, function(uid, finish)
return require("moonsource_fanqie.reviews").pageAsync(
self.settings, identity.stable_id, uid, tonumber(range), max_idx, finish)
end)
end, cb)
end
--- 某条段评的一页回复。不走段评缓存。
---@param _identity BookIdentity
---@param review_id string
---@param max_idx integer|string
---@param cb fun(page: WechatReviewPage|nil, err: string|nil)
---@return CancelHandle
function Source:paragraphReviewRepliesAsync(_identity, review_id, max_idx, cb)
return require("moonsource_fanqie.reviews").repliesAsync(self.settings, review_id, max_idx, cb)
end
return M