Skip to content

Commit 12c0200

Browse files
committed
Fix Season, credit season, person
1 parent 6ff0892 commit 12c0200

File tree

5 files changed

+790
-96
lines changed

5 files changed

+790
-96
lines changed

credits.go

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,19 @@ import (
66

77
// CreditsDetails type is a struct for credits JSON response.
88
type CreditsDetails struct {
9-
CreditType string `json:"credit_type"`
10-
Department string `json:"department"`
11-
Job string `json:"job"`
12-
Media struct {
13-
Adult bool `json:"adult,omitempty"` // Movie
14-
OriginalName string `json:"original_name,omitempty"` // TV
15-
OriginalTitle string `json:"original_title,omitempty"` // Movie
16-
ID int64 `json:"id"`
17-
Name string `json:"name,omitempty"` // TV
18-
FirstAirDate string `json:"first_air_date,omitempty"` // TV
19-
PosterPath string `json:"poster_path"`
20-
ReleaseDate string `json:"release_date,omitempty"` // Movie
21-
Title string `json:"title,omitempty"` // Movie
22-
Video bool `json:"video,omitempty"` // Movie
23-
GenreIDs []int64 `json:"genre_ids"`
24-
OriginalLanguage string `json:"original_language"`
25-
BackdropPath string `json:"backdrop_path"`
26-
Overview string `json:"overview"`
27-
OriginCountry []string `json:"origin_country,omitempty"` // TV
28-
Popularity float32 `json:"popularity"`
29-
Character string `json:"character"`
30-
Episodes []struct {
31-
AirDate string `json:"air_date"`
32-
EpisodeNumber int64 `json:"episode_number"`
33-
Name string `json:"name"`
34-
Overview string `json:"overview"`
35-
SeasonNumber int `json:"season_number"`
36-
StillPath string `json:"still_path"`
37-
} `json:"episodes,omitempty"` // TV
38-
Seasons []Season `json:"seasons,omitempty"` // TV
39-
VoteMetrics
40-
} `json:"media"`
9+
CreditType string `json:"credit_type"`
10+
Department string `json:"department"`
11+
Job string `json:"job"`
12+
Media CreditMedia `json:"media"`
13+
MediaType string `json:"media_type"`
14+
ID string `json:"id"`
15+
Person Person `json:"person"`
16+
}
17+
18+
type CreditSeason struct {
19+
Season
4120
MediaType string `json:"media_type"`
42-
ID string `json:"id"`
43-
Person struct {
44-
Adult bool `json:"adult"`
45-
Gender int `json:"gender"`
46-
Name string `json:"name"`
47-
ID int64 `json:"id"`
48-
KnownFor []struct {
49-
Adult bool `json:"adult,omitempty"`
50-
BackdropPath string `json:"backdrop_path"`
51-
// GenreIDs []int64 `json:"genre_ids"` // FIXME: -> []float32
52-
// ID int64 `json:"id"` // FIXME: -> float32
53-
OriginalLanguage string `json:"original_language"`
54-
OriginalTitle string `json:"original_title,omitempty"`
55-
Overview string `json:"overview"`
56-
PosterPath string `json:"poster_path"`
57-
ReleaseDate string `json:"release_date,omitempty"`
58-
Title string `json:"title,omitempty"`
59-
Video bool `json:"video,omitempty"`
60-
Popularity float32 `json:"popularity"`
61-
MediaType string `json:"media_type"`
62-
OriginalName string `json:"original_name,omitempty"`
63-
Name string `json:"name,omitempty"`
64-
FirstAirDate string `json:"first_air_date,omitempty"`
65-
OriginCountry []string `json:"origin_country,omitempty"`
66-
VoteMetrics
67-
} `json:"known_for"`
68-
KnownForDepartment string `json:"known_for_department"`
69-
ProfilePath string `json:"profile_path"`
70-
Popularity float32 `json:"popularity"`
71-
} `json:"person"`
21+
ShowID int64 `json:"show_id"`
7222
}
7323

7424
// GetCreditDetails get a movie or TV credit details by id.

find.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,8 @@ type FindByID struct {
1919
Popularity float32 `json:"popularity"`
2020
VoteMetrics
2121
} `json:"movie_results,omitempty"`
22-
PersonResults []struct {
23-
Adult bool `json:"adult"`
24-
Gender int `json:"gender"`
25-
Name string `json:"name"`
26-
ID int64 `json:"id"`
27-
KnownFor []struct {
28-
Adult bool `json:"adult,omitempty"` // Movie
29-
BackdropPath string `json:"backdrop_path"`
30-
FirstAirDate string `json:"first_air_date,omitempty"` // TV
31-
// GenreIDs []int64 `json:"genre_ids"` // FIXME: -> []float32
32-
// ID int64 `json:"id"` // FIXME: -> float32
33-
MediaType string `json:"media_type"`
34-
Name string `json:"name,omitempty"` // TV
35-
OriginalLanguage string `json:"original_language"`
36-
OriginalName string `json:"original_name,omitempty"` // TV
37-
OriginalTitle string `json:"original_title,omitempty"` // Movie
38-
OriginCountry []string `json:"origin_country,omitempty"` // TV
39-
Overview string `json:"overview"`
40-
Popularity float32 `json:"popularity"`
41-
PosterPath string `json:"poster_path"`
42-
ReleaseDate string `json:"release_date,omitempty"` // Movie
43-
Title string `json:"title,omitempty"` // Movie
44-
Video bool `json:"video,omitempty"` // Movie
45-
VoteMetrics
46-
} `json:"known_for"`
47-
KnownForDepartment string `json:"known_for_department"`
48-
ProfilePath string `json:"profile_path"`
49-
Popularity float32 `json:"popularity"`
50-
} `json:"person_results,omitempty"`
51-
TvResults []struct {
22+
PersonResults []PersonResults `json:"person_results"`
23+
TvResults []struct {
5224
OriginalName string `json:"original_name"`
5325
ID int64 `json:"id"`
5426
Name string `json:"name"`

types.go

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,14 @@ type PaginatedResultsMeta struct {
6969

7070
// Season represents a TV show season with details such as air date, episode count, name, overview, poster path, season number, vote average, and associated show ID.
7171
type Season struct {
72-
AirDate string `json:"air_date"`
73-
EpisodeCount int `json:"episode_count"`
7472
ID int64 `json:"id"`
7573
Name string `json:"name"`
7674
Overview string `json:"overview"`
7775
PosterPath string `json:"poster_path"`
76+
VoteAverage float32 `json:"vote_average"`
77+
AirDate string `json:"air_date"`
7878
SeasonNumber int `json:"season_number"`
79-
VoteAverage float32 `json:"vote_average,omitempty"`
80-
ShowID int64 `json:"show_id,omitempty"`
79+
EpisodeCount int `json:"episode_count"`
8180
}
8281

8382
// LastEpisodeToAir represents the details of the most recently aired episode of a TV show.
@@ -143,3 +142,107 @@ type ImageBase struct {
143142
Width int `json:"width"`
144143
VoteMetrics
145144
}
145+
146+
type PersonBase struct {
147+
Adult bool `json:"adult"`
148+
ID int64 `json:"id"`
149+
Name string `json:"name"`
150+
OriginalName string `json:"original_name"`
151+
Popularity float32 `json:"popularity"`
152+
Gender int `json:"gender"`
153+
KnownForDepartment string `json:"known_for_department"`
154+
ProfilePath string `json:"profile_path"`
155+
}
156+
157+
type Person struct {
158+
PersonBase
159+
}
160+
161+
type PersonResults struct {
162+
PersonBase
163+
MediaType string `json:"media_type"`
164+
KnownFor []KnownFor `json:"known_for"`
165+
}
166+
167+
type KnownForBase struct {
168+
Adult bool `json:"adult"`
169+
BackdropPath string `json:"backdrop_path"`
170+
ID int32 `json:"id"`
171+
Overview string `json:"overview"`
172+
PosterPath string `json:"poster_path"`
173+
MediaType string `json:"media_type"`
174+
OriginalLanguage string `json:"original_language"`
175+
GenreIDs []int32 `json:"genre_ids"`
176+
Popularity float32 `json:"popularity"`
177+
Video bool `json:"video"`
178+
VoteMetrics
179+
}
180+
181+
type KnownFor interface {
182+
GetMediaType() string
183+
}
184+
185+
func (movie KnownForMovie) GetMediaType() string { return movie.MediaType }
186+
func (tv KnownForTV) GetMediaType() string { return tv.MediaType }
187+
188+
type KnownForMovie struct {
189+
KnownForBase
190+
Title string `json:"title"`
191+
OriginalTitle string `json:"original_title"`
192+
ReleaseDate string `json:"release_date"`
193+
}
194+
195+
type KnownForTV struct {
196+
KnownForBase
197+
Name string `json:"name"`
198+
OriginalName string `json:"original_name"`
199+
FirstAirDate string `json:"first_air_date"`
200+
OriginCountry []string `json:"origin_country"`
201+
}
202+
203+
type CreditMedia interface {
204+
GetMediaType() string
205+
}
206+
207+
func (movie CreditMediaMovie) GetMediaType() string { return movie.MediaType }
208+
func (tv CreditMediaTV) GetMediaType() string { return tv.MediaType }
209+
210+
type CreditMediaBase struct {
211+
Adult bool `json:"adult"`
212+
BackdropPath string `json:"backdrop_path"`
213+
ID int64 `json:"id"`
214+
Overview string `json:"overview"`
215+
PosterPath string `json:"poster_path"`
216+
MediaType string `json:"media_type"`
217+
OriginalLanguage string `json:"original_language"`
218+
GenreIDs []int64 `json:"genre_ids"`
219+
Popularity float32 `json:"popularity"`
220+
Character string `json:"character"`
221+
VoteMetrics
222+
}
223+
type CreditMediaTV struct {
224+
CreditMediaBase
225+
Name string `json:"name"`
226+
OriginalName string `json:"original_name"`
227+
FirstAirDate string `json:"first_air_date"`
228+
OriginCountry []string `json:"origin_country"`
229+
Episodes []Episode `json:"episodes"`
230+
Seasons []CreditSeason `json:"seasons"`
231+
}
232+
233+
type CreditMediaMovie struct {
234+
CreditMediaBase
235+
Title string `json:"title"`
236+
OriginalTitle string `json:"original_title"`
237+
ReleaseDate string `json:"release_date"`
238+
Video bool `json:"video"`
239+
}
240+
241+
type Episode struct {
242+
AirDate string `json:"air_date"`
243+
EpisodeNumber int64 `json:"episode_number"`
244+
Name string `json:"name"`
245+
Overview string `json:"overview"`
246+
SeasonNumber int `json:"season_number"`
247+
StillPath string `json:"still_path"`
248+
}

unmarshal.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package tmdb
2+
3+
import (
4+
"fmt"
5+
6+
json "github.com/goccy/go-json"
7+
)
8+
9+
func (c *CreditsDetails) UnmarshalJSON(data []byte) error {
10+
type alias CreditsDetails
11+
var raw struct {
12+
alias
13+
Media json.RawMessage `json:"media"`
14+
}
15+
if err := json.Unmarshal(data, &raw); err != nil {
16+
return err
17+
}
18+
*c = CreditsDetails(raw.alias)
19+
var mediaType struct {
20+
MediaType string `json:"media_type"`
21+
}
22+
if err := json.Unmarshal(raw.Media, &mediaType); err != nil {
23+
return err
24+
}
25+
26+
switch mediaType.MediaType {
27+
case "movie":
28+
var movie CreditMediaMovie
29+
if err := json.Unmarshal(raw.Media, &movie); err != nil {
30+
return err
31+
}
32+
c.Media = movie
33+
case "tv":
34+
var tv CreditMediaTV
35+
if err := json.Unmarshal(raw.Media, &tv); err != nil {
36+
return err
37+
}
38+
c.Media = tv
39+
default:
40+
return fmt.Errorf("unknown media_type: %s", mediaType.MediaType)
41+
}
42+
return nil
43+
}
44+
45+
func (p *PersonResults) UnmarshalJSON(data []byte) error {
46+
type alias PersonResults
47+
var raw struct {
48+
alias
49+
KnownFor []json.RawMessage `json:"known_for"`
50+
}
51+
if err := json.Unmarshal(data, &raw); err != nil {
52+
return err
53+
}
54+
*p = PersonResults(raw.alias)
55+
for _, item := range raw.KnownFor {
56+
var mediaType struct {
57+
MediaType string `json:"media_type"`
58+
}
59+
if err := json.Unmarshal(item, &mediaType); err != nil {
60+
return err
61+
}
62+
63+
switch mediaType.MediaType {
64+
case "movie":
65+
var movie KnownForMovie
66+
if err := json.Unmarshal(item, &movie); err != nil {
67+
return err
68+
}
69+
p.KnownFor = append(p.KnownFor, movie)
70+
case "tv":
71+
var tv KnownForTV
72+
if err := json.Unmarshal(item, &tv); err != nil {
73+
return err
74+
}
75+
p.KnownFor = append(p.KnownFor, tv)
76+
default:
77+
return fmt.Errorf("unknown media_type: %s", mediaType.MediaType)
78+
}
79+
}
80+
return nil
81+
}

0 commit comments

Comments
 (0)