/
models_combination.go
162 lines (141 loc) · 3.89 KB
/
models_combination.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
154
155
156
157
158
159
160
161
162
package app
import (
"fmt"
"sort"
)
func (c *Connector) CombinationGet(id string) (*Combination, error) {
m, err := c.Combination.Get(id, &Combination{})
if err != nil {
return nil, err
}
// post process here
return m, nil
}
func (c *Connector) CombinationByName(name string) (*Combination, error) {
list, err := c.Combination.Query().Where("name", name).Run()
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, err
}
if len(list) > 1 {
return nil, err
}
return list[0], nil
}
func (c *Connector) CombinationList(page, limit int) ([]*Combination, error) {
if page == 0 {
page = 1
}
if limit == 0 {
limit = 10
}
list, err := c.Combination.Query().Skip((page - 1) * limit).Limit(limit).Run()
if err != nil {
return nil, err
}
return list, nil
}
type CombinationChild struct {
RatingKey string `json:"ratingKey"`
Key string `json:"key"`
GUID string `json:"guid"`
Type string `json:"type"`
Title string `json:"title"`
LibraryID int64 `json:"librarySectionID"`
LibraryTitle string `json:"librarySectionTitle"`
LibraryKey string `json:"librarySectionKey"`
Summary string `json:"summary"`
Thumb string `json:"thumb"`
Total int `json:"total"`
Viewed int `json:"viewed"`
Link string `json:"link"`
Next string `json:"next"`
LastViewedAt int64 `json:"lastViewedAt"`
AddedAt int64 `json:"addedAt"`
UpdatedAt int64 `json:"updatedAt"`
}
type stuffSorter struct {
list []*CombinationChild
by func(p1, p2 *CombinationChild) bool
}
// Len is part of sort.Interface.
func (s *stuffSorter) Len() int {
return len(s.list)
}
// Swap is part of sort.Interface.
func (s *stuffSorter) Swap(i, j int) {
s.list[i], s.list[j] = s.list[j], s.list[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *stuffSorter) Less(i, j int) bool {
return s.by(s.list[i], s.list[j])
}
func (c *Connector) CombinationChildren(name string) ([]*CombinationChild, error) {
comb, err := c.CombinationByName(name)
if err != nil {
return nil, err
}
keys := []string{}
for _, id := range comb.Collections {
col, err := c.CollectionGet(id)
if err != nil {
return nil, err
}
keys = append(keys, col.RatingKey)
}
list := []*CombinationChild{}
for _, i := range keys {
children, err := app.Plex.GetCollectionChildren(i)
if err != nil {
return nil, err
}
for _, child := range children {
metadata, err := app.Plex.GetViewedByKey(child.RatingKey)
if err != nil {
return nil, err
}
if metadata == nil {
return nil, fmt.Errorf("metadata not found for %s", child.RatingKey)
}
if metadata.Leaves == metadata.Viewed {
continue
}
unwatched, err := app.Plex.GetSeriesEpisodesUnwatched(child.RatingKey)
if err != nil {
return nil, err
}
stuff := &CombinationChild{
RatingKey: child.RatingKey,
Key: child.Key,
GUID: child.GUID,
Type: child.Type,
Title: child.Title,
LibraryID: child.LibraryID,
LibraryTitle: child.LibraryTitle,
LibraryKey: child.LibraryKey,
Summary: child.Summary,
Link: fmt.Sprintf("https://app.plex.tv/desktop/#!/server/%s/details?key=%s?X-Plex-Token=%s", app.Config.PlexMachineIdentifier, child.Key, app.Config.PlexToken),
Thumb: fmt.Sprintf("%s%s?X-Plex-Token=%s", app.Config.PlexServerURL, child.Thumb, app.Config.PlexToken),
Total: metadata.Leaves,
Viewed: metadata.Viewed,
LastViewedAt: metadata.LastViewedAt,
}
if unwatched != nil {
stuff.Next = unwatched.RatingKey
stuff.AddedAt = unwatched.AddedAt
stuff.UpdatedAt = unwatched.UpdatedAt
}
list = append(list, stuff)
}
}
sorter := &stuffSorter{
list: list,
by: func(p1, p2 *CombinationChild) bool {
return p1.AddedAt > p2.AddedAt
},
}
sort.Sort(sorter)
return list, nil
}