--[[-- 晋江段评:本章各段评论数、某段的评论页、把段落定位到章节 HTML。 走晋江网页版的段评接口(www.jjwxc.net/app.jjwxc/Pc/comment/…),免费章节匿名可读,VIP 章节要登录态(当前协议的 commentAuth:web 带 Cookie,app 带 token 查询参数): - getNovelParagraphCommentNum:本章每段的评论数;paragraph_id 是「第几个非空段」(从 1 数),-2 是章末总评。 - getCommentList:某段的评论,offset / limit 翻页,新的在前。 章节 HTML 的段落就是非空行逐个包成
(Text.textToBody),所以第 n 个
就是 paragraph_id = n。
@module koplugin.book.moonsource_jjwxc.reviews
--]]
local JSON = require("json")
local Request = require("http.request")
local ReviewLocate = require("book.review_locate")
local Text = require("utils.text")
local _ = require("l10n").get("moonsource_jjwxc")
local Reviews = {}
local BASE = "https://www.jjwxc.net/app.jjwxc/Pc/comment/"
local VERSION = "489"
local PAGE_SIZE = 20
---@class JjwxcReviewHotspot
---@field range string 段落序号(第几个非空段)
---@field text string
---@field total integer 本段评论数
---@param name string
---@param query string
---@param novel_id string
---@param chapter_id string
---@param cb fun(data: table|nil, err: string|nil)
---@return HttpJob
local function get(name, query, novel_id, chapter_id, cb)
local referer = "https://www.jjwxc.net/onebook.php?novelid=" .. novel_id .. "&chapterid=" .. chapter_id
local headers = { ["User-Agent"] = "Mozilla/5.0", Referer = referer, versionCode = VERSION }
-- 免费章节匿名可读,VIP 章节要登录态;没有任何登录态就匿名发。
local protocol = require("moonsource_jjwxc.protocols").current()
local auth_query, auth_headers = "", {}
if protocol then auth_query, auth_headers = protocol.commentAuth() end
for key, value in pairs(auth_headers) do headers[key] = value end
return Request.get(BASE .. name .. "?versionCode=" .. VERSION .. query .. auth_query, {
accept = "application/json",
headers = headers,
timeout = require("http.timeout").net(0.5),
}, function(raw, err)
if not raw then cb(nil, err and tostring(err) or _("请求失败")); return end
local ok, wire = pcall(JSON.decode, raw)
if not ok or type(wire) ~= "table" then cb(nil, _("响应无效")); return end
if tostring(wire.code) ~= "200" or type(wire.data) ~= "table" then
cb(nil, tostring(wire.message or _("晋江段评请求失败")))
return
end
cb(wire.data)
end)
end
--- 统计回包 → 有评论的段落与章末(-2);源入口按粒度分开。
---@param data table
---@return JjwxcReviewHotspot[]|nil, string|nil
function Reviews.hotspots(data)
if next(data) and data[1] == nil then return nil, _("响应无效") end
local out = {}
for _idx, row in ipairs(data) do
if type(row) ~= "table" then return nil, _("响应无效") end
local pid, total = tonumber(row.paragraph_id), tonumber(row.comment_total)
if not pid or not total or total < 0 then return nil, _("响应无效") end
if (pid > 0 or pid == -2) and total > 0 then
out[#out + 1] = { range = tostring(pid), text = "", total = total }
end
end
table.sort(out, function(a, b) return tonumber(a.range) < tonumber(b.range) end)
return out
end
--- 表情标记交给主题行内图片渲染;保留原文供离线缺图时显示。
---@param content string
---@return table