Skip to content

Commit 0f65d19

Browse files
authored
Merge pull request #17 from lkvrsfld/fix-GetClientID
fix getting dynamic client ID by searching it in all provided assets
2 parents d733a17 + 00121eb commit 0f65d19

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

pkg/soundcloud/get_client_id.go

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package soundcloud
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"log"
77
"regexp"
88
)
@@ -12,33 +12,58 @@ import (
1212

1313
// GetClientID returns a new generated client_id when a request is made to SoundCloud's API
1414
func (s *Soundcloud) GetClientID() (string, error) {
15-
var clientID string
16-
17-
// this is the JS file that is injected into the page source
18-
// this can always change at some point, so we have to keep an eye on it
19-
resp, err := s.Client.Get("https://a-v2.sndcdn.com/assets/2-fbfac8ab.js")
15+
// Make the initial request to SoundCloud
16+
resp, err := s.Client.Get("https://soundcloud.com")
2017
if err != nil {
2118
return "", err
2219
}
23-
2420
defer resp.Body.Close()
2521

26-
body, err := ioutil.ReadAll(resp.Body)
22+
// Read the response body
23+
body, err := io.ReadAll(resp.Body)
2724
if err != nil {
2825
log.Fatalf("failed to read response body: %v", err)
26+
return "", fmt.Errorf("failed to read response body: %v", err)
2927
}
3028

31-
// regex to find the client_id
32-
re := regexp.MustCompile(`client_id\s*:\s*['"]([^'"]+)['"]`)
33-
matches := re.FindSubmatch(body)
29+
// Compile regex to find asset URLs
30+
assetre := regexp.MustCompile(`src="(https:\/\/a-v2\.sndcdn\.com\/assets\/[^\s"]+)"`)
31+
assetMatches := assetre.FindAllSubmatch(body, -1)
3432

35-
if len(matches) > 1 {
36-
// Found a client_id
37-
clientID = string(matches[1])
38-
} else {
39-
log.Println("client_id not found")
40-
return "", fmt.Errorf("client_id not found")
33+
// Check if any asset matches were found
34+
if len(assetMatches) == 0 {
35+
return "", fmt.Errorf("asset not found")
4136
}
42-
43-
return clientID, nil
37+
38+
// Iterate over asset matches to find the client ID
39+
for _, match := range assetMatches {
40+
assetURL := string(match[1])
41+
42+
// Make a request to the asset URL
43+
resp, err := s.Client.Get(assetURL)
44+
if err != nil {
45+
return "", err
46+
}
47+
defer resp.Body.Close()
48+
49+
// Read the response body
50+
body, err := io.ReadAll(resp.Body)
51+
if err != nil {
52+
log.Fatalf("failed to read response body: %v", err)
53+
return "", fmt.Errorf("failed to read response body: %v", err)
54+
}
55+
56+
// Compile regex to find the client ID
57+
re := regexp.MustCompile(`client_id:\"([^\"]+)\"`)
58+
matches := re.FindSubmatch(body)
59+
60+
// Check if a client ID was found
61+
if len(matches) > 1 {
62+
return string(matches[1]), nil
63+
}
64+
}
65+
66+
// If no client ID was found
67+
log.Println("client_id not found")
68+
return "", fmt.Errorf("client_id not found")
4469
}

0 commit comments

Comments
 (0)