mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-12-18 13:36:09 +05:00
fixes
This commit is contained in:
@@ -3,6 +3,7 @@ package services
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -74,16 +75,28 @@ type KPSearchResponse struct {
|
||||
}
|
||||
|
||||
type KPFilmShort struct {
|
||||
// Old format fields
|
||||
FilmId int `json:"filmId"`
|
||||
// New format fields
|
||||
KinopoiskId int `json:"kinopoiskId"`
|
||||
|
||||
NameRu string `json:"nameRu"`
|
||||
NameEn string `json:"nameEn"`
|
||||
NameOriginal string `json:"nameOriginal"`
|
||||
ImdbId string `json:"imdbId"`
|
||||
Type string `json:"type"`
|
||||
Year string `json:"year"`
|
||||
Year int `json:"year"` // Changed from string to int
|
||||
Description string `json:"description"`
|
||||
FilmLength string `json:"filmLength"`
|
||||
Countries []KPCountry `json:"countries"`
|
||||
Genres []KPGenre `json:"genres"`
|
||||
|
||||
// Old format rating field
|
||||
Rating string `json:"rating"`
|
||||
// New format rating fields
|
||||
RatingKinopoisk float64 `json:"ratingKinopoisk"`
|
||||
RatingImdb float64 `json:"ratingImdb"`
|
||||
|
||||
RatingVoteCount int `json:"ratingVoteCount"`
|
||||
PosterUrl string `json:"posterUrl"`
|
||||
PosterUrlPreview string `json:"posterUrlPreview"`
|
||||
@@ -113,19 +126,29 @@ func NewKinopoiskService(apiKey, baseURL string) *KinopoiskService {
|
||||
func (s *KinopoiskService) makeRequest(endpoint string, target interface{}) error {
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
log.Printf("[Kinopoisk] makeRequest error creating request: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Log API key status
|
||||
if s.apiKey == "" {
|
||||
log.Printf("[Kinopoisk] ⚠️ API Key is EMPTY!")
|
||||
} else {
|
||||
log.Printf("[Kinopoisk] Using API Key (first 10 chars): %s...", s.apiKey[:10])
|
||||
}
|
||||
|
||||
req.Header.Set("X-API-KEY", s.apiKey)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("[Kinopoisk] makeRequest HTTP error: %v", err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Printf("[Kinopoisk] makeRequest status code: %d for endpoint: %s", resp.StatusCode, endpoint)
|
||||
return fmt.Errorf("Kinopoisk API error: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
@@ -165,6 +188,57 @@ func (s *KinopoiskService) SearchFilms(keyword string, page int) (*KPSearchRespo
|
||||
return &response, err
|
||||
}
|
||||
|
||||
// GetPopularFilms tries to get popular films using filters API
|
||||
// This is an alternative to GetTopFilms which may not work anymore
|
||||
func (s *KinopoiskService) GetPopularFilms(page int) (*KPSearchResponse, error) {
|
||||
// Try using the filters API with popularity sort
|
||||
endpoint := fmt.Sprintf("%s/v2.2/films?sortField=num_vote&sortType=DESC&page=%d", s.baseURL, page)
|
||||
log.Printf("[Kinopoisk] GetPopularFilms (via filters): %s", endpoint)
|
||||
|
||||
// Try new format first (items/total)
|
||||
var responseNew struct {
|
||||
Total int `json:"total"`
|
||||
TotalPages int `json:"totalPages"`
|
||||
Items []KPFilmShort `json:"items"`
|
||||
}
|
||||
|
||||
err := s.makeRequest(endpoint, &responseNew)
|
||||
if err != nil {
|
||||
log.Printf("[Kinopoisk] GetPopularFilms error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If we got items, use them
|
||||
if len(responseNew.Items) > 0 {
|
||||
log.Printf("[Kinopoisk] GetPopularFilms got %d films (new format)", len(responseNew.Items))
|
||||
return &KPSearchResponse{
|
||||
PagesCount: responseNew.TotalPages,
|
||||
Films: responseNew.Items,
|
||||
SearchFilmsCountResult: len(responseNew.Items),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Fallback to old format (films/pagesCount)
|
||||
var responseOld struct {
|
||||
PagesCount int `json:"pagesCount"`
|
||||
Films []KPFilmShort `json:"films"`
|
||||
}
|
||||
|
||||
err = s.makeRequest(endpoint, &responseOld)
|
||||
if err != nil {
|
||||
log.Printf("[Kinopoisk] GetPopularFilms error (old format): %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Printf("[Kinopoisk] GetPopularFilms got %d films (old format)", len(responseOld.Films))
|
||||
|
||||
return &KPSearchResponse{
|
||||
PagesCount: responseOld.PagesCount,
|
||||
Films: responseOld.Films,
|
||||
SearchFilmsCountResult: len(responseOld.Films),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *KinopoiskService) GetExternalSources(kinopoiskId int) ([]KPExternalSource, error) {
|
||||
endpoint := fmt.Sprintf("%s/v2.2/films/%d/external_sources", s.baseURL, kinopoiskId)
|
||||
|
||||
@@ -182,6 +256,7 @@ func (s *KinopoiskService) GetExternalSources(kinopoiskId int) ([]KPExternalSour
|
||||
|
||||
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)
|
||||
log.Printf("[Kinopoisk] GetTopFilms: %s", endpoint)
|
||||
|
||||
var response struct {
|
||||
PagesCount int `json:"pagesCount"`
|
||||
@@ -190,9 +265,17 @@ func (s *KinopoiskService) GetTopFilms(topType string, page int) (*KPSearchRespo
|
||||
|
||||
err := s.makeRequest(endpoint, &response)
|
||||
if err != nil {
|
||||
log.Printf("[Kinopoisk] GetTopFilms error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Printf("[Kinopoisk] GetTopFilms got %d films (pagesCount=%d)", len(response.Films), response.PagesCount)
|
||||
|
||||
// If no films returned, log warning
|
||||
if len(response.Films) == 0 {
|
||||
log.Printf("[Kinopoisk] ⚠️ GetTopFilms returned empty results for type=%s", topType)
|
||||
}
|
||||
|
||||
return &KPSearchResponse{
|
||||
PagesCount: response.PagesCount,
|
||||
Films: response.Films,
|
||||
|
||||
Reference in New Issue
Block a user