mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-27 17:38:51 +05:00
fix(api): add TMDB ID enrichment for KP content via IMDB ID lookup
- Add TMDBID field to ExternalIDs model - Enhance GetExternalIDs in MovieService and TVService to fetch TMDB ID via FindTMDBIdByIMDB - Add EnrichKPWithTMDBID function to enrich unified content with TMDB IDs - Update unified handlers to automatically enrich KP content with TMDB IDs - Enrich search results with TMDB IDs by fetching full film data for each result This ensures that when using source=kp, the response includes TMDB IDs in externalIds when available through IMDB ID mapping, while preserving all original KP data.
This commit is contained in:
@@ -61,6 +61,10 @@ func (h *UnifiedHandler) GetMovie(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
data = services.MapKPToUnified(kpFilm)
|
||||
// Обогащаем TMDB ID если есть IMDB ID
|
||||
if h.tmdb != nil {
|
||||
services.EnrichKPWithTMDBID(data, h.tmdb)
|
||||
}
|
||||
} else {
|
||||
// tmdb
|
||||
movie, err := h.tmdb.GetMovie(id, language)
|
||||
@@ -99,6 +103,10 @@ func (h *UnifiedHandler) GetTV(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
data = services.MapKPToUnified(kpFilm)
|
||||
// Обогащаем TMDB ID если есть IMDB ID
|
||||
if h.tmdb != nil {
|
||||
services.EnrichKPWithTMDBID(data, h.tmdb)
|
||||
}
|
||||
} else {
|
||||
tv, err := h.tmdb.GetTVShow(id, language)
|
||||
if err != nil {
|
||||
@@ -139,6 +147,23 @@ func (h *UnifiedHandler) Search(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
items := services.MapKPSearchToUnifiedItems(kpSearch)
|
||||
// Обогащаем результаты поиска TMDB ID через получение полной информации о фильмах
|
||||
if h.tmdb != nil {
|
||||
for i := range items {
|
||||
if kpID, err := strconv.Atoi(items[i].ID); err == nil {
|
||||
if kpFilm, err := h.kp.GetFilmByKinopoiskId(kpID); err == nil && kpFilm.ImdbId != "" {
|
||||
items[i].ExternalIDs.IMDb = kpFilm.ImdbId
|
||||
mediaType := "movie"
|
||||
if items[i].Type == "tv" {
|
||||
mediaType = "tv"
|
||||
}
|
||||
if tmdbID, err := h.tmdb.FindTMDBIdByIMDB(kpFilm.ImdbId, mediaType, "ru-RU"); err == nil {
|
||||
items[i].ExternalIDs.TMDB = &tmdbID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
resp := models.UnifiedSearchResponse{
|
||||
Success: true,
|
||||
Data: items,
|
||||
|
||||
@@ -123,6 +123,7 @@ type ExternalIDs struct {
|
||||
ID int `json:"id"`
|
||||
IMDbID string `json:"imdb_id"`
|
||||
KinopoiskID int `json:"kinopoisk_id,omitempty"`
|
||||
TMDBID int `json:"tmdb_id,omitempty"`
|
||||
TVDBID int `json:"tvdb_id,omitempty"`
|
||||
WikidataID string `json:"wikidata_id"`
|
||||
FacebookID string `json:"facebook_id"`
|
||||
|
||||
@@ -388,3 +388,37 @@ func FormatKPDate(year int) string {
|
||||
}
|
||||
return fmt.Sprintf("%d-01-01", year)
|
||||
}
|
||||
|
||||
// EnrichKPWithTMDBID обогащает KP контент TMDB ID через IMDB ID
|
||||
func EnrichKPWithTMDBID(content *models.UnifiedContent, tmdbService *TMDBService) {
|
||||
if content == nil || content.IMDbID == "" || content.ExternalIDs.TMDB != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mediaType := "movie"
|
||||
if content.Type == "tv" {
|
||||
mediaType = "tv"
|
||||
}
|
||||
|
||||
if tmdbID, err := tmdbService.FindTMDBIdByIMDB(content.IMDbID, mediaType, "ru-RU"); err == nil {
|
||||
content.ExternalIDs.TMDB = &tmdbID
|
||||
}
|
||||
}
|
||||
|
||||
// EnrichKPSearchItemsWithTMDBID обогащает массив поисковых элементов TMDB ID
|
||||
func EnrichKPSearchItemsWithTMDBID(items []models.UnifiedSearchItem, tmdbService *TMDBService) {
|
||||
for i := range items {
|
||||
if items[i].ExternalIDs.IMDb == "" || items[i].ExternalIDs.TMDB != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
mediaType := "movie"
|
||||
if items[i].Type == "tv" {
|
||||
mediaType = "tv"
|
||||
}
|
||||
|
||||
if tmdbID, err := tmdbService.FindTMDBIdByIMDB(items[i].ExternalIDs.IMDb, mediaType, "ru-RU"); err == nil {
|
||||
items[i].ExternalIDs.TMDB = &tmdbID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +122,14 @@ func (s *MovieService) GetExternalIDs(id int) (*models.ExternalIDs, error) {
|
||||
if err == nil && kpFilm != nil {
|
||||
externalIDs := MapKPExternalIDsToTMDB(kpFilm)
|
||||
externalIDs.ID = id
|
||||
|
||||
// Пытаемся получить TMDB ID через IMDB ID
|
||||
if kpFilm.ImdbId != "" && s.tmdb != nil {
|
||||
if tmdbID, tmdbErr := s.tmdb.FindTMDBIdByIMDB(kpFilm.ImdbId, "movie", "ru-RU"); tmdbErr == nil {
|
||||
externalIDs.TMDBID = tmdbID
|
||||
}
|
||||
}
|
||||
|
||||
return externalIDs, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,5 +86,34 @@ func (s *TVService) GetSimilar(id, page int, language string) (*models.TMDBTVRes
|
||||
}
|
||||
|
||||
func (s *TVService) GetExternalIDs(id int) (*models.ExternalIDs, error) {
|
||||
return s.tmdb.GetTVExternalIDs(id)
|
||||
if s.kpService != nil {
|
||||
kpFilm, err := s.kpService.GetFilmByKinopoiskId(id)
|
||||
if err == nil && kpFilm != nil {
|
||||
externalIDs := MapKPExternalIDsToTMDB(kpFilm)
|
||||
externalIDs.ID = id
|
||||
|
||||
// Пытаемся получить TMDB ID через IMDB ID
|
||||
if kpFilm.ImdbId != "" && s.tmdb != nil {
|
||||
if tmdbID, tmdbErr := s.tmdb.FindTMDBIdByIMDB(kpFilm.ImdbId, "tv", "ru-RU"); tmdbErr == nil {
|
||||
externalIDs.TMDBID = tmdbID
|
||||
}
|
||||
}
|
||||
|
||||
return externalIDs, nil
|
||||
}
|
||||
}
|
||||
|
||||
tmdbIDs, err := s.tmdb.GetTVExternalIDs(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if s.kpService != nil && tmdbIDs.IMDbID != "" {
|
||||
kpFilm, err := s.kpService.GetFilmByImdbId(tmdbIDs.IMDbID)
|
||||
if err == nil && kpFilm != nil {
|
||||
tmdbIDs.KinopoiskID = kpFilm.KinopoiskId
|
||||
}
|
||||
}
|
||||
|
||||
return tmdbIDs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user