forked from Hakkin/twitchpipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.go
124 lines (105 loc) · 2.7 KB
/
playlist.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
package main
import (
"bufio"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)
type playlistInfo struct {
Name string
Group string
Bandwidth int
Width int
Height int
URL string
}
var (
groupRegex = regexp.MustCompile(`GROUP-ID="([^"]+)"`)
nameRegex = regexp.MustCompile(`NAME="([^"]+)"`)
bandwidthRegex = regexp.MustCompile(`BANDWIDTH=([0-9]+)`)
resolutionRegex = regexp.MustCompile(`RESOLUTION=([0-9]+x[0-9]+)`)
)
func getPlaylists(c *http.Client, username string, token *accessToken) ([]playlistInfo, error) {
pURL, err := url.Parse(fmt.Sprintf(playlistURL, username))
if err != nil {
return nil, err
}
query := pURL.Query()
query.Set("allow_source", "true")
query.Set("allow_audio_only", "true")
query.Set("fast_bread", "true")
query.Set("sig", token.Sig)
query.Set("token", token.Token)
pURL.RawQuery = query.Encode()
req, err := http.NewRequest("GET", pURL.String(), nil)
if err != nil {
return nil, err
}
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, fmt.Errorf("playlist got http status %s", res.Status)
}
return nil, errors.New("stream is offline")
}
scanner := bufio.NewScanner(res.Body)
scanner.Split(bufio.ScanLines)
var playlists []playlistInfo
var info playlistInfo
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "http") {
info.URL = scanner.Text()
playlists = append(playlists, info)
info = playlistInfo{}
continue
}
if !strings.HasPrefix(scanner.Text(), "#EXT") {
continue
}
split := strings.SplitN(scanner.Text(), ":", 2)
if len(split) != 2 {
continue
}
tag, attribute := split[0], split[1]
switch tag {
case "#EXT-X-MEDIA":
groupMatch := groupRegex.FindStringSubmatch(attribute)
if groupMatch != nil {
info.Group = groupMatch[1]
}
nameMatch := nameRegex.FindStringSubmatch(attribute)
if nameMatch != nil {
info.Name = nameMatch[1]
}
case "#EXT-X-STREAM-INF":
bandwidthMatch := bandwidthRegex.FindStringSubmatch(attribute)
if bandwidthMatch != nil {
bandwidthInt, err := strconv.Atoi(bandwidthMatch[1])
if err == nil {
info.Bandwidth = bandwidthInt
}
}
resolutionMatch := resolutionRegex.FindStringSubmatch(attribute)
if resolutionMatch != nil {
resolution := strings.SplitN(resolutionMatch[1], "x", 2)
if len(resolution) == 2 {
width, widthErr := strconv.Atoi(resolution[0])
height, heightErr := strconv.Atoi(resolution[1])
if widthErr == nil && heightErr == nil {
info.Width = width
info.Height = height
}
}
}
}
}
return playlists, nil
}