This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathutils.go
63 lines (49 loc) · 1.42 KB
/
utils.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 ytdl
import (
"context"
"fmt"
"io/ioutil"
"net/http"
)
type errUnexpectedStatusCode int
func (err errUnexpectedStatusCode) Error() string {
return fmt.Sprintf("unexpected status code: %d", err)
}
func reverseStringSlice(s []string) {
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func interfaceToString(val interface{}) string {
return fmt.Sprintf("%v", val)
}
func (c *Client) httpGet(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
// Youtube responses depend on language and user agent
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0")
return c.HTTPClient.Do(req)
}
func (c *Client) httpGetAndCheckResponse(ctx context.Context, url string) (*http.Response, error) {
c.Logger.Debug().Msgf("Fetching %v", url)
resp, err := c.httpGet(ctx, url)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
resp.Body.Close()
return nil, errUnexpectedStatusCode(resp.StatusCode)
}
return resp, nil
}
func (c *Client) httpGetAndCheckResponseReadBody(ctx context.Context, url string) ([]byte, error) {
resp, err := c.httpGetAndCheckResponse(ctx, url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}