-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feel like this is a pretty decent solution for getting the client ID
- Loading branch information
1 parent
b3b3465
commit b585104
Showing
2 changed files
with
36 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
package soundcloud | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"regexp" | ||
) | ||
|
||
// Unfortunately, SoundCloud does not inject the client ID into the page source unless the request is made from a browser. ( Javascript is enabled ) | ||
// This is a workaround to get the client ID from the JS file that is injected into the page source. | ||
|
||
// GetClientID returns a new generated client_id when a request is made to SoundCloud's API | ||
func (s *Soundcloud) GetClientID() (string, error) { | ||
var clientID string | ||
|
||
// this is the JS file that is injected into the page source | ||
// this can always change at some point, so we have to keep an eye on it | ||
_, err := s.Client.Get("https://a-v2.sndcdn.com/assets/2-1475fa5a.js") | ||
resp, err := s.Client.Get("https://a-v2.sndcdn.com/assets/2-fbfac8ab.js") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// return hardcoded client ID for now | ||
return "vbQio6WOh4nNUyVjDdlg5sqdAigTAEzF", nil | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalf("failed to read response body: %v", err) | ||
} | ||
|
||
// regex to find the client_id | ||
re := regexp.MustCompile(`client_id\s*:\s*['"]([^'"]+)['"]`) | ||
matches := re.FindSubmatch(body) | ||
|
||
if len(matches) > 1 { | ||
// Found a client_id | ||
clientID = string(matches[1]) | ||
} else { | ||
log.Println("client_id not found") | ||
return "", fmt.Errorf("client_id not found") | ||
} | ||
|
||
return clientID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package soundcloud | ||
|
||
// Service is the interface that wraps the basic methods to interact with SoundCloud's API | ||
type Service interface { | ||
GetClientID() (string, error) | ||
Download(url string) error | ||
} |