Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getting dynamic client ID by searching it in all provided assets #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions pkg/soundcloud/get_client_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package soundcloud

import (
"fmt"
"io/ioutil"
"io"
"log"
"regexp"
)
Expand All @@ -12,33 +12,58 @@ import (

// 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
resp, err := s.Client.Get("https://a-v2.sndcdn.com/assets/2-fbfac8ab.js")
// Make the initial request to SoundCloud
resp, err := s.Client.Get("https://soundcloud.com")
if err != nil {
return "", err
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("failed to read response body: %v", err)
return "", fmt.Errorf("failed to read response body: %v", err)
}

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

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")
// Check if any asset matches were found
if len(assetMatches) == 0 {
return "", fmt.Errorf("asset not found")
}

return clientID, nil

// Iterate over asset matches to find the client ID
for _, match := range assetMatches {
assetURL := string(match[1])

// Make a request to the asset URL
resp, err := s.Client.Get(assetURL)
if err != nil {
return "", err
}
defer resp.Body.Close()

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("failed to read response body: %v", err)
return "", fmt.Errorf("failed to read response body: %v", err)
}

// Compile regex to find the client ID
re := regexp.MustCompile(`client_id:\"([^\"]+)\"`)
matches := re.FindSubmatch(body)

// Check if a client ID was found
if len(matches) > 1 {
return string(matches[1]), nil
}
}

// If no client ID was found
log.Println("client_id not found")
return "", fmt.Errorf("client_id not found")
}