-
Notifications
You must be signed in to change notification settings - Fork 3
/
media_list.go
107 lines (93 loc) · 2.72 KB
/
media_list.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
package list
import (
"encoding/json"
"time"
"github.com/tympanix/supper/types"
)
// LocalMedia is a list of locally stored media
type LocalMedia struct {
media []types.LocalMedia
}
// NewLocalMedia return a new local media list from its arguments
func NewLocalMedia(media ...types.LocalMedia) *LocalMedia {
return &LocalMedia{
media,
}
}
// Add adds new local media to the list
func (l *LocalMedia) Add(m types.LocalMedia) {
l.media = append(l.media, m)
}
// Len returns the number of media in the list
func (l *LocalMedia) Len() int {
return len(l.media)
}
// List returns the list of localmedia as a plain slice
func (l *LocalMedia) List() []types.LocalMedia {
return l.media
}
// Filter return the list of local media which satisfies some predicate
func (l *LocalMedia) Filter(p types.MediaFilter) types.LocalMediaList {
filtered := make([]types.LocalMedia, 0)
for _, media := range l.List() {
if p(media) {
filtered = append(filtered, media)
}
}
return NewLocalMedia(filtered...)
}
// FilterModified returns only media which has been modified since some duration
func (l *LocalMedia) FilterModified(d time.Duration) types.LocalMediaList {
t := time.Now().Local().Add(-1 * d)
media := make([]types.LocalMedia, 0)
for _, m := range l.List() {
if m.ModTime().After(t) {
media = append(media, m)
}
}
return NewLocalMedia(media...)
}
// FilterVideo returns only media which is of type video (e.g. not subtites)
func (l *LocalMedia) FilterVideo() types.VideoList {
video := make([]types.Video, 0)
for _, media := range l.List() {
if v, ok := media.(types.Video); ok {
video = append(video, v)
}
}
return NewVideo(video...)
}
// FilterMovies return only media which is of type movie
func (l *LocalMedia) FilterMovies() types.LocalMediaList {
movies := make([]types.LocalMedia, 0)
for _, media := range l.List() {
if _, ok := media.TypeMovie(); ok {
movies = append(movies, media)
}
}
return NewLocalMedia(movies...)
}
// FilterEpisodes returns only media which is of type episode
func (l *LocalMedia) FilterEpisodes() types.LocalMediaList {
episodes := make([]types.LocalMedia, 0)
for _, media := range l.List() {
if _, ok := media.TypeEpisode(); ok {
episodes = append(episodes, media)
}
}
return NewLocalMedia(episodes...)
}
// FilterSubtitles returns only media which is of type subtitles
func (l *LocalMedia) FilterSubtitles() types.LocalMediaList {
subtitles := make([]types.LocalMedia, 0)
for _, media := range l.List() {
if _, ok := media.TypeSubtitle(); ok {
subtitles = append(subtitles, media)
}
}
return NewLocalMedia(subtitles...)
}
// MarshalJSON returns a JSON representation of the media list
func (l *LocalMedia) MarshalJSON() (b []byte, err error) {
return json.Marshal(l.List())
}