Skip to content

Commit ce6bff2

Browse files
committed
feat(func): media library support change api url
1 parent cc1fec5 commit ce6bff2

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

internal/media/scraper/discogs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func NewDiscogsScraper(token string, baseURL string) *DiscogsScraper {
2828
if base == "" {
2929
base = discogsDefaultBaseURL
3030
}
31+
// 若用户填入的地址未带协议头,则自动补全为 https://
32+
if !strings.HasPrefix(base, "http://") && !strings.HasPrefix(base, "https://") {
33+
base = "https://" + base
34+
}
3135
return &DiscogsScraper{
3236
Token: token,
3337
BaseURL: base,

internal/media/scraper/tmdb.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,28 @@ type TMDBScraper struct {
9999
}
100100

101101
// NewTMDBScraper 创建TMDB刮削器
102-
// baseURL 为可自定义的 TMDB API 服务地址(不含版本路径),为空时使用默认 https://api.themoviedb.org
102+
// baseURL 为可自定义的 TMDB API 服务地址,为空时使用默认 https://api.themoviedb.org/3
103+
//
104+
// 支持的填写形式:
105+
// 1. 留空:使用默认 https://api.themoviedb.org/3
106+
// 2. 官方主域名(不带路径):如 api.themoviedb.org / https://api.themoviedb.org,自动补全协议和 /3 版本路径
107+
// 3. 反代/自定义完整路径:如 https://eo-tmd.example.com/api/tmdb/3,会原样使用,不再追加 /3
103108
func NewTMDBScraper(apiKey string, baseURL string) *TMDBScraper {
104109
base := strings.TrimRight(strings.TrimSpace(baseURL), "/")
105110
if base == "" {
106111
base = tmdbDefaultBaseURL
107112
}
108-
// 若用户填入的地址未带 /3 版本路径,则自动追加
109-
if !strings.HasSuffix(base, "/3") {
110-
base = base + "/3"
113+
// 若用户填入的地址未带协议头,则自动补全为 https://
114+
if !strings.HasPrefix(base, "http://") && !strings.HasPrefix(base, "https://") {
115+
base = "https://" + base
116+
}
117+
// 仅当用户填写的是「裸主机名(不含自定义路径)」时,才自动追加 /3 版本路径
118+
// 反代地址通常带有自定义前缀(如 /api/tmdb/3),不应再追加,否则会导致 404
119+
if u, err := url.Parse(base); err == nil {
120+
// Path 为空或仅根路径 "/" 时,认为用户没有填写自定义路径,自动补 /3
121+
if u.Path == "" || u.Path == "/" {
122+
base = strings.TrimRight(base, "/") + "/3"
123+
}
111124
}
112125
return &TMDBScraper{
113126
APIKey: apiKey,
@@ -168,7 +181,13 @@ func (s *TMDBScraper) doTMDBSearch(query, year string) (*tmdbSearchResult, error
168181
body, _ := io.ReadAll(resp.Body)
169182
var result tmdbSearchResult
170183
if err := json.Unmarshal(body, &result); err != nil {
171-
return nil, fmt.Errorf("TMDB搜索结果解析失败: %w", err)
184+
// 附带响应状态码和响应体片段,便于排查 API 地址错误、被代理拦截等问题
185+
snippet := string(body)
186+
if len(snippet) > 200 {
187+
snippet = snippet[:200]
188+
}
189+
return nil, fmt.Errorf("TMDB搜索结果解析失败(status=%d, url=%s): %w, body=%s",
190+
resp.StatusCode, searchURL, err, snippet)
172191
}
173192
return &result, nil
174193
}

0 commit comments

Comments
 (0)