@@ -2,7 +2,7 @@ package soundcloud
22
33import (
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
1414func (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