/
utils.go
115 lines (97 loc) · 2.39 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
package utils
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/mmzou/geektime-dl/requester"
)
// MAXLENGTH Maximum length of file name
const MAXLENGTH = 80
//FileName filter invalid string
func FileName(name string, ext string) string {
rep := strings.NewReplacer("\n", " ", "/", " ", "|", "-", ": ", ":", ":", ":", "'", "’", "\t", " ")
name = rep.Replace(name)
if runtime.GOOS == "windows" {
rep := strings.NewReplacer("\"", " ", "?", " ", "*", " ", "\\", " ", "<", " ", ">", " ", ":", " ", ":", " ")
name = rep.Replace(name)
}
name = strings.TrimSpace(name)
limitedName := LimitLength(name, MAXLENGTH)
if ext != "" {
return fmt.Sprintf("%s.%s", limitedName, ext)
}
return limitedName
}
//LimitLength cut string
func LimitLength(s string, length int) string {
ellipses := "..."
str := []rune(s)
if len(str) > length {
s = string(str[:length-len(ellipses)]) + ellipses
}
return s
}
// FilePath gen valid file path
func FilePath(name, ext string, escape bool) (string, error) {
var outputPath string
var fileName string
if escape {
fileName = FileName(name, ext)
} else {
fileName = fmt.Sprintf("%s.%s", name, ext)
}
outputPath = filepath.Join(fileName)
return outputPath, nil
}
//Mkdir mkdir path
func Mkdir(elem ...string) (string, error) {
path := filepath.Join(elem...)
err := os.MkdirAll(path, os.ModePerm)
return path, err
}
// FileSize return the file size of the specified path file
func FileSize(filePath string) (int, bool, error) {
file, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
return 0, false, nil
}
return 0, false, err
}
return int(file.Size()), true, nil
}
// M3u8URLs get all ts urls from m3u8 url
func M3u8URLs(uri string) ([]string, error) {
if len(uri) == 0 {
return nil, errors.New("M3u8地址为空")
}
html, err := requester.HTTPGet(uri)
if err != nil {
return nil, err
}
lines := strings.Split(string(html), "\n")
var urls []string
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
if strings.HasPrefix(line, "http") {
urls = append(urls, line)
} else {
base, err := url.Parse(uri)
if err != nil {
continue
}
u, err := url.Parse(line)
if err != nil {
continue
}
urls = append(urls, fmt.Sprintf("%s", base.ResolveReference(u)))
}
}
}
return urls, nil
}