package handlers import ( "encoding/json" "fmt" "io" "log" "net/http" "net/url" "strconv" "strings" "time" "github.com/gorilla/mux" "neomovies-api/pkg/config" "neomovies-api/pkg/players" ) type PlayersHandler struct { config *config.Config } func NewPlayersHandler(cfg *config.Config) *PlayersHandler { return &PlayersHandler{ config: cfg, } } func (h *PlayersHandler) GetAllohaPlayer(w http.ResponseWriter, r *http.Request) { log.Printf("GetAllohaPlayer called: %s %s", r.Method, r.URL.Path) vars := mux.Vars(r) idType := vars["id_type"] id := vars["id"] if idType == "" || id == "" { log.Printf("Error: id_type or id is empty") http.Error(w, "id_type and id are required", http.StatusBadRequest) return } if idType == "kinopoisk_id" { idType = "kp" } if idType != "kp" && idType != "imdb" { log.Printf("Error: invalid id_type: %s", idType) http.Error(w, "id_type must be 'kp' (kinopoisk_id) or 'imdb'", http.StatusBadRequest) return } log.Printf("Processing %s ID: %s", idType, id) if h.config.AllohaToken == "" { log.Printf("Error: ALLOHA_TOKEN is missing") http.Error(w, "Server misconfiguration: ALLOHA_TOKEN missing", http.StatusInternalServerError) return } idParam := fmt.Sprintf("%s=%s", idType, url.QueryEscape(id)) apiURL := fmt.Sprintf("https://api.alloha.tv/?token=%s&%s", h.config.AllohaToken, idParam) log.Printf("Calling Alloha API: %s", apiURL) resp, err := http.Get(apiURL) if err != nil { log.Printf("Error calling Alloha API: %v", err) http.Error(w, "Failed to fetch from Alloha API", http.StatusInternalServerError) return } defer resp.Body.Close() log.Printf("Alloha API response status: %d", resp.StatusCode) if resp.StatusCode != http.StatusOK { http.Error(w, fmt.Sprintf("Alloha API error: %d", resp.StatusCode), http.StatusBadGateway) return } body, err := io.ReadAll(resp.Body) if err != nil { log.Printf("Error reading Alloha response: %v", err) http.Error(w, "Failed to read Alloha response", http.StatusInternalServerError) return } log.Printf("Alloha API response body: %s", string(body)) var allohaResponse struct { Status string `json:"status"` Data struct { Iframe string `json:"iframe"` } `json:"data"` } if err := json.Unmarshal(body, &allohaResponse); err != nil { log.Printf("Error unmarshaling JSON: %v", err) http.Error(w, "Invalid JSON from Alloha", http.StatusBadGateway) return } if allohaResponse.Status != "success" || allohaResponse.Data.Iframe == "" { log.Printf("Video not found or empty iframe") http.Error(w, "Video not found", http.StatusNotFound) return } // Получаем параметры для сериалов season := r.URL.Query().Get("season") episode := r.URL.Query().Get("episode") translation := r.URL.Query().Get("translation") if translation == "" { translation = "66" // дефолтная озвучка } // Используем iframe URL из API iframeCode := allohaResponse.Data.Iframe // Если это не HTML код, а просто URL var playerURL string if !strings.Contains(iframeCode, "<") { playerURL = iframeCode // Добавляем параметры для сериалов if season != "" && episode != "" { separator := "?" if strings.Contains(playerURL, "?") { separator = "&" } playerURL = fmt.Sprintf("%s%sseason=%s&episode=%s&translation=%s", playerURL, separator, season, episode, translation) } iframeCode = fmt.Sprintf(``, playerURL) } htmlDoc := fmt.Sprintf(`