feat: Update player API to use id_type in path

All Russian players now use format: /players/{player}/{id_type}/{id}
- id_type can be kp (Kinopoisk) or imdb
- Alloha, Lumex, Vibix, HDVB support both ID types
- Added validation for id_type parameter
- Updated handlers to parse id_type from path
This commit is contained in:
2025-10-18 20:21:13 +00:00
parent 43ca411897
commit b5d9f3c57d
12 changed files with 835 additions and 85 deletions

261
pkg/services/kinopoisk.go Normal file
View File

@@ -0,0 +1,261 @@
package services
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
type KinopoiskService struct {
apiKey string
baseURL string
client *http.Client
}
type KPFilm struct {
KinopoiskId int `json:"kinopoiskId"`
ImdbId string `json:"imdbId"`
NameRu string `json:"nameRu"`
NameEn string `json:"nameEn"`
NameOriginal string `json:"nameOriginal"`
PosterUrl string `json:"posterUrl"`
PosterUrlPreview string `json:"posterUrlPreview"`
CoverUrl string `json:"coverUrl"`
LogoUrl string `json:"logoUrl"`
ReviewsCount int `json:"reviewsCount"`
RatingGoodReview float64 `json:"ratingGoodReview"`
RatingGoodReviewVoteCount int `json:"ratingGoodReviewVoteCount"`
RatingKinopoisk float64 `json:"ratingKinopoisk"`
RatingKinopoiskVoteCount int `json:"ratingKinopoiskVoteCount"`
RatingImdb float64 `json:"ratingImdb"`
RatingImdbVoteCount int `json:"ratingImdbVoteCount"`
RatingFilmCritics float64 `json:"ratingFilmCritics"`
RatingFilmCriticsVoteCount int `json:"ratingFilmCriticsVoteCount"`
RatingAwait float64 `json:"ratingAwait"`
RatingAwaitCount int `json:"ratingAwaitCount"`
RatingRfCritics float64 `json:"ratingRfCritics"`
RatingRfCriticsVoteCount int `json:"ratingRfCriticsVoteCount"`
WebUrl string `json:"webUrl"`
Year int `json:"year"`
FilmLength int `json:"filmLength"`
Slogan string `json:"slogan"`
Description string `json:"description"`
ShortDescription string `json:"shortDescription"`
EditorAnnotation string `json:"editorAnnotation"`
IsTicketsAvailable bool `json:"isTicketsAvailable"`
ProductionStatus string `json:"productionStatus"`
Type string `json:"type"`
RatingMpaa string `json:"ratingMpaa"`
RatingAgeLimits string `json:"ratingAgeLimits"`
HasImax bool `json:"hasImax"`
Has3D bool `json:"has3d"`
LastSync string `json:"lastSync"`
Countries []struct {
Country string `json:"country"`
} `json:"countries"`
Genres []struct {
Genre string `json:"genre"`
} `json:"genres"`
StartYear int `json:"startYear"`
EndYear int `json:"endYear"`
Serial bool `json:"serial"`
ShortFilm bool `json:"shortFilm"`
Completed bool `json:"completed"`
}
type KPSearchResponse struct {
Keyword string `json:"keyword"`
PagesCount int `json:"pagesCount"`
Films []KPFilmShort `json:"films"`
SearchFilmsCountResult int `json:"searchFilmsCountResult"`
}
type KPFilmShort struct {
FilmId int `json:"filmId"`
NameRu string `json:"nameRu"`
NameEn string `json:"nameEn"`
Type string `json:"type"`
Year string `json:"year"`
Description string `json:"description"`
FilmLength string `json:"filmLength"`
Countries []KPCountry `json:"countries"`
Genres []KPGenre `json:"genres"`
Rating string `json:"rating"`
RatingVoteCount int `json:"ratingVoteCount"`
PosterUrl string `json:"posterUrl"`
PosterUrlPreview string `json:"posterUrlPreview"`
}
type KPCountry struct {
Country string `json:"country"`
}
type KPGenre struct {
Genre string `json:"genre"`
}
type KPExternalSource struct {
Source string `json:"source"`
ID string `json:"id"`
}
func NewKinopoiskService(apiKey, baseURL string) *KinopoiskService {
return &KinopoiskService{
apiKey: apiKey,
baseURL: baseURL,
client: &http.Client{Timeout: 10 * time.Second},
}
}
func (s *KinopoiskService) makeRequest(endpoint string, target interface{}) error {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
req.Header.Set("X-API-KEY", s.apiKey)
req.Header.Set("Accept", "application/json")
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Kinopoisk API error: %d", resp.StatusCode)
}
return json.NewDecoder(resp.Body).Decode(target)
}
func (s *KinopoiskService) GetFilmByKinopoiskId(id int) (*KPFilm, error) {
endpoint := fmt.Sprintf("%s/v2.2/films/%d", s.baseURL, id)
var film KPFilm
err := s.makeRequest(endpoint, &film)
return &film, err
}
func (s *KinopoiskService) GetFilmByImdbId(imdbId string) (*KPFilm, error) {
endpoint := fmt.Sprintf("%s/v2.2/films?imdbId=%s", s.baseURL, imdbId)
var response struct {
Films []KPFilm `json:"items"`
}
err := s.makeRequest(endpoint, &response)
if err != nil {
return nil, err
}
if len(response.Films) == 0 {
return nil, fmt.Errorf("film not found")
}
return &response.Films[0], nil
}
func (s *KinopoiskService) SearchFilms(keyword string, page int) (*KPSearchResponse, error) {
endpoint := fmt.Sprintf("%s/v2.1/films/search-by-keyword?keyword=%s&page=%d", s.baseURL, keyword, page)
var response KPSearchResponse
err := s.makeRequest(endpoint, &response)
return &response, err
}
func (s *KinopoiskService) GetExternalSources(kinopoiskId int) ([]KPExternalSource, error) {
endpoint := fmt.Sprintf("%s/v2.2/films/%d/external_sources", s.baseURL, kinopoiskId)
var response struct {
Items []KPExternalSource `json:"items"`
}
err := s.makeRequest(endpoint, &response)
if err != nil {
return nil, err
}
return response.Items, nil
}
func (s *KinopoiskService) GetTopFilms(topType string, page int) (*KPSearchResponse, error) {
endpoint := fmt.Sprintf("%s/v2.2/films/top?type=%s&page=%d", s.baseURL, topType, page)
var response struct {
PagesCount int `json:"pagesCount"`
Films []KPFilmShort `json:"films"`
}
err := s.makeRequest(endpoint, &response)
if err != nil {
return nil, err
}
return &KPSearchResponse{
PagesCount: response.PagesCount,
Films: response.Films,
SearchFilmsCountResult: len(response.Films),
}, nil
}
func KPIdToImdbId(kpService *KinopoiskService, kpId int) (string, error) {
film, err := kpService.GetFilmByKinopoiskId(kpId)
if err != nil {
return "", err
}
return film.ImdbId, nil
}
func ImdbIdToKPId(kpService *KinopoiskService, imdbId string) (int, error) {
film, err := kpService.GetFilmByImdbId(imdbId)
if err != nil {
return 0, err
}
return film.KinopoiskId, nil
}
func TmdbIdToKPId(tmdbService *TMDBService, kpService *KinopoiskService, tmdbId int) (int, error) {
externalIds, err := tmdbService.GetMovieExternalIDs(tmdbId)
if err != nil {
return 0, err
}
if externalIds.ImdbID == "" {
return 0, fmt.Errorf("no IMDb ID found for TMDB ID %d", tmdbId)
}
return ImdbIdToKPId(kpService, externalIds.ImdbID)
}
func KPIdToTmdbId(tmdbService *TMDBService, kpService *KinopoiskService, kpId int) (int, error) {
imdbId, err := KPIdToImdbId(kpService, kpId)
if err != nil {
return 0, err
}
movies, err := tmdbService.SearchMovies("", 1, "en-US", "", 0)
if err != nil {
return 0, err
}
for _, movie := range movies.Results {
ids, err := tmdbService.GetMovieExternalIDs(movie.ID)
if err != nil {
continue
}
if ids.ImdbID == imdbId {
return movie.ID, nil
}
}
return 0, fmt.Errorf("TMDB ID not found for KP ID %d", kpId)
}
func ConvertKPRating(rating float64) float64 {
return rating
}
func FormatKPYear(year int) string {
return strconv.Itoa(year)
}

309
pkg/services/kp_mapper.go Normal file
View File

@@ -0,0 +1,309 @@
package services
import (
"fmt"
"strconv"
"strings"
"time"
"neomovies-api/pkg/models"
)
func MapKPFilmToTMDBMovie(kpFilm *KPFilm) *models.Movie {
if kpFilm == nil {
return nil
}
releaseDate := ""
if kpFilm.Year > 0 {
releaseDate = fmt.Sprintf("%d-01-01", kpFilm.Year)
}
genres := make([]models.Genre, 0)
for _, g := range kpFilm.Genres {
genres = append(genres, models.Genre{
ID: 0,
Name: g.Genre,
})
}
countries := make([]models.ProductionCountry, 0)
for _, c := range kpFilm.Countries {
countries = append(countries, models.ProductionCountry{
ISO31661: "",
Name: c.Country,
})
}
posterPath := ""
if kpFilm.PosterUrlPreview != "" {
posterPath = kpFilm.PosterUrlPreview
} else if kpFilm.PosterUrl != "" {
posterPath = kpFilm.PosterUrl
}
backdropPath := ""
if kpFilm.CoverUrl != "" {
backdropPath = kpFilm.CoverUrl
}
overview := kpFilm.Description
if overview == "" {
overview = kpFilm.ShortDescription
}
title := kpFilm.NameRu
if title == "" {
title = kpFilm.NameEn
}
if title == "" {
title = kpFilm.NameOriginal
}
originalTitle := kpFilm.NameOriginal
if originalTitle == "" {
originalTitle = kpFilm.NameEn
}
return &models.Movie{
ID: kpFilm.KinopoiskId,
Title: title,
OriginalTitle: originalTitle,
Overview: overview,
PosterPath: posterPath,
BackdropPath: backdropPath,
ReleaseDate: releaseDate,
VoteAverage: kpFilm.RatingKinopoisk,
VoteCount: kpFilm.RatingKinopoiskVoteCount,
Popularity: float64(kpFilm.RatingKinopoisk * 100),
Adult: false,
OriginalLanguage: detectLanguage(kpFilm),
Runtime: kpFilm.FilmLength,
Genres: genres,
Tagline: kpFilm.Slogan,
ProductionCountries: countries,
ImdbID: kpFilm.ImdbId,
KinopoiskID: kpFilm.KinopoiskId,
}
}
func MapKPFilmToTVShow(kpFilm *KPFilm) *models.TVShow {
if kpFilm == nil {
return nil
}
firstAirDate := ""
if kpFilm.StartYear > 0 {
firstAirDate = fmt.Sprintf("%d-01-01", kpFilm.StartYear)
}
lastAirDate := ""
if kpFilm.EndYear > 0 {
lastAirDate = fmt.Sprintf("%d-01-01", kpFilm.EndYear)
}
genres := make([]models.Genre, 0)
for _, g := range kpFilm.Genres {
genres = append(genres, models.Genre{
ID: 0,
Name: g.Genre,
})
}
posterPath := ""
if kpFilm.PosterUrlPreview != "" {
posterPath = kpFilm.PosterUrlPreview
} else if kpFilm.PosterUrl != "" {
posterPath = kpFilm.PosterUrl
}
backdropPath := ""
if kpFilm.CoverUrl != "" {
backdropPath = kpFilm.CoverUrl
}
overview := kpFilm.Description
if overview == "" {
overview = kpFilm.ShortDescription
}
name := kpFilm.NameRu
if name == "" {
name = kpFilm.NameEn
}
if name == "" {
name = kpFilm.NameOriginal
}
originalName := kpFilm.NameOriginal
if originalName == "" {
originalName = kpFilm.NameEn
}
status := "Ended"
if kpFilm.Completed {
status = "Ended"
} else {
status = "Returning Series"
}
return &models.TVShow{
ID: kpFilm.KinopoiskId,
Name: name,
OriginalName: originalName,
Overview: overview,
PosterPath: posterPath,
BackdropPath: backdropPath,
FirstAirDate: firstAirDate,
LastAirDate: lastAirDate,
VoteAverage: kpFilm.RatingKinopoisk,
VoteCount: kpFilm.RatingKinopoiskVoteCount,
Popularity: float64(kpFilm.RatingKinopoisk * 100),
OriginalLanguage: detectLanguage(kpFilm),
Genres: genres,
Tagline: kpFilm.Slogan,
Status: status,
InProduction: !kpFilm.Completed,
ImdbID: kpFilm.ImdbId,
KinopoiskID: kpFilm.KinopoiskId,
}
}
func MapKPSearchToTMDBResponse(kpSearch *KPSearchResponse) *models.TMDBResponse {
if kpSearch == nil {
return &models.TMDBResponse{
Page: 1,
Results: []models.Movie{},
TotalPages: 0,
TotalResults: 0,
}
}
results := make([]models.Movie, 0)
for _, film := range kpSearch.Films {
movie := mapKPFilmShortToMovie(film)
if movie != nil {
results = append(results, *movie)
}
}
totalPages := kpSearch.PagesCount
if totalPages == 0 && len(results) > 0 {
totalPages = 1
}
return &models.TMDBResponse{
Page: 1,
Results: results,
TotalPages: totalPages,
TotalResults: kpSearch.SearchFilmsCountResult,
}
}
func mapKPFilmShortToMovie(film KPFilmShort) *models.Movie {
genres := make([]models.Genre, 0)
for _, g := range film.Genres {
genres = append(genres, models.Genre{
ID: 0,
Name: g.Genre,
})
}
year := 0
if film.Year != "" {
year, _ = strconv.Atoi(film.Year)
}
releaseDate := ""
if year > 0 {
releaseDate = fmt.Sprintf("%d-01-01", year)
}
posterPath := film.PosterUrlPreview
if posterPath == "" {
posterPath = film.PosterUrl
}
title := film.NameRu
if title == "" {
title = film.NameEn
}
originalTitle := film.NameEn
if originalTitle == "" {
originalTitle = film.NameRu
}
rating := 0.0
if film.Rating != "" {
rating, _ = strconv.ParseFloat(film.Rating, 64)
}
return &models.Movie{
ID: film.FilmId,
Title: title,
OriginalTitle: originalTitle,
Overview: film.Description,
PosterPath: posterPath,
ReleaseDate: releaseDate,
VoteAverage: rating,
VoteCount: film.RatingVoteCount,
Popularity: rating * 100,
Genres: genres,
KinopoiskID: film.FilmId,
}
}
func detectLanguage(film *KPFilm) string {
if film.NameRu != "" {
return "ru"
}
if film.NameEn != "" {
return "en"
}
return "ru"
}
func MapKPExternalIDsToTMDB(kpFilm *KPFilm) *models.ExternalIDs {
if kpFilm == nil {
return &models.ExternalIDs{}
}
return &models.ExternalIDs{
ID: kpFilm.KinopoiskId,
ImdbID: kpFilm.ImdbId,
KinopoiskID: kpFilm.KinopoiskId,
}
}
func ShouldUseKinopoisk(language string) bool {
if language == "" {
return false
}
lang := strings.ToLower(language)
return strings.HasPrefix(lang, "ru")
}
func NormalizeLanguage(language string) string {
if language == "" {
return "en-US"
}
lang := strings.ToLower(language)
if strings.HasPrefix(lang, "ru") {
return "ru-RU"
}
return "en-US"
}
func ConvertKPRatingToTMDB(kpRating float64) float64 {
return kpRating
}
func FormatKPDate(year int) string {
if year <= 0 {
return time.Now().Format("2006-01-02")
}
return fmt.Sprintf("%d-01-01", year)
}

View File

@@ -7,28 +7,54 @@ import (
)
type MovieService struct {
tmdb *TMDBService
tmdb *TMDBService
kpService *KinopoiskService
}
func NewMovieService(db *mongo.Database, tmdb *TMDBService) *MovieService {
func NewMovieService(db *mongo.Database, tmdb *TMDBService, kpService *KinopoiskService) *MovieService {
return &MovieService{
tmdb: tmdb,
tmdb: tmdb,
kpService: kpService,
}
}
func (s *MovieService) Search(query string, page int, language, region string, year int) (*models.TMDBResponse, error) {
if ShouldUseKinopoisk(language) && s.kpService != nil {
kpSearch, err := s.kpService.SearchFilms(query, page)
if err == nil {
return MapKPSearchToTMDBResponse(kpSearch), nil
}
}
return s.tmdb.SearchMovies(query, page, language, region, year)
}
func (s *MovieService) GetByID(id int, language string) (*models.Movie, error) {
if ShouldUseKinopoisk(language) && s.kpService != nil {
kpFilm, err := s.kpService.GetFilmByKinopoiskId(id)
if err == nil {
return MapKPFilmToTMDBMovie(kpFilm), nil
}
}
return s.tmdb.GetMovie(id, language)
}
func (s *MovieService) GetPopular(page int, language, region string) (*models.TMDBResponse, error) {
if ShouldUseKinopoisk(language) && s.kpService != nil {
kpTop, err := s.kpService.GetTopFilms("TOP_100_POPULAR_FILMS", page)
if err == nil {
return MapKPSearchToTMDBResponse(kpTop), nil
}
}
return s.tmdb.GetPopularMovies(page, language, region)
}
func (s *MovieService) GetTopRated(page int, language, region string) (*models.TMDBResponse, error) {
if ShouldUseKinopoisk(language) && s.kpService != nil {
kpTop, err := s.kpService.GetTopFilms("TOP_250_BEST_FILMS", page)
if err == nil {
return MapKPSearchToTMDBResponse(kpTop), nil
}
}
return s.tmdb.GetTopRatedMovies(page, language, region)
}
@@ -49,5 +75,17 @@ func (s *MovieService) GetSimilar(id, page int, language string) (*models.TMDBRe
}
func (s *MovieService) GetExternalIDs(id int) (*models.ExternalIDs, error) {
return s.tmdb.GetMovieExternalIDs(id)
tmdbIDs, err := s.tmdb.GetMovieExternalIDs(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
}

View File

@@ -7,14 +7,16 @@ import (
)
type TVService struct {
db *mongo.Database
tmdb *TMDBService
db *mongo.Database
tmdb *TMDBService
kpService *KinopoiskService
}
func NewTVService(db *mongo.Database, tmdb *TMDBService) *TVService {
func NewTVService(db *mongo.Database, tmdb *TMDBService, kpService *KinopoiskService) *TVService {
return &TVService{
db: db,
tmdb: tmdb,
db: db,
tmdb: tmdb,
kpService: kpService,
}
}
@@ -23,6 +25,12 @@ func (s *TVService) Search(query string, page int, language string, year int) (*
}
func (s *TVService) GetByID(id int, language string) (*models.TVShow, error) {
if ShouldUseKinopoisk(language) && s.kpService != nil {
kpFilm, err := s.kpService.GetFilmByKinopoiskId(id)
if err == nil && kpFilm != nil {
return MapKPFilmToTVShow(kpFilm), nil
}
}
return s.tmdb.GetTVShow(id, language)
}