- 正文参数支持语义值(plain/html/keep/remove)与别名(img_mode/review_mode),URL 参数优先于配置 - 修复纯文本被段评强制包回 HTML、HTML 模式段评注入失效的问题 - 修复 PKCS7 填充弱校验导致漫画/正文偶发解密乱码返回 200 的问题 - 搜索结果展平:去 search_tabs 层与 selected_tab_idx/server_ab_params,提升 has_more/next_offset - 全接口解包字符串形式的 JSON 字段(category_schema 等),缓存出口幂等处理 - 序列化关闭 HTML 转义,video/audio 播放链接可直接复制播放 - video 响应透传 CENC 加密状态(key_seed/kid/encryption_method) - 新增 gzip 中间件,JSON/文本响应压缩约 81%,二进制透传 - 漫画默认输出模式改为 proxy(url 模式链接 404 且内容加密不可用) - openapi.yaml:补全 23 个路径参数定义、tab_type 完整枚举、CDN 签名说明,示例更新
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package fqapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fanqie/internal/common"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
var searchLogger = common.NewLogger("search", "INFO", "")
|
|
|
|
// GetDeviceHeader 获取设备header(需要从外部注入)
|
|
var GetDeviceHeader func() string
|
|
|
|
// HandleBookSearch 处理书籍搜索,搜索结果短缓存 10 分钟,减轻上游压力
|
|
func HandleBookSearch(key string, offset int, tabType int) (string, error) {
|
|
return withCache(fmt.Sprintf("search_%s_%d_%d", key, offset, tabType), 600, func() (string, error) {
|
|
return fetchBookSearch(key, offset, tabType)
|
|
})
|
|
}
|
|
|
|
func fetchBookSearch(key string, offset int, tabType int) (string, error) {
|
|
if key == "" {
|
|
return BuildErrorResponseWithCode(1, "需要参数 key"), nil
|
|
}
|
|
|
|
if tabType == 0 {
|
|
tabType = 3
|
|
}
|
|
|
|
// 编码查询关键字
|
|
encodedKey := url.QueryEscape(key)
|
|
|
|
// 使用新的参数构建函数
|
|
reqURL := BuildURL("https://reading.snssdk.com/reading/bookapi/search/tab/v/",
|
|
fmt.Sprintf("tab_type=%d", tabType),
|
|
"update_version_code=58932",
|
|
"query="+encodedKey,
|
|
fmt.Sprintf("passback=%d", offset),
|
|
)
|
|
|
|
resp, err := GorgonRequest(reqURL, map[string]interface{}{
|
|
"method": "GET",
|
|
"headers": map[string]string{
|
|
"User-Agent": "com.dragon.read",
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
searchLogger.Error(fmt.Sprintf("搜索请求失败: %v", err))
|
|
return BuildErrorResponseWithCode(1, "搜索请求失败"), err
|
|
}
|
|
|
|
respJSON, err := resp.JSON()
|
|
if err != nil {
|
|
searchLogger.Error(fmt.Sprintf("解析响应失败: %v", err))
|
|
return BuildErrorResponseWithCode(1, "解析响应失败"), err
|
|
}
|
|
|
|
if code, ok := respJSON["code"].(float64); ok && code == 0 {
|
|
// 用 UseNumber 重新解析后按 tab_type 过滤:
|
|
// 上游 search/tab/v/ 固定返回所有 tab 的 search_tabs;
|
|
// 大整数 ID 经 float64 重组会丢精度,必须 UseNumber
|
|
dec := json.NewDecoder(bytes.NewReader(resp.Content))
|
|
dec.UseNumber()
|
|
var numJSON map[string]interface{}
|
|
if err := dec.Decode(&numJSON); err != nil {
|
|
return resp.Text, nil // 解析失败时退回原样转发
|
|
}
|
|
filterSearchTabs(numJSON, tabType)
|
|
unwrapSearchTab(numJSON)
|
|
UnwrapJSONStrings(numJSON)
|
|
return ToJSON(numJSON)
|
|
}
|
|
|
|
return BuildErrorResponseWithCode(1, "搜索失败"), nil
|
|
}
|
|
|
|
// filterSearchTabs 按 tab_type 只保留 search_tabs 中匹配的项;无匹配时保留原样
|
|
func filterSearchTabs(respJSON map[string]interface{}, tabType int) {
|
|
tabs, ok := respJSON["search_tabs"].([]interface{})
|
|
if !ok {
|
|
return
|
|
}
|
|
filtered := make([]interface{}, 0, 1)
|
|
for _, t := range tabs {
|
|
m, ok := t.(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
if tt, ok := m["tab_type"].(json.Number); ok {
|
|
if v, err := tt.Int64(); err == nil && int(v) == tabType {
|
|
filtered = append(filtered, m)
|
|
}
|
|
}
|
|
}
|
|
if len(filtered) > 0 {
|
|
respJSON["search_tabs"] = filtered
|
|
}
|
|
}
|
|
|
|
// unwrapSearchTab 将过滤后的唯一 tab 展平:去掉 search_tabs 包装,
|
|
// tab 的 data 直接作为顶层 data,并提升分页字段 has_more/next_offset
|
|
func unwrapSearchTab(respJSON map[string]interface{}) {
|
|
tabs, ok := respJSON["search_tabs"].([]interface{})
|
|
if !ok || len(tabs) != 1 {
|
|
return
|
|
}
|
|
tab, ok := tabs[0].(map[string]interface{})
|
|
if !ok {
|
|
return
|
|
}
|
|
delete(respJSON, "search_tabs")
|
|
delete(respJSON, "selected_tab_idx") // tab 已展平,索引无意义
|
|
delete(respJSON, "server_ab_params") // 上游 AB 实验参数,透传无意义
|
|
if data, ok := tab["data"]; ok {
|
|
respJSON["data"] = data
|
|
}
|
|
for _, k := range []string{"has_more", "next_offset"} {
|
|
if v, ok := tab[k]; ok {
|
|
respJSON[k] = v
|
|
}
|
|
}
|
|
}
|