package kongfz import ( "encoding/json" "errors" "io" "log" "net/http" "net/url" "strings" "time" ) var TailTokens = []string{ "28fe4cf37dd6b2fa74f349236eb99c5c3914ebe3", "4014640556b5b7c2181db662dea8775eb2c2469e", "44c59815ae2c21935a88887b77cc89f5e3bc1fc3", "69a0f30520e40fe617070d0688930366f544727f", "1967ba84f30723c259b0dac2bd5734e025316ef8", "39fade4d7167d94cef519d873362af1949990fde", "cdc7d73a44d0ef8ca84b6618dde283ebca694892", "8e93849cff58c83cca7227a6dc99abb56ac3d33f", "1fd05fc23e8403431b724d816a115b579c77d515", "b20b7d9728ceb5a4006104ef12d164e9438994e6", "a97ab57a40df46de2bf850ceb854e8bab282cd33", } // BookResponse 孔网数据返回格式 type BookResponse struct { Data struct { ISBN string `json:"isbn"` Title string `json:"title"` Author string `json:"author"` Publisher string `json:"publisher"` PublicationTime int64 `json:"publication_time"` Price float64 `json:"price"` BookName string `json:"book_name"` FixPrice string `json:"fix_price"` BindingLayout string `json:"binding_layout"` BookPic string `json:"book_pic"` BookPicS string `json:"book_pic_s"` } `json:"data"` Success bool `json:"success"` Error string `json:"error"` // <- 新增 } func GetBookImageByISBN(isbn, proxyType, username, password string) (*BookResponse, error) { baseURL := "http://103.236.81.185:8989/api/outGetImageByIsbn" client := &http.Client{Timeout: 10 * time.Second} for _, token := range TailTokens { params := url.Values{} params.Set("isbn", isbn) params.Set("token", token) params.Set("proxyType", proxyType) params.Set("username", username) params.Set("password", password) reqURL := baseURL + "?" + params.Encode() log.Println("[GetBookImageByISBN] 请求参数:", reqURL) req, err := http.NewRequest("GET", reqURL, nil) if err != nil { log.Println("[GetBookImageByISBN] 构建请求失败:", err) continue } resp, err := client.Do(req) if err != nil { log.Println("[GetBookImageByISBN] 请求失败:", err) continue } body, err := io.ReadAll(resp.Body) resp.Body.Close() // 立即关闭 if err != nil { log.Println("[GetBookImageByISBN] 读取响应失败:", err) continue } log.Println("[GetBookImageByISBN] 响应:", string(body)) var res BookResponse if err := json.Unmarshal(body, &res); err != nil { log.Println("[GetBookImageByISBN] 解析 JSON 失败:", err) continue } log.Printf("[GetBookImageByISBN] 解析后的 Success=%v, Error=%s", res.Success, res.Error) if res.Success { log.Println("[GetBookImageByISBN] 成功返回 BookResponse") return &res, nil } else if strings.Contains(res.Error, "请登录后再进行访问") { log.Println("[GetBookImageByISBN] Token 需要登录,尝试下一个 token") continue } else { log.Println("[GetBookImageByISBN] 其他错误,直接返回") return &res, errors.New(res.Error) } } return nil, errors.New("所有 token 均失效或接口不可用") } // 简单包含判断 func contains(s, substr string) bool { return strings.Contains(s, substr) }