Skip to content

Commit e5b4386

Browse files
authored
fixed the types for metadata json responses (#35)
* fixed the types for metadata json responses * added GetPlaylist() to cli * added GetPlaylist() to cli * fixed GetPlaylist() - catch non-200 status http errors * changed types on properties that are int or string * changed RatingCount per #41 * handle OptimizedForStreaming bool or int
1 parent c211f79 commit e5b4386

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

models.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package plex
33
import (
44
"encoding/json"
55
"encoding/xml"
6+
"fmt"
67
"net/http"
78
"time"
89
)
@@ -68,7 +69,7 @@ type Metadata struct {
6869
ParentRatingKey string `json:"parentRatingKey"`
6970
ParentThumb string `json:"parentThumb"`
7071
ParentTitle string `json:"parentTitle"`
71-
RatingCount string `json:"ratingCount"`
72+
RatingCount int `json:"ratingCount"`
7273
Rating float64 `json:"rating"`
7374
RatingKey string `json:"ratingKey"`
7475
SessionKey string `json:"sessionKey"`
@@ -91,6 +92,37 @@ type AltGUID struct {
9192
ID string `json:"id"`
9293
}
9394

95+
type boolOrInt struct {
96+
bool
97+
}
98+
99+
func (b *boolOrInt) UnmarshalJSON(data []byte) error {
100+
var isInt int
101+
102+
if err := json.Unmarshal(data, &isInt); err == nil {
103+
if isInt == 0 || isInt == 1 {
104+
105+
if isInt != 0 && isInt != 1 {
106+
return fmt.Errorf("invalid boolOrInt: %d", isInt)
107+
}
108+
109+
b.bool = isInt == 1
110+
111+
return nil
112+
}
113+
}
114+
115+
var isBool bool
116+
117+
if err := json.Unmarshal(data, &isBool); err != nil {
118+
return err
119+
}
120+
121+
b.bool = isBool
122+
123+
return nil
124+
}
125+
94126
// Media media info
95127
type Media struct {
96128
AspectRatio json.Number `json:"aspectRatio"`
@@ -103,7 +135,8 @@ type Media struct {
103135
Has64bitOffsets bool `json:"has64bitOffsets"`
104136
Height int `json:"height"`
105137
ID json.Number `json:"id"`
106-
OptimizedForStreaming bool `json:"optimizedForStreaming"` // plex can return int or boolean: 0 or 1; true or false
138+
OptimizedForStreaming boolOrInt `json:"optimizedForStreaming"` // plex can return int (GetMetadata(), GetPlaylist()) or boolean (GetSessions()): 0 or 1; true or false
139+
107140
Selected bool `json:"selected"`
108141
VideoCodec string `json:"videoCodec"`
109142
VideoFrameRate string `json:"videoFrameRate"`
@@ -821,7 +854,7 @@ type Part struct {
821854
HasThumbnail string `json:"hasThumbnail"`
822855
ID json.Number `json:"id"`
823856
Key string `json:"key"`
824-
OptimizedForStreaming bool `json:"optimizedForStreaming"`
857+
OptimizedForStreaming boolOrInt `json:"optimizedForStreaming"`
825858
Selected bool `json:"selected"`
826859
Size int `json:"size"`
827860
Stream []Stream `json:"Stream"`

0 commit comments

Comments
 (0)