-
Notifications
You must be signed in to change notification settings - Fork 5
/
urls.go
113 lines (98 loc) · 2.11 KB
/
urls.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
package main
import (
"bufio"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
var (
errNoLinks = errors.New("no links found")
errStreamOver = errors.New("stream over")
)
var initRegex = regexp.MustCompile(`URI="([^"]*)"`)
type Segment struct {
Name string
URI string
MapURI string
Duration float64
Seq int
Discontinuity bool
Prefetch bool
}
type DateRange struct {
ID string
Class string
StartDate time.Time
EndDate time.Time
Duration time.Duration
EndOnNext bool
Extra []string
}
func getURLs(c *http.Client, playlist string) ([]Segment, error) {
req, err := http.NewRequest("GET", playlist, nil)
if err != nil {
return nil, err
}
req.Header.Set("Origin", "https://player.twitch.tv")
res, err := c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusNotFound {
return nil, errStreamOver
}
return nil, fmt.Errorf("urls got http status %s", res.Status)
}
var urls []Segment
var done bool
var segment Segment
var mapURI string
scanner := bufio.NewScanner(res.Body)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
switch v := scanner.Text(); {
case strings.HasPrefix(v, mapTag):
matches := initRegex.FindStringSubmatch(v[len(mapTag):])
if len(matches) != 2 {
break
}
mapURI = matches[1]
case strings.HasPrefix(v, mediaSequenceTag):
sequenceText := v[len(mediaSequenceTag):]
seq, err := strconv.Atoi(sequenceText)
if err != nil {
break
}
segment.Seq = seq
case v == discontinuityTag:
segment.Discontinuity = true
case v == endListTag:
done = true
case strings.HasPrefix(v, prefetchTag):
v = v[len(prefetchTag):]
segment.Prefetch = true
fallthrough
case !strings.HasPrefix(v, "#"):
if len(urls) > 0 {
segment.Seq = urls[len(urls)-1].Seq + 1
}
segment.URI = v
segment.MapURI = mapURI
urls = append(urls, segment)
segment = Segment{}
}
}
if len(urls) == 0 {
return nil, errNoLinks
}
if done {
err = errStreamOver
}
return urls, err
}