Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the types for metadata json responses #35

Merged
merged 9 commits into from
Apr 28, 2022
39 changes: 36 additions & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plex
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"time"
)
Expand Down Expand Up @@ -68,7 +69,7 @@ type Metadata struct {
ParentRatingKey string `json:"parentRatingKey"`
ParentThumb string `json:"parentThumb"`
ParentTitle string `json:"parentTitle"`
RatingCount string `json:"ratingCount"`
RatingCount int `json:"ratingCount"`
Rating float64 `json:"rating"`
RatingKey string `json:"ratingKey"`
SessionKey string `json:"sessionKey"`
Expand All @@ -91,6 +92,37 @@ type AltGUID struct {
ID string `json:"id"`
}

type boolOrInt struct {
bool
}

func (b *boolOrInt) UnmarshalJSON(data []byte) error {
var isInt int

if err := json.Unmarshal(data, &isInt); err == nil {
if isInt == 0 || isInt == 1 {

if isInt != 0 && isInt != 1 {
return fmt.Errorf("invalid boolOrInt: %d", isInt)
}

b.bool = isInt == 1

return nil
}
}

var isBool bool

if err := json.Unmarshal(data, &isBool); err != nil {
return err
}

b.bool = isBool

return nil
}

// Media media info
type Media struct {
AspectRatio json.Number `json:"aspectRatio"`
Expand All @@ -103,7 +135,8 @@ type Media struct {
Has64bitOffsets bool `json:"has64bitOffsets"`
Height int `json:"height"`
ID json.Number `json:"id"`
OptimizedForStreaming bool `json:"optimizedForStreaming"` // plex can return int or boolean: 0 or 1; true or false
OptimizedForStreaming boolOrInt `json:"optimizedForStreaming"` // plex can return int (GetMetadata(), GetPlaylist()) or boolean (GetSessions()): 0 or 1; true or false

Selected bool `json:"selected"`
VideoCodec string `json:"videoCodec"`
VideoFrameRate string `json:"videoFrameRate"`
Expand Down Expand Up @@ -796,7 +829,7 @@ type Part struct {
HasThumbnail string `json:"hasThumbnail"`
ID json.Number `json:"id"`
Key string `json:"key"`
OptimizedForStreaming bool `json:"optimizedForStreaming"`
OptimizedForStreaming boolOrInt `json:"optimizedForStreaming"`
Selected bool `json:"selected"`
Size int `json:"size"`
Stream []Stream `json:"Stream"`
Expand Down