Skip to content

Commit

Permalink
Merge pull request #1 from zackradisic/v0.1.4
Browse files Browse the repository at this point in the history
v0.1.4
  • Loading branch information
zackradisic committed Jan 13, 2021
2 parents 71c5cc7 + 01908b1 commit feb9f1c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
21 changes: 15 additions & 6 deletions soundcloudapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (sc *API) GetDownloadURL(url string, streamType string) (string, error) {
streamType = "progressive"
}

if IsURL(url) && !IsPlaylistURL(url) {
if IsURL(url, false, false) && !IsPlaylistURL(url) {
info, err := sc.client.getTrackInfo(GetTrackInfoOptions{
URL: url,
})
Expand Down Expand Up @@ -173,16 +173,25 @@ func (sc *API) GetDownloadURL(url string, streamType string) (string, error) {

func (sc *API) prepareURL(url string) (string, error) {
if sc.StripMobilePrefix {
url = StripMobilePrefix(url)
if IsMobileURL(url) {
url = StripMobilePrefix(url)
}
}

if sc.ConvertFirebaseURLs {
var err error
url, err = ConvertFirebaseLink(url)
if err != nil {
return "", errors.Wrap(err, "Failed to convert Firebase URL")
if IsFirebaseURL(url) {
var err error
url, err = ConvertFirebaseLink(url)
if err != nil {
return "", errors.Wrap(err, "Failed to convert Firebase URL")
}
}
}

return url, nil
}

// IsURL is a shorthand for IsURL(url, sc.StripMobilePrefix, sc.ConvertFirebaseURLs)
func (sc *API) IsURL(url string) bool {
return IsURL(url, sc.StripMobilePrefix, sc.ConvertFirebaseURLs)
}
36 changes: 27 additions & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ import (
"github.com/pkg/errors"
)

const urlRegexp = `^https?:\/\/(soundcloud\.com)\/(.*)$`
var firebaseRegex = regexp.MustCompile("https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,500}\\.[a-zA-Z0-9()]{1,500}\\b([-a-zA-Z0-9()@:%_+.~#?&//\\\\=]*)")

const firebaseRegexp = "https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,500}\\.[a-zA-Z0-9()]{1,500}\\b([-a-zA-Z0-9()@:%_+.~#?&//\\\\=]*)"
var urlRegex = regexp.MustCompile(`(?m)^https?:\/\/(soundcloud\.com)\/(.*)$`)

var firebaseRegex = regexp.MustCompile(firebaseRegexp)
var firebaseURLRegex = regexp.MustCompile(`(?m)^https?:\/\/(soundcloud\.app\.goo\.gl)\/(.*)$`)

var urlRegex = regexp.MustCompile(urlRegexp)
var mobileURLRegex = regexp.MustCompile(`(?m)^https?:\/\/(m\.soundcloud\.com)\/(.*)$`)

var unicodeRegex = regexp.MustCompile(`(?i)\\u([\d\w]{4})`)

// IsURL returns true if the provided url is a valid SoundCloud URL
func IsURL(url string) bool {
return len(urlRegex.FindAllString(url, -1)) > 0
func IsURL(url string, testMobile, testFirebase bool) bool {
success := false
if testMobile {
success = IsMobileURL(url)
}

if testFirebase && !success {
success = IsFirebaseURL(url)
}

if !success {
success = len(urlRegex.FindAllString(url, -1)) > 0
}

return success
}

// StripMobilePrefix removes the prefix for mobile urls. Returns the same string if an error parsing the URL occurs
Expand All @@ -41,7 +54,12 @@ func StripMobilePrefix(u string) string {

// IsFirebaseURL returns true if the url is a SoundCloud Firebase url (has the following form: https://soundcloud.app.goo.gl/xxxxxxxx)
func IsFirebaseURL(u string) bool {
return !strings.Contains(u, "https://soundcloud.app.goo.gl")
return len(firebaseURLRegex.FindAllString(u, -1)) > 0
}

// IsMobileURL returns true if the url is a SoundCloud Firebase url (has the following form: https://m.soundcloud.com/xxxxxx)
func IsMobileURL(u string) bool {
return len(mobileURLRegex.FindAllString(u, -1)) > 0
}

func replaceUnicodeChars(str string) (string, error) {
Expand Down Expand Up @@ -79,7 +97,7 @@ func ConvertFirebaseLink(u string) (string, error) {
matches := firebaseRegex.FindAllString(string(data), -1)

for _, match := range matches {
if IsURL(match) {
if IsURL(match, false, false) {
str, err := replaceUnicodeChars(match)
if err != nil {
return "", err
Expand All @@ -93,7 +111,7 @@ func ConvertFirebaseLink(u string) (string, error) {

// IsPlaylistURL retuns true if the provided url is a valid SoundCloud playlist URL
func IsPlaylistURL(u string) bool {
if !IsURL(u) {
if !IsURL(u, false, false) {
return false
}

Expand Down

0 comments on commit feb9f1c

Please sign in to comment.