-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
executable file
·200 lines (173 loc) · 4.58 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
package utils
import (
"errors"
"fmt"
"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":
return int64(number) * 1000
case "MB":
return int64(number) * 1000000
case "GB":
return int64(number) * 10000000000
default:
return int64(number)
}
}
// 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 {
re := regexp.MustCompile(`[^>]*</h1>`)
h1s := re.FindAllString(*htmlString, -1)
h1sLen := len(h1s)
if idx == -1 {
idx = h1sLen
}
// if index out of range set last
if h1sLen < idx+1 {
idx = h1sLen - 1
if idx == -1 {
return ""
}
}
return strings.TrimSuffix(h1s[idx], "</h1>")
}
// 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
}
// ParseM3UMaster into static.Stream to prefill the structure
// returns a pre filled structure where URLs[0].URL is the media m3u URI
func ParseM3UMaster(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 file: %s", *master)
}
out := []*static.Stream{}
for _, stream := range matchedStreams {
s := &static.Stream{}
s.Type = static.DataTypeVideo
for _, v := range stream[1:] {
re = regexp.MustCompile(`([A-Z\-]+=(?:"[^"]*"|[^,]*))`) // 1=list of PARAMNAME=value,
matchedStreamParams := re.FindAllStringSubmatch(v, -1)
if len(matchedStreamParams) == 0 {
s.URLs = []*static.URL{
{
URL: strings.TrimSpace(v),
Ext: "",
},
}
continue
}
for _, streamParam := range matchedStreamParams {
splitParam := strings.Split(streamParam[1], "=")
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)
}