MCPcopy
hub / github.com/fish2018/pansou / executeSearch

Method executeSearch

plugin/xys/xys.go:237–293  ·  view source on GitHub ↗

executeSearch 执行搜索请求

(client *http.Client, token, keyword string)

Source from the content-addressed store, hash-verified

235
236// executeSearch 执行搜索请求
237func (p *XysPlugin) executeSearch(client *http.Client, token, keyword string) ([]model.SearchResult, error) {
238 // 构建搜索URL
239 searchURL := fmt.Sprintf("%s%s?DToken2=%s&requestID=undefined&mode=90002&stype=undefined&scope_content=0&wd=%s&uk=&page=1&limit=20&screen_filetype=",
240 BaseURL, SearchPath, token, url.QueryEscape(keyword))
241
242 // 创建带超时的上下文
243 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
244 defer cancel()
245
246 req, err := http.NewRequestWithContext(ctx, "POST", searchURL, nil)
247 if err != nil {
248 return nil, fmt.Errorf("[%s] 创建搜索请求失败: %w", p.Name(), err)
249 }
250
251 // 设置完整的请求头
252 req.Header.Set("User-Agent", UserAgent)
253 req.Header.Set("Accept", "application/json, text/plain, */*")
254 req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
255 req.Header.Set("Connection", "keep-alive")
256 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
257 req.Header.Set("Referer", BaseURL+"/")
258 req.Header.Set("Origin", BaseURL)
259 req.Header.Set("X-Requested-With", "XMLHttpRequest")
260
261 resp, err := p.doRequestWithRetry(req, client)
262 if err != nil {
263 return nil, fmt.Errorf("[%s] 搜索请求失败: %w", p.Name(), err)
264 }
265 defer resp.Body.Close()
266
267 if resp.StatusCode != 200 {
268 return nil, fmt.Errorf("[%s] 搜索请求HTTP状态错误: %d", p.Name(), resp.StatusCode)
269 }
270
271 // 读取响应体
272 respBody, err := io.ReadAll(resp.Body)
273 if err != nil {
274 return nil, fmt.Errorf("[%s] 读取响应体失败: %w", p.Name(), err)
275 }
276
277 // 解析JSON响应
278 var searchResp SearchResponse
279 if err := json.Unmarshal(respBody, &searchResp); err != nil {
280 return nil, fmt.Errorf("[%s] JSON解析失败: %w", p.Name(), err)
281 }
282
283 if searchResp.Code != 0 {
284 return nil, fmt.Errorf("[%s] 搜索API返回错误: %s", p.Name(), searchResp.Msg)
285 }
286
287 if p.debugMode {
288 log.Printf("[XYS] 搜索API响应成功,data长度: %d", len(searchResp.Data))
289 }
290
291 // 解析HTML内容
292 return p.parseSearchResults(searchResp.Data, keyword)
293}
294

Callers 1

searchImplMethod · 0.95

Calls 6

NameMethod · 0.95
doRequestWithRetryMethod · 0.95
parseSearchResultsMethod · 0.95
UnmarshalFunction · 0.92
SetMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected