forked from Hakkin/twitchpipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urls.go
63 lines (50 loc) · 1.08 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
package main
import (
"bufio"
"errors"
"fmt"
"net/http"
"regexp"
)
var urlRegexp = regexp.MustCompile(`https?://[^\s]+\.ts$`)
var (
errNoLinks = errors.New("no links found")
errStreamOver = errors.New("stream over")
)
func getURLs(c *http.Client, playlist string) ([]string, 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 []string
var done bool
scanner := bufio.NewScanner(res.Body)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if scanner.Text() == "#EXT-X-ENDLIST" {
done = true
}
if url := urlRegexp.FindString(scanner.Text()); url != "" {
urls = append(urls, url)
}
}
if len(urls) == 0 {
return nil, errNoLinks
}
if done {
err = errStreamOver
}
return urls, err
}