fix(players/alloha): parse map-based seasons/episodes structure and include translations

This commit is contained in:
Erno
2025-10-21 16:01:25 +00:00
parent 04fe3f3925
commit f42d548105

View File

@@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -171,24 +172,20 @@ func (h *PlayersHandler) GetAllohaMetaByKP(w http.ResponseWriter, r *http.Reques
return return
} }
// Define only the parts we need // Define only the parts we need (map-based structure as в примере)
var raw struct { var raw struct {
Status string `json:"status"` Status string `json:"status"`
Data struct { Data struct {
Seasons []struct { SeasonsCount int `json:"seasons_count"`
Key string `json:"key"` Seasons map[string]struct {
Value struct { Season int `json:"season"`
Episodes []struct { Episodes map[string]struct {
Key string `json:"key"` Episode int `json:"episode"`
Value struct { Translation map[string]struct {
Translation []struct {
Value struct {
Translation string `json:"translation"` Translation string `json:"translation"`
} `json:"value"` Name string `json:"name"`
} `json:"translation"` } `json:"translation"`
} `json:"value"`
} `json:"episodes"` } `json:"episodes"`
} `json:"value"`
} `json:"seasons"` } `json:"seasons"`
} `json:"data"` } `json:"data"`
} }
@@ -209,24 +206,50 @@ func (h *PlayersHandler) GetAllohaMetaByKP(w http.ResponseWriter, r *http.Reques
out := struct { out := struct {
Success bool `json:"success"` Success bool `json:"success"`
Seasons []seasonMeta `json:"seasons"` Seasons []seasonMeta `json:"seasons"`
}{Success: true} }{Success: true, Seasons: make([]seasonMeta, 0)}
for _, s := range raw.Data.Seasons { if raw.Status == "success" && len(raw.Data.Seasons) > 0 {
seasonNum, _ := strconv.Atoi(strings.TrimSpace(s.Key)) // sort seasons by numeric key
sm := seasonMeta{Season: seasonNum} seasonKeys := make([]int, 0, len(raw.Data.Seasons))
for _, e := range s.Value.Episodes { for k := range raw.Data.Seasons {
epNum, _ := strconv.Atoi(strings.TrimSpace(e.Key)) if n, err := strconv.Atoi(strings.TrimSpace(k)); err == nil {
em := episodeMeta{Episode: epNum} seasonKeys = append(seasonKeys, n)
for _, tr := range e.Value.Translation { }
t := strings.TrimSpace(tr.Value.Translation) }
sort.Ints(seasonKeys)
for _, sn := range seasonKeys {
s := raw.Data.Seasons[strconv.Itoa(sn)]
sm := seasonMeta{Season: sn}
// sort episodes by numeric key
epKeys := make([]int, 0, len(s.Episodes))
for ek := range s.Episodes {
if en, err := strconv.Atoi(strings.TrimSpace(ek)); err == nil {
epKeys = append(epKeys, en)
}
}
sort.Ints(epKeys)
for _, en := range epKeys {
e := s.Episodes[strconv.Itoa(en)]
em := episodeMeta{Episode: en}
// collect translations
for _, tr := range e.Translation {
t := strings.TrimSpace(tr.Translation)
if t == "" {
t = strings.TrimSpace(tr.Name)
}
if t != "" { if t != "" {
em.Translations = append(em.Translations, t) em.Translations = append(em.Translations, t)
} }
} }
sm.Episodes = append(sm.Episodes, em) sm.Episodes = append(sm.Episodes, em)
} }
out.Seasons = append(out.Seasons, sm) out.Seasons = append(out.Seasons, sm)
} }
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(out) _ = json.NewEncoder(w).Encode(out)