Skip to content

Commit 2922e62

Browse files
committed
refactor(client): add baseURL and httpClient to client ID extraction
1 parent 923bb68 commit 2922e62

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

client.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ type Client struct {
2020
// NewClient creates a new SoundCloud client by extracting a client_id
2121
// from the SoundCloud website.
2222
func NewClient() (*Client, error) {
23+
return newClient("https://soundcloud.com", &http.Client{})
24+
}
25+
26+
func newClient(baseURL string, httpClient *http.Client) (*Client, error) {
2327
c := &Client{
24-
httpClient: &http.Client{},
28+
httpClient: httpClient,
2529
}
2630

27-
clientID, err := c.extractClientID()
31+
clientID, err := c.extractClientIDFrom(baseURL)
2832
if err != nil {
2933
return nil, fmt.Errorf("extract client_id: %w", err)
3034
}
@@ -61,9 +65,13 @@ var (
6165
)
6266

6367
func (c *Client) extractClientID() (string, error) {
64-
body, err := c.get("https://soundcloud.com")
68+
return c.extractClientIDFrom("https://soundcloud.com")
69+
}
70+
71+
func (c *Client) extractClientIDFrom(baseURL string) (string, error) {
72+
body, err := c.get(baseURL)
6573
if err != nil {
66-
return "", fmt.Errorf("fetch soundcloud.com: %w", err)
74+
return "", fmt.Errorf("fetch %s: %w", baseURL, err)
6775
}
6876

6977
matches := assetRe.FindAllSubmatch(body, -1)

download.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ func (c *Client) Download(track *Track, outputDir string, progress func(download
3333
return "", fmt.Errorf("parse M3U8: %w", err)
3434
}
3535

36-
countUint := mpl.Count()
37-
// Check for integer overflow before converting uint to int
38-
// Max int value on this architecture
39-
const maxInt = int(^uint(0) >> 1)
40-
if countUint > uint(maxInt) {
41-
return "", fmt.Errorf("playlist too large to handle")
42-
}
43-
count := int(countUint)
36+
count := int(mpl.Count())
4437
if count == 0 {
4538
return "", fmt.Errorf("no segments in playlist")
4639
}
@@ -127,10 +120,7 @@ func (c *Client) parseM3U8(m3u8URL string) (*m3u8.MediaPlaylist, error) {
127120
}
128121

129122
mpl := playlist.(*m3u8.MediaPlaylist)
130-
base, err := url.Parse(m3u8URL)
131-
if err != nil {
132-
return nil, err
133-
}
123+
base, _ := url.Parse(m3u8URL)
134124

135125
if mpl.Key != nil && mpl.Key.URI != "" {
136126
mpl.Key.URI, err = resolveURI(base, mpl.Key.URI)

stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func parseHLSURL(hlsURL string) (trackID, streamToken string, err error) {
4747

4848
// Find the part containing "soundcloud:tracks:"
4949
for i, part := range parts {
50-
if strings.HasPrefix(part, "soundcloud:tracks:") {
50+
if strings.HasPrefix(part, "soundcloud:tracks") {
5151
idParts := strings.Split(part, ":")
5252
if len(idParts) < 3 {
5353
return "", "", fmt.Errorf("invalid track ID format in URL")

0 commit comments

Comments
 (0)