-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
93 lines (75 loc) · 2.24 KB
/
client.go
File metadata and controls
93 lines (75 loc) · 2.24 KB
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
// Package scdl provides a client for interacting with SoundCloud's internal API.
package scdl
import (
"context"
"fmt"
"io"
"net/http"
"regexp"
)
const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
// Client provides methods to interact with the SoundCloud API.
type Client struct {
httpClient *http.Client
clientID string
}
// NewClient creates a new SoundCloud client by extracting a client_id
// from the SoundCloud website.
func NewClient(ctx context.Context) (*Client, error) {
return newClient(ctx, "https://soundcloud.com", &http.Client{})
}
func newClient(ctx context.Context, baseURL string, httpClient *http.Client) (*Client, error) {
c := &Client{
httpClient: httpClient,
}
clientID, err := c.extractClientIDFrom(ctx, baseURL)
if err != nil {
return nil, fmt.Errorf("extract client_id: %w", err)
}
c.clientID = clientID
return c, nil
}
func (c *Client) get(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("HTTP %d for %s", resp.StatusCode, url)
}
const maxResponseSize = 50 << 20 // 50 MB
return io.ReadAll(io.LimitReader(resp.Body, maxResponseSize))
}
var (
assetRe = regexp.MustCompile(`src="(https://a-v2\.sndcdn\.com/assets/[^\s"]+)"`)
clientIDRe = regexp.MustCompile(`client_id:"([^"]+)"`)
)
func (c *Client) extractClientIDFrom(ctx context.Context, baseURL string) (string, error) {
body, err := c.get(ctx, baseURL)
if err != nil {
return "", fmt.Errorf("fetch %s: %w", baseURL, err)
}
matches := assetRe.FindAllSubmatch(body, -1)
if len(matches) == 0 {
return "", fmt.Errorf("no asset URLs found on soundcloud.com")
}
for _, match := range matches {
assetURL := string(match[1])
assetBody, err := c.get(ctx, assetURL)
if err != nil {
continue
}
if m := clientIDRe.FindSubmatch(assetBody); len(m) > 1 {
return string(m[1]), nil
}
}
return "", fmt.Errorf("client_id not found in any asset bundle")
}