@@ -30,6 +30,19 @@ type ApiSearchItem struct {
3030 TypeName * string `json:"type_name,omitempty"`
3131}
3232
33+ type ApiSearchItem2 struct {
34+ VodID string `json:"vod_id"`
35+ VodName string `json:"vod_name"`
36+ VodPic string `json:"vod_pic"`
37+ VodRemarks * string `json:"vod_remarks,omitempty"`
38+ VodPlayURL * string `json:"vod_play_url,omitempty"`
39+ VodClass * string `json:"vod_class,omitempty"`
40+ VodYear * string `json:"vod_year,omitempty"`
41+ VodContent * string `json:"vod_content,omitempty"`
42+ VodDoubanID * int `json:"vod_douban_id,omitempty"`
43+ TypeName * string `json:"type_name,omitempty"`
44+ }
45+
3346// models.SearchResult 搜索结果数据结构
3447
3548// CachedSearchPage 缓存的搜索页面数据
@@ -38,6 +51,7 @@ type CachedSearchPage struct {
3851 Data []models.SearchResult `json:"data"`
3952 PageCount * int `json:"page_count,omitempty"`
4053 Timestamp time.Time `json:"timestamp"`
54+ ExpiresAt time.Time `json:"expires_at"` // 过期时间
4155}
4256
4357// 全局变量
@@ -102,40 +116,76 @@ func SearchWithCache(apiSite models.APISite, query string, page int, url string,
102116
103117 if resp .StatusCode == 403 {
104118 SetCachedSearchPage (apiSite .Key , query , page , "forbidden" , []models.SearchResult {}, nil )
105- fmt .Println ("搜索请求失败" , resp .StatusCode )
119+ // fmt.Println("搜索请求失败", resp.StatusCode)
106120 return []models.SearchResult {}, nil , nil
107121 }
108122
109123 if resp .StatusCode != http .StatusOK {
110- fmt .Println ("搜索请求失败" , resp .StatusCode )
124+ // fmt.Println("搜索请求失败", resp.StatusCode)
111125 return []models.SearchResult {}, nil , fmt .Errorf ("HTTP error: %d" , resp .StatusCode )
112126 }
113127
114128 body , err := io .ReadAll (resp .Body )
115129 if err != nil {
116- fmt .Println ("搜索请求失败" , err )
130+ // fmt.Println("搜索请求失败", err)
117131 return []models.SearchResult {}, nil , err
118132 }
119133
134+ // 尝试第一种数据结构 (ApiSearchItem with int VodID)
120135 var data struct {
121136 List []ApiSearchItem `json:"list"`
122137 PageCount int `json:"pagecount"`
123138 }
124139
140+ var data2 struct {
141+ List []ApiSearchItem2 `json:"list"`
142+ PageCount int `json:"pagecount"`
143+ }
144+
145+ var items []ApiSearchItem
125146 if err := json .Unmarshal (body , & data ); err != nil {
126- fmt .Println ("搜索请求失败" , err )
127- return []models.SearchResult {}, nil , err
147+ // 如果第一种结构解析失败,尝试第二种结构 (ApiSearchItem2 with string VodID)
148+ if err2 := json .Unmarshal (body , & data2 ); err2 != nil {
149+ // fmt.Println("搜索请求失败,两种数据结构都解析失败", err, err2)
150+ fmt .Println (string (body ))
151+ return []models.SearchResult {}, nil , err
152+ }
153+ // 将 ApiSearchItem2 转换为 ApiSearchItem
154+ items = make ([]ApiSearchItem , len (data2 .List ))
155+ for i , item2 := range data2 .List {
156+ // 将 string 类型的 VodID 转换为 int
157+ vodID , err := strconv .Atoi (item2 .VodID )
158+ if err != nil {
159+ // 如果转换失败,跳过这个项目或使用默认值
160+ // fmt.Printf("警告: 无法转换 VodID %s 为整数,跳过该项目\n", item2.VodID)
161+ continue
162+ }
163+ items [i ] = ApiSearchItem {
164+ VodID : vodID ,
165+ VodName : item2 .VodName ,
166+ VodPic : item2 .VodPic ,
167+ VodRemarks : item2 .VodRemarks ,
168+ VodPlayURL : item2 .VodPlayURL ,
169+ VodClass : item2 .VodClass ,
170+ VodYear : item2 .VodYear ,
171+ VodContent : item2 .VodContent ,
172+ VodDoubanID : item2 .VodDoubanID ,
173+ TypeName : item2 .TypeName ,
174+ }
175+ }
176+ } else {
177+ items = data .List
128178 }
129179
130- if len (data . List ) == 0 {
180+ if len (items ) == 0 {
131181 // 空结果不做负缓存要求
132- fmt .Println ("搜索请求失败" , "空结果" )
182+ // fmt.Println("搜索请求失败", "空结果")
133183 return []models.SearchResult {}, nil , nil
134184 }
135185
136- // 处理结果数据
186+ // 统一处理结果数据
137187 var allResults []models.SearchResult
138- for _ , item := range data . List {
188+ for _ , item := range items {
139189 var episodes []string
140190 var titles []string
141191
@@ -204,7 +254,7 @@ func SearchFromAPI(apiSite models.APISite, query string, maxPages int) ([]models
204254 // 使用新的缓存搜索函数处理第一页
205255 firstPageResults , pageCountFromFirst , err := SearchWithCache (apiSite , query , 1 , apiURL , 8000 )
206256 if err != nil {
207- fmt .Println ("搜索请求失败" , err )
257+ // fmt.Println("搜索请求失败", err)
208258 return []models.SearchResult {}, err
209259 }
210260
@@ -343,7 +393,7 @@ func GetDetailFromAPI(apiSite models.APISite, id string) (*models.SearchResult,
343393
344394 req , err := http .NewRequestWithContext (ctx , "GET" , detailURL , nil )
345395 if err != nil {
346- fmt .Println ("详情请求失败" , err )
396+ // fmt.Println("详情请求失败", err)
347397 return nil , err
348398 }
349399
@@ -563,7 +613,7 @@ func FetchVideoDetail(options FetchVideoDetailOptions) (*models.SearchResult, er
563613 apiSites := config .GlobalConfig .APISites
564614 apiSite , exists := apiSites [options .Source ]
565615 if ! exists {
566- fmt .Println ("无效的API来源" , options .Source )
616+ // fmt.Println("无效的API来源", options.Source)
567617 return nil , fmt .Errorf ("无效的API来源" )
568618 }
569619
0 commit comments