-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredundanthls.go
92 lines (77 loc) · 2.37 KB
/
redundanthls.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
package redundanthls
import (
"errors"
"github.com/franela/goreq"
"strings"
)
// GetRawManifest returns body of url as string
func GetRawManifest(url string) (string, error) {
res, err := goreq.Request{Uri: url}.Do()
if err != nil || res.StatusCode != 200 {
return "", errors.New("Error retrieving raw manifest.")
}
body, err := res.Body.ToString()
if err != nil {
return "", err
}
return body, nil
}
func getRedundantManifest(rawManifest string, hosts []string, maxLevel string, protocol string) string {
var redundantManifest string
var hasFoundMaxLevel bool
var looksForMaxLevel bool
if maxLevel != "" {
looksForMaxLevel = true
}
rawLines := strings.Split(rawManifest, "\n")
// fmt.Println(rawLines[0])
var levelInfo string
for key, line := range rawLines {
if key == 0 {
redundantManifest = line
} else {
if strings.HasPrefix(line, "#EXT-X-STREAM-INF:PROGRAM-ID=1") {
levelInfo = line
// redundantManifest = redundantManifest + "\n"
} else {
if strings.HasSuffix(line, ".m3u8") {
if looksForMaxLevel && !hasFoundMaxLevel || !looksForMaxLevel {
if strings.Contains(line, maxLevel) {
hasFoundMaxLevel = true
}
for _, host := range hosts {
redundantManifest = redundantManifest + "\n" + levelInfo + "\n" + protocol + host + "/" + line
}
}
} else {
redundantManifest = redundantManifest + "\n" + line
}
}
}
}
return redundantManifest
}
type error interface {
Error() string
}
// RedundantManifestFromString returns redundant manifest from string
func RedundantManifestFromString(rawManifest string, hosts []string, maxLevel string, protocol string) (string, error) {
if rawManifest == "" {
return "", errors.New("Manifest is empty")
}
redundantManifest := getRedundantManifest(rawManifest, hosts, maxLevel, protocol)
return redundantManifest, nil
}
// RedundantManifestFromURL returns redundant manifest from url
func RedundantManifestFromURL(url string, hosts []string, protocol string) (string, error) {
res, err := goreq.Request{Uri: url}.Do()
if err != nil || res.StatusCode != 200 {
return "", errors.New("Error retrieving manifest.")
}
rawManifest, err := res.Body.ToString()
if err != nil {
return "", errors.New("Error getting the body of the manifest.")
}
redundantManifest := getRedundantManifest(rawManifest, hosts, "", protocol)
return redundantManifest, nil
}