This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
format_list.go
139 lines (122 loc) · 2.89 KB
/
format_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
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
package ytdl
import (
"fmt"
"sort"
)
// FormatList is a slice of formats with filtering functionality
type FormatList []*Format
func (formats FormatList) Filter(key FormatKey, values []interface{}) FormatList {
var dst FormatList
for _, v := range values {
for _, f := range formats {
if interfaceToString(f.ValueForKey(key)) == interfaceToString(v) {
dst = append(dst, f)
}
}
}
return dst
}
func (formats FormatList) Extremes(key FormatKey, best bool) FormatList {
dst := formats.Copy()
if len(dst) > 1 {
dst.Sort(key, best)
first := dst[0]
var i int
for i = 0; i < len(dst)-1; i++ {
if first.CompareKey(dst[i+1], key) != 0 {
break
}
}
i++
dst = dst[0:i]
}
return dst
}
func (formats FormatList) Best(key FormatKey) FormatList {
return formats.Extremes(key, true)
}
func (formats FormatList) Worst(key FormatKey) FormatList {
return formats.Extremes(key, false)
}
func (formats FormatList) Sort(key FormatKey, reverse bool) {
var wrapper sort.Interface = formatsSortWrapper{formats, key}
if reverse {
wrapper = sort.Reverse(wrapper)
}
sort.Stable(wrapper)
}
func (formats FormatList) Subtract(other FormatList) FormatList {
var dst FormatList
for _, f := range formats {
include := true
for _, f2 := range other {
if f2.Itag == f.Itag {
include = false
break
}
}
if include {
dst = append(dst, f)
}
}
return dst
}
func (formats FormatList) Copy() FormatList {
dst := make(FormatList, len(formats))
copy(dst, formats)
return dst
}
func (formats *FormatList) addByInfo(info formatInfo, adaptive bool) error {
var err error
var format *Format
itag := getItag(info.Itag)
if itag == nil {
return fmt.Errorf("no itag found with number: %v", info.Itag)
}
switch {
case info.Cipher != nil:
format, err = parseFormat(*info.Cipher)
if err != nil {
return fmt.Errorf("unable to parse cipher '%v': %w", info.Cipher, err)
}
format.Itag = *itag
case info.SignatureCipher != nil:
format, err = parseFormat(*info.SignatureCipher)
if err != nil {
return fmt.Errorf("unable to parse cipher '%v': %w", info.SignatureCipher, err)
}
format.Itag = *itag
default:
format = &Format{
Itag: *itag,
url: info.URL,
}
}
format.Adaptive = adaptive
format.Index = info.Index
format.Init = info.Init
*formats = append(*formats, format)
return nil
}
func (formats *FormatList) addByQueryString(input string, adaptive bool) error {
format, err := parseFormat(input)
if err != nil {
return err
}
format.Adaptive = adaptive
*formats = append(*formats, format)
return nil
}
type formatsSortWrapper struct {
formats FormatList
key FormatKey
}
func (s formatsSortWrapper) Len() int {
return len(s.formats)
}
func (s formatsSortWrapper) Less(i, j int) bool {
return s.formats[i].CompareKey(s.formats[j], s.key) < 0
}
func (s formatsSortWrapper) Swap(i, j int) {
s.formats[i], s.formats[j] = s.formats[j], s.formats[i]
}