-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
executable file
·215 lines (186 loc) · 5.16 KB
/
utils.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package utils
import (
"errors"
"fmt"
"html"
"regexp"
"strconv"
"strings"
"github.com/gan-of-culture/get-sauce/config"
"github.com/gan-of-culture/get-sauce/static"
)
// GetLastItemString of slice
func GetLastItemString(slice []string) string {
if len(slice) <= 0 {
return ""
}
return slice[len(slice)-1]
}
// CalcSizeInByte func
func CalcSizeInByte(number float64, unit string) int64 {
switch unit {
case "KB":
number *= 1000
case "MB":
number *= 1000000
case "GB":
number *= 10000000000
}
return int64(number)
}
// ByteCountSI turn bytes to SI (decimal) format - https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
// NeedDownloadList return the indices of gallery that need download
func NeedDownloadList(length int) []int {
if config.Pages != "" {
var items []int
var selStart, selEnd int
temp := strings.Split(config.Pages, ",")
for _, i := range temp {
selection := strings.Split(i, "-")
selStart, _ = strconv.Atoi(strings.TrimSpace(selection[0]))
if len(selection) >= 2 {
selEnd, _ = strconv.Atoi(strings.TrimSpace(selection[1]))
} else {
selEnd = selStart
}
for item := selStart; item <= selEnd; item++ {
items = append(items, item)
}
}
return items
}
out := []int{}
for i := 1; i <= length; i++ {
out = append(out, i)
}
return out
}
// GetMediaType e.g. put in png get image, mp4 -> video
func GetMediaType(t string) static.DataType {
switch t {
case "jpg", "jpeg", "png", "gif", "webp", "avif":
return static.DataTypeImage
case "webm", "mp4", "mkv", "m4a", "txt", "m3u8", "avi":
return static.DataTypeVideo
default:
return static.DataTypeUnknown
}
}
// GetH1 of html - file idx -1 = last h1 found - if index out of range set to last h1
func GetH1(htmlString *string, idx int) string {
return GetSectionHeadingElement(htmlString, 1, idx)
}
func GetSectionHeadingElement(htmlString *string, level, idx int) string {
closingSectionHeadingTag := fmt.Sprintf("</h%d>", level)
re := regexp.MustCompile(fmt.Sprintf(`[^>]*%s`, closingSectionHeadingTag))
sectionHeadingElements := re.FindAllString(*htmlString, -1)
sectionHeadingElementsLen := len(sectionHeadingElements)
if idx == -1 {
idx = sectionHeadingElementsLen
}
// if index out of range set last
if sectionHeadingElementsLen < idx+1 {
idx = sectionHeadingElementsLen - 1
if idx == -1 {
return ""
}
}
return html.UnescapeString(strings.TrimSuffix(sectionHeadingElements[idx], closingSectionHeadingTag))
}
// GetMeta of html file
func GetMeta(htmlString *string, property string) string {
re := regexp.MustCompile(fmt.Sprintf("<meta property=[\"']*%s[\"']* content=[\"']([^\"']*)", property))
metaTags := re.FindAllStringSubmatch(*htmlString, -1)
if len(metaTags) < 1 {
return fmt.Sprintf("no matches found for %s", property)
}
return metaTags[0][1]
}
// RemoveAdjDuplicates of string slice
func RemoveAdjDuplicates(slice []string) []string {
out := []string{}
var last string
for _, s := range slice {
if s != last {
out = append(out, s)
}
last = s
}
return out
}
// ParseHLSMaster into static.Stream to prefill the structure
// returns a pre filled structure where URLs[0].URL is the media stream URI
func ParseHLSMaster(master *string) ([]*static.Stream, error) {
re := regexp.MustCompile(`#EXT-X-STREAM-INF:([^\n]*)\n([^\n]+)`) // 1=PARAMS 2=MEDIAURI
matchedStreams := re.FindAllStringSubmatch(*master, -1)
if len(matchedStreams) < 1 {
return nil, fmt.Errorf("unable to parse any stream in m3u master: %s", *master)
}
out := []*static.Stream{}
for _, stream := range matchedStreams {
s := &static.Stream{
Type: static.DataTypeVideo,
URLs: []*static.URL{
{
URL: strings.TrimSpace(stream[2]),
Ext: "",
},
},
}
re = regexp.MustCompile(`[A-Z\-]+=(?:"[^"]*"|[^,]*)`) // PARAMETERNAME=VALUE
for _, streamParam := range re.FindAllString(stream[1], -1) {
splitParam := strings.Split(streamParam, "=")
splitParam[1] = strings.Trim(splitParam[1], `",`)
switch splitParam[0] {
case "RESOLUTION":
s.Quality = splitParam[1]
case "CODECS":
s.Info = splitParam[1]
}
}
out = append(out, s)
}
// AUDIO
re = regexp.MustCompile(`#EXT-X-MEDIA:([^\n]*)\n`) // 1=PARAMS
matchedAudioStream := re.FindStringSubmatch(*master)
if len(matchedAudioStream) < 2 {
return out, nil
}
params := map[string]string{}
for _, param := range strings.Split(matchedAudioStream[1], ",") {
splitParam := strings.Split(param, "=")
params[splitParam[0]] = strings.Trim(splitParam[1], `"`)
}
out = append(out, &static.Stream{
Type: static.DataTypeAudio,
URLs: []*static.URL{
{
URL: params["URI"],
},
},
Info: params["LANGUAGE"],
})
return out, nil
}
// Wrap error with context
func Wrap(err error, ctx string) error {
return errors.New(err.Error() + ": " + ctx)
}
// GetFileExt from simple string
func GetFileExt(str string) string {
re := regexp.MustCompile(`\w+$`)
return re.FindString(str)
}