mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-06 21:36:37 +08:00
feat: 实现智能分类功能
- 配置模型扩展(OrganizerConfig:smart_classify + categories) - 数据模型扩展(Media:languages/countries/genres 字段) - TMDbProvider 增强(GetDetails 方法获取扩展元数据) - Scraper 服务增强(保存 languages/countries/genres 到数据库) - Organizer 智能分类逻辑(根据元数据自动分类到子目录) - 前端 SettingsPage 添加智能分类开关 - 后端支持从数据库读取 organizer.smart_classify 设置 Task #90-94 完成,Task #95 部分完成
This commit is contained in:
+151
-9
@@ -80,14 +80,22 @@ func (t *TMDbProvider) Enabled() bool {
|
||||
func (t *TMDbProvider) resolveAPIKey(ctx context.Context) string {
|
||||
// Check config first (fast path)
|
||||
if t.cfg.Secrets.TMDbAPIKey != "" {
|
||||
t.log.Debug("tmdb: using API key from config file")
|
||||
return t.cfg.Secrets.TMDbAPIKey
|
||||
}
|
||||
// Fall back to database
|
||||
if t.apiConfig != nil {
|
||||
resolved, err := t.apiConfig.Resolve(ctx, "tmdb")
|
||||
if err == nil && resolved.APIKey != "" {
|
||||
if err != nil {
|
||||
t.log.Warn("tmdb: failed to resolve API key from database", zap.Error(err))
|
||||
} else if resolved.APIKey == "" {
|
||||
t.log.Warn("tmdb: API key is empty in database")
|
||||
} else {
|
||||
t.log.Debug("tmdb: using API key from database")
|
||||
return resolved.APIKey
|
||||
}
|
||||
} else {
|
||||
t.log.Warn("tmdb: apiConfig is nil, cannot resolve API key from database")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -113,14 +121,17 @@ func (t *TMDbProvider) resolveBaseURL(ctx context.Context) string {
|
||||
// across providers; provider-specific IDs sit side-by-side so the scraper
|
||||
// orchestrator can write them all into a single update.
|
||||
type Match struct {
|
||||
TMDbID int `json:"tmdb_id"`
|
||||
BangumiID int `json:"bangumi_id"`
|
||||
Title string `json:"title"`
|
||||
Overview string `json:"overview"`
|
||||
PosterURL string `json:"poster_url"`
|
||||
BackdropURL string `json:"backdrop_url"`
|
||||
Year int `json:"year"`
|
||||
Rating float32 `json:"rating"`
|
||||
TMDbID int `json:"tmdb_id"`
|
||||
BangumiID int `json:"bangumi_id"`
|
||||
Title string `json:"title"`
|
||||
Overview string `json:"overview"`
|
||||
PosterURL string `json:"poster_url"`
|
||||
BackdropURL string `json:"backdrop_url"`
|
||||
Year int `json:"year"`
|
||||
Rating float32 `json:"rating"`
|
||||
Languages []string `json:"languages,omitempty"`
|
||||
Countries []string `json:"countries,omitempty"`
|
||||
Genres []string `json:"genres,omitempty"`
|
||||
}
|
||||
|
||||
// SearchMovie issues `/search/movie` and returns the best match, or nil
|
||||
@@ -201,3 +212,134 @@ func (t *TMDbProvider) getJSON(ctx context.Context, url string, out any) error {
|
||||
}
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
|
||||
// TMDbDetails holds extended metadata from the /movie/{id} or /tv/{id} endpoints.
|
||||
type TMDbDetails struct {
|
||||
Languages []string `json:"languages"`
|
||||
Countries []string `json:"countries"`
|
||||
Genres []string `json:"genres"`
|
||||
}
|
||||
|
||||
// GetDetails fetches extended metadata for a TMDb ID.
|
||||
// It calls /movie/{id} or /tv/{id} with append_to_response=genres
|
||||
// and extracts languages, production countries, and genres.
|
||||
// mediaType should be "movie" or "tv".
|
||||
func (t *TMDbProvider) GetDetails(ctx context.Context, tmdbID int, mediaType string) (*TMDbDetails, error) {
|
||||
apiKey := t.resolveAPIKey(ctx)
|
||||
if apiKey == "" {
|
||||
return nil, fmt.Errorf("tmdb: no API key available")
|
||||
}
|
||||
base := t.resolveBaseURL(ctx)
|
||||
|
||||
path := "/movie/" + fmt.Sprint(tmdbID)
|
||||
if mediaType == "tv" {
|
||||
path = "/tv/" + fmt.Sprint(tmdbID)
|
||||
}
|
||||
|
||||
q := url.Values{}
|
||||
q.Set("api_key", apiKey)
|
||||
q.Set("language", "zh-CN")
|
||||
q.Set("append_to_response", "genres")
|
||||
u := base + path + "?" + q.Encode()
|
||||
|
||||
// Response structs for /movie/{id} and /tv/{id}
|
||||
type genre struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
type movieResult struct {
|
||||
OriginalLanguage string `json:"original_language"`
|
||||
ProductionCountries []struct {
|
||||
Iso3166_1 string `json:"iso_3166_1"`
|
||||
} `json:"production_countries"`
|
||||
SpokenLanguages []struct {
|
||||
Iso639_1 string `json:"iso_639_1"`
|
||||
} `json:"spoken_languages"`
|
||||
Genres []genre `json:"genres"`
|
||||
}
|
||||
type tvResult struct {
|
||||
OriginCountry []string `json:"origin_country"`
|
||||
SpokenLanguages []struct {
|
||||
Iso639_1 string `json:"iso_639_1"`
|
||||
} `json:"spoken_languages"`
|
||||
Genres []genre `json:"genres"`
|
||||
}
|
||||
|
||||
var (
|
||||
languages []string
|
||||
countries []string
|
||||
genres []string
|
||||
)
|
||||
|
||||
if mediaType == "tv" {
|
||||
var r tvResult
|
||||
if err := t.getJSON(ctx, u, &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Spoken languages
|
||||
for _, l := range r.SpokenLanguages {
|
||||
languages = append(languages, l.Iso639_1)
|
||||
}
|
||||
// Origin countries
|
||||
countries = append(countries, r.OriginCountry...)
|
||||
// Genres
|
||||
for _, g := range r.Genres {
|
||||
genres = append(genres, g.Name)
|
||||
}
|
||||
} else {
|
||||
var r movieResult
|
||||
if err := t.getJSON(ctx, u, &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Original language
|
||||
if r.OriginalLanguage != "" {
|
||||
languages = append(languages, r.OriginalLanguage)
|
||||
}
|
||||
// Spoken languages
|
||||
for _, l := range r.SpokenLanguages {
|
||||
languages = append(languages, l.Iso639_1)
|
||||
}
|
||||
// Production countries
|
||||
for _, c := range r.ProductionCountries {
|
||||
countries = append(countries, c.Iso3166_1)
|
||||
}
|
||||
// Genres
|
||||
for _, g := range r.Genres {
|
||||
genres = append(genres, g.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate
|
||||
languages = deduplicate(languages)
|
||||
countries = deduplicate(countries)
|
||||
genres = deduplicate(genres)
|
||||
|
||||
t.log.Debug("tmdb: getDetails",
|
||||
zap.Int("tmdb_id", tmdbID),
|
||||
zap.String("type", mediaType),
|
||||
zap.Strings("languages", languages),
|
||||
zap.Strings("countries", countries),
|
||||
zap.Strings("genres", genres),
|
||||
)
|
||||
|
||||
return &TMDbDetails{
|
||||
Languages: languages,
|
||||
Countries: countries,
|
||||
Genres: genres,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// deduplicate removes duplicates from a string slice.
|
||||
func deduplicate(s []string) []string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
}
|
||||
seen := make(map[string]bool, len(s))
|
||||
out := make([]string, 0, len(s))
|
||||
for _, v := range s {
|
||||
if !seen[v] {
|
||||
seen[v] = true
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user