Skip to content

Commit

Permalink
feel like this is a pretty decent solution for getting the client ID
Browse files Browse the repository at this point in the history
  • Loading branch information
imthaghost committed Feb 9, 2024
1 parent b3b3465 commit b585104
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pkg/soundcloud/get_client_id.go
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
}
7 changes: 7 additions & 0 deletions pkg/soundcloud/service.go
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
}

0 comments on commit b585104

Please sign in to comment.