-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
60 lines (50 loc) · 1.15 KB
/
stream.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
package grunk
import (
"fmt"
"io"
"log"
"net/http"
"strings"
)
type stream map[string]string
type streamList []stream
func (s stream) Url() string {
return s["url"] + "&signature=" + s["sig"]
}
func (s stream) Format() string {
for format, trigger := range formatsTrigger {
if strings.Contains(s["type"], trigger) {
return format
}
}
return FORMAT_UNKNOWN
}
func (s stream) Quality() string {
for _, quality := range sortedQualities {
if quality == s["quality"] {
return quality
}
}
return QUALITY_UNKNOWN
}
func downloadFromUrl(url string, out io.Writer) error {
log.Printf("Running stream from '%s'", url)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("requesting stream: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("reading answer: non 200 status code received: '%s'", err)
}
length, err := io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("saving file: %s (%d bytes copied)", err, length)
}
log.Printf("Downloaded %d bytes", length)
return nil
}
func (stream stream) download(out io.Writer) error {
url := stream.Url()
return downloadFromUrl(url, out)
}