-
Notifications
You must be signed in to change notification settings - Fork 3
/
subtitle_list.go
108 lines (92 loc) · 2.68 KB
/
subtitle_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
108
package list
import (
"fmt"
"reflect"
"github.com/fatih/set"
"github.com/tympanix/supper/types"
"golang.org/x/text/language"
)
// Subtitles creates a new list of subtitles from variadic argument
func Subtitles(subs ...types.Subtitle) types.SubtitleList {
list := subtitleList(subs)
return &list
}
// NewSubtitlesFromInterface construct a subtitle list from interface values
func NewSubtitlesFromInterface(subs ...interface{}) (l types.SubtitleList, err error) {
var list []types.Subtitle
defer func() {
if r := recover(); r != nil {
l, err = nil, fmt.Errorf("%v", r)
}
}()
for _, v := range subs {
list = append(list, extractSubtitles(v)...)
}
lx := subtitleList(list)
return &lx, nil
}
func extractSubtitles(e interface{}) []types.Subtitle {
if s, ok := e.(types.Subtitle); ok {
return []types.Subtitle{s}
}
t := reflect.TypeOf(e).Kind()
if t == reflect.Slice || t == reflect.Array {
var subs []types.Subtitle
v := reflect.ValueOf(e)
for j := 0; j < v.Len(); j++ {
subs = append(subs, extractSubtitles(v.Index(j).Interface())...)
}
return subs
}
panic(fmt.Sprintf("Unknown subtitle format %v", reflect.TypeOf(e)))
}
type subtitleList []types.Subtitle
func (s *subtitleList) Len() int {
return len(*s)
}
func (s *subtitleList) List() []types.Subtitle {
return *s
}
func (s *subtitleList) Best() (types.Subtitle, float32) {
return nil, -1
}
func (s *subtitleList) Add(sub ...types.Subtitle) {
*s = append(*s, sub...)
}
// FilterLanguage returns a new subtitle collection including only the argument language
func (s *subtitleList) FilterLanguage(lang language.Tag) types.SubtitleList {
_subs := make([]types.Subtitle, 0)
for _, sub := range *s {
if sub.Language() == lang {
_subs = append(_subs, sub)
}
}
list := subtitleList(_subs)
return &list
}
func (s *subtitleList) FilterScore(score float32) types.SubtitleList {
panic("unrated subtitle list does not support filtering by score")
}
// HearingImpaired returns a new subtitle collection where hearing impared subtitles has been filtered
func (s *subtitleList) HearingImpaired(hi bool) types.SubtitleList {
_subs := make([]types.Subtitle, 0)
for _, sub := range *s {
if sub.HearingImpaired() == hi {
_subs = append(_subs, sub)
}
}
list := subtitleList(_subs)
return &list
}
// RateByMedia returns a rated subtitle list, where every subtitle has been
// given a score according to how well it matches the argument media
func (s *subtitleList) RateByMedia(m types.Media, e types.Evaluator) types.RatedSubtitleList {
return NewRatedSubtitles(m, e, (*s)...)
}
func (s *subtitleList) LanguageSet() set.Interface {
langs := set.New()
for _, sub := range *s {
langs.Add(sub.Language())
}
return langs
}