-
Notifications
You must be signed in to change notification settings - Fork 3
/
plex.go
153 lines (117 loc) · 2.77 KB
/
plex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"github.com/jyggen/go-plex-client"
"strings"
"time"
)
type Plex struct {
connection *plex.Plex
}
func NewPlex(baseUrl string, token string, timeout time.Duration) (*Plex, error) {
connection, err := plex.New(strings.TrimRight(baseUrl, "/"), token)
if err != nil {
return &Plex{}, err
}
connection.HTTPClient.Timeout = timeout
_, err = connection.Test()
if err != nil {
return &Plex{}, err
}
return &Plex{
connection: connection,
}, nil
}
func (p *Plex) Shows() ([]*Show, error) {
shows := make([]*Show, 0)
libraries, err := p.connection.GetLibraries()
if err != nil {
return shows, fmt.Errorf("unable to retrieve libraries: %w", err)
}
for _, library := range libraries.MediaContainer.Directory {
if library.Type != "show" {
continue
}
content, err := p.connection.GetLibraryContent(library.Key, "")
if err != nil {
return shows, fmt.Errorf("unable to retrieve content for library %v: %w", library.Key, err)
}
for _, show := range content.MediaContainer.Metadata {
shows = append(shows, &Show{
connection: p.connection,
metadata: show,
})
}
}
return shows, nil
}
type Show struct {
connection *plex.Plex
metadata plex.Metadata
}
func (s *Show) Seasons() ([]*Season, error) {
seasons := make([]*Season, 0)
children, err := s.connection.GetMetadataChildren(s.metadata.RatingKey)
if err != nil {
return seasons, err
}
for _, season := range children.MediaContainer.Metadata {
seasons = append(seasons, &Season{
connection: s.connection,
metadata: season,
})
}
return seasons, nil
}
func (s *Show) RatingKey() string {
return s.metadata.RatingKey
}
func (s *Show) SortTitle() string {
if s.metadata.TitleSort == "" {
return s.metadata.Title
}
return s.metadata.TitleSort
}
func (s *Show) Title() string {
return s.metadata.Title
}
type Season struct {
connection *plex.Plex
metadata plex.Metadata
}
func (s *Season) Episodes() ([]*Episode, error) {
episodes := make([]*Episode, 0)
children, err := s.connection.GetMetadataChildren(s.metadata.RatingKey)
if err != nil {
return episodes, err
}
for _, episode := range children.MediaContainer.Metadata {
metadata, err := s.connection.GetMetadata(episode.RatingKey)
if err != nil {
return episodes, err
}
episodes = append(episodes, &Episode{
connection: s.connection,
metadata: metadata.MediaContainer.Metadata[0],
})
}
return episodes, nil
}
func (e *Season) Number() int {
return int(e.metadata.Index)
}
type Episode struct {
connection *plex.Plex
metadata plex.Metadata
}
func (e *Episode) HasIntroMarker() bool {
for _, marker := range e.metadata.Marker {
if marker.Type == "intro" {
return true
}
}
return false
}
func (e *Episode) Number() int {
return int(e.metadata.Index)
}