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

Method searchImpl

plugin/xiaozhang/xiaozhang.go:135–198  ·  view source on GitHub ↗

searchImpl 实际的搜索实现

(client *http.Client, keyword string, ext map[string]interface{})

Source from the content-addressed store, hash-verified

133
134// searchImpl 实际的搜索实现
135func (p *XiaozhangPlugin) searchImpl(client *http.Client, keyword string, ext map[string]interface{}) ([]model.SearchResult, error) {
136 searchURL := fmt.Sprintf("%s%s?keyword=%s", BaseURL, SearchPath, url.QueryEscape(keyword))
137
138 if p.debugMode {
139 log.Printf("[Xiaozhang] 开始搜索: %s", keyword)
140 log.Printf("[Xiaozhang] 搜索URL: %s", searchURL)
141 }
142
143 // 发送搜索请求
144 resp, err := p.doRequest(client, searchURL, BaseURL, true)
145 if err != nil {
146 return nil, fmt.Errorf("发送搜索请求失败: %w", err)
147 }
148 defer resp.Body.Close()
149
150 if resp.StatusCode != http.StatusOK {
151 return nil, fmt.Errorf("搜索响应状态码异常: %d", resp.StatusCode)
152 }
153
154 // 处理响应体(可能是gzip压缩的)
155 var reader io.Reader = resp.Body
156
157 // 检查Content-Encoding
158 contentEncoding := resp.Header.Get("Content-Encoding")
159 if p.debugMode {
160 log.Printf("[Xiaozhang] Content-Encoding: %s", contentEncoding)
161 log.Printf("[Xiaozhang] Content-Type: %s", resp.Header.Get("Content-Type"))
162 }
163
164 // 如果是gzip压缩,手动解压
165 if contentEncoding == "gzip" {
166 gzReader, err := gzip.NewReader(resp.Body)
167 if err != nil {
168 return nil, fmt.Errorf("创建gzip reader失败: %w", err)
169 }
170 defer gzReader.Close()
171 reader = gzReader
172 }
173
174 // 解析HTML
175 doc, err := goquery.NewDocumentFromReader(reader)
176 if err != nil {
177 return nil, fmt.Errorf("解析HTML失败: %w", err)
178 }
179
180 // 提取搜索结果
181 results := p.extractSearchResults(doc, keyword)
182
183 if p.debugMode {
184 log.Printf("[Xiaozhang] 找到 %d 个搜索结果", len(results))
185 }
186
187 // 并发获取详情页链接
188 results = p.enrichWithDetailLinks(client, results, keyword)
189
190 // 过滤结果
191 filteredResults := plugin.FilterResultsByKeyword(results, keyword)
192

Callers

nothing calls this directly

Calls 6

doRequestMethod · 0.95
extractSearchResultsMethod · 0.95
enrichWithDetailLinksMethod · 0.95
FilterResultsByKeywordFunction · 0.92
CloseMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected