-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.go
185 lines (149 loc) · 4.36 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package soundcloudapi
import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
var firebaseRegex = regexp.MustCompile("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 firebaseURLRegex = regexp.MustCompile(`(?m)^https?:\/\/(soundcloud\.app\.goo\.gl)\/(.*)$`)
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, 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
func StripMobilePrefix(u string) string {
if !strings.Contains(u, "m.soundcloud.com") {
return u
}
_url, err := url.Parse(u)
if err != nil {
return u
}
_url.Host = "soundcloud.com"
return _url.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 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 IsNewMobileURL(u string) bool {
return strings.Index(u, "https://on.soundcloud.com/") == 0
}
func replaceUnicodeChars(str string) (string, error) {
for _, match := range unicodeRegex.FindAllString(str, -1) {
s, err := strconv.Unquote("'" + match + "'")
if err != nil {
return "", err
}
str = strings.Replace(str, match, s, -1)
}
return str, nil
}
// ConvertFirebaseLink converts a link of the form (https://soundcloud.app.goo.gl/xxxxxxxx) to a regular
// SoundCloud link.
func ConvertFirebaseLink(u string) (string, error) {
_url, err := url.Parse(u)
if err != nil {
return "", err
}
q := _url.Query()
q.Set("d", "1")
_url.RawQuery = q.Encode()
res, err := http.Get(_url.String())
if err != nil {
return "", err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
matches := firebaseRegex.FindAllString(string(data), -1)
for _, match := range matches {
if IsURL(match, false, false) {
str, err := replaceUnicodeChars(match)
if err != nil {
return "", err
}
return str, nil
}
}
return "", errors.New("Could not find regular SoundCloud URL from the URL provided")
}
// IsPlaylistURL retuns true if the provided url is a valid SoundCloud playlist URL
func IsPlaylistURL(u string) bool {
if !IsURL(u, false, false) {
return false
}
if IsPersonalizedTrackURL(u) {
return false
}
uObj, err := url.Parse(u)
if err != nil {
return false
}
return strings.Contains(uObj.Path, "/sets/")
}
// IsSearchURL returns true if the provided url is a valid search url
func IsSearchURL(url string) bool {
return strings.Index(url, "https://soundcloud.com/search?") == 0
}
// IsPersonalizedTrackURL returns true if the provided url is a valid personalized track url. Ex/
// https://soundcloud.com/discover/sets/personalized-tracks::sam:335899198
func IsPersonalizedTrackURL(url string) bool {
return strings.Contains(url, "https://soundcloud.com/discover/sets/personalized-tracks::")
}
// ExtractIDFromPersonalizedTrackURL extracts the track ID from a personalized track URL, returns -1
// if no track ID can be extracted
func ExtractIDFromPersonalizedTrackURL(url string) int64 {
if !IsPersonalizedTrackURL(url) {
return -1
}
split := strings.Split(url, ":")
if len(split) < 5 {
return -1
}
id, err := strconv.ParseInt(split[4], 10, 64)
if err != nil {
return -1
}
return id
}
func sliceContains(slice []int64, x int64) bool {
for _, i := range slice {
if i == x {
return true
}
}
return false
}
func deleteEmptyTracks(slice []Track) []Track {
newTracks := []Track{}
for _, t := range slice {
if t.ID != 0 {
newTracks = append(newTracks, t)
}
}
return newTracks
}