-
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.
Merge pull request #12 from imthaghost/dev
Dev
- Loading branch information
Showing
305 changed files
with
202,025 additions
and
12,837 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
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
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
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,29 @@ | ||
package soundcloud | ||
|
||
import ( | ||
"fmt" | ||
"github.com/antchfx/htmlquery" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
func (s *Soundcloud) GetArtwork(doc *html.Node) (string, error) { | ||
// XPath query | ||
artworkPath := "//meta[@property='og:image']/@content" | ||
|
||
// Query the document for the artwork node | ||
nodes, err := htmlquery.QueryAll(doc, artworkPath) | ||
if err != nil { | ||
fmt.Println("Error executing XPath query:", err) | ||
return "", err | ||
} | ||
|
||
// Check if any nodes were found | ||
if len(nodes) > 0 { | ||
// Extract the content from the first node | ||
artwork := htmlquery.InnerText(nodes[0]) | ||
|
||
return artwork, nil | ||
} | ||
|
||
return "", 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,22 @@ | ||
package soundcloud | ||
|
||
import "log" | ||
|
||
// 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) { | ||
|
||
// 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-1475fa5a.js") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
log.Print(resp) | ||
|
||
// return hardcoded client ID for now | ||
return "nUB9ZvnjRiqKF43CkKf3iu69D8bboyKY", 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,83 @@ | ||
package soundcloud | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/antchfx/htmlquery" | ||
"golang.org/x/net/html" | ||
"regexp" | ||
) | ||
|
||
// GetHLSURL gets the HLS URL from the SoundCloud song URL | ||
func (s *Soundcloud) GetHLSURL(doc *html.Node) (string, error) { | ||
// Declare the variable to hold the HLS URL | ||
var hlsURL string | ||
|
||
// XPath query to find the script node containing the relevant JSON | ||
hlsURLPath := "//script[contains(text(), 'media') and contains(text(), 'transcodings')]" | ||
|
||
// Query the document for the script node | ||
nodes, err := htmlquery.QueryAll(doc, hlsURLPath) | ||
if err != nil { | ||
fmt.Println("Error executing XPath query:", err) | ||
return "", err | ||
} | ||
|
||
found := false // Flag to indicate if HLS URL is found | ||
for _, node := range nodes { | ||
scriptContent := htmlquery.InnerText(node) | ||
|
||
// Use regex to extract the JSON part from the script content | ||
re := regexp.MustCompile(`window\.__sc_hydration\s*=\s*(\[{.*?\}]);`) | ||
matches := re.FindStringSubmatch(scriptContent) | ||
if len(matches) > 1 { | ||
jsonData := matches[1] | ||
|
||
// Parse the JSON | ||
var data []map[string]interface{} | ||
if err := json.Unmarshal([]byte(jsonData), &data); err != nil { | ||
fmt.Println("Error parsing JSON:", err) | ||
return "", err | ||
} | ||
|
||
// Traverse the JSON data to find the HLS URL | ||
for _, item := range data { | ||
if val, ok := item["hydratable"]; ok && val == "sound" { | ||
if soundData, ok := item["data"].(map[string]interface{}); ok { | ||
if media, ok := soundData["media"].(map[string]interface{}); ok { | ||
if transcodings, ok := media["transcodings"].([]interface{}); ok { | ||
for _, transcoding := range transcodings { | ||
if t, ok := transcoding.(map[string]interface{}); ok { | ||
if format, ok := t["format"].(map[string]interface{}); ok { | ||
if mimeType, ok := format["mime_type"].(string); ok && mimeType == "audio/mpeg" { | ||
if url, ok := t["url"].(string); ok { | ||
hlsURL = url | ||
found = true | ||
break // Found the HLS URL | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if found { | ||
break // Exit the loop as we found the HLS URL | ||
} | ||
} | ||
} | ||
if found { | ||
break // Exit the main loop as HLS URL is found | ||
} | ||
} | ||
|
||
if !found { | ||
return "", errors.New("HLS URL not found") | ||
} | ||
|
||
// Return the HLS URL | ||
return hlsURL, 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,72 @@ | ||
package soundcloud | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/net/html" | ||
"log" | ||
"strings" | ||
) | ||
|
||
// ConstructStreamURL will construct a stream url from a SoundCloud song url | ||
func (s *Soundcloud) ConstructStreamURL(doc *html.Node) (string, error) { | ||
|
||
// get client id | ||
clientID, err := s.GetClientID() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// get track authorization | ||
trackAuth, err := s.GetTrackAuthorization(doc) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// get hls stream url | ||
hlsStreamURL, err := s.GetHLSURL(doc) | ||
if err != nil { | ||
return "", err | ||
} | ||
log.Println("Track Auth: ", trackAuth) | ||
log.Println("Client ID: ", clientID) | ||
log.Println("HLS Stream URL: ", hlsStreamURL) | ||
|
||
trackID, streamToken, err := getTrackInfo(hlsStreamURL) | ||
|
||
// construct stream url | ||
baseURL := "https://api-v2.soundcloud.com/media/soundcloud:tracks:%s/%s/stream/hls?client_id=%s&track_authorization=%s" | ||
|
||
streamURL := fmt.Sprintf(baseURL, trackID, streamToken, clientID, trackAuth) | ||
|
||
log.Println("Track ID: ", trackID) | ||
log.Println("Stream Token: ", streamToken) | ||
|
||
log.Println("Stream URL: ", streamURL) | ||
|
||
return streamURL, nil | ||
} | ||
|
||
func getTrackInfo(url string) (string, string, error) { | ||
// Split the URL by '/' | ||
parts := strings.Split(url, "/") | ||
|
||
// Check if the parts length is as expected | ||
if len(parts) < 8 { | ||
return "", "", fmt.Errorf("invalid URL format") | ||
} | ||
|
||
// Extract the part that contains 'soundcloud:tracks:TRACK_ID' | ||
trackIDPart := parts[4] // "soundcloud:tracks:373180994" | ||
|
||
// Further split the track ID part to get the actual track ID | ||
trackIDParts := strings.Split(trackIDPart, ":") | ||
if len(trackIDParts) < 3 { | ||
return "", "", fmt.Errorf("invalid track ID format in URL") | ||
} | ||
trackID := trackIDParts[2] | ||
|
||
// The stream token is the next part of the URL | ||
streamToken := parts[5] // "ca04c4eb-a299-4f4b-9ff1-ac20ed014fe5" | ||
|
||
return trackID, streamToken, 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,29 @@ | ||
package soundcloud | ||
|
||
import ( | ||
"fmt" | ||
"github.com/antchfx/htmlquery" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
// GetTitle will return the title of the song | ||
func (s *Soundcloud) GetTitle(doc *html.Node) (string, error) { | ||
// XPath query | ||
titlePath := "//meta[@property='og:title']/@content" | ||
|
||
// Query the document for the title node | ||
nodes, err := htmlquery.QueryAll(doc, titlePath) | ||
if err != nil { | ||
fmt.Println("Error executing XPath query:", err) | ||
return "", err | ||
} | ||
|
||
// Check if any nodes were found | ||
if len(nodes) > 0 { | ||
// Extract the content from the first node | ||
title := htmlquery.InnerText(nodes[0]) | ||
return title, nil | ||
} | ||
|
||
return "", nil | ||
} |
Oops, something went wrong.