-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundcloud.go
218 lines (175 loc) · 4.7 KB
/
soundcloud.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
)
type Likes struct {
Collection []struct {
Track struct {
URL string `json:"permalink_url"`
} `json:"track"`
} `json:"collection"`
NextHref string `json:"next_href"`
}
type User struct {
ID int `json:"id"`
}
var client = &http.Client{Timeout: 15 * time.Second}
func getRequest(url string) (resp *http.Response, err error) {
res, err := client.Get(url)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, errors.New("Non-200 in getRequest")
}
return res, nil
}
func likesRequest(url string) ([]string, string, error) {
res, err := getRequest(url)
if err != nil {
return nil, "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, "", err
}
var likes = new(Likes)
err = json.Unmarshal(body, &likes)
urls := []string{}
for _, track := range likes.Collection {
urls = append(urls, track.Track.URL)
}
return urls, likes.NextHref, nil
}
// The HTML returned from soundcloud has a lot of script tags. If you follow
// the URL in the last tag you get some javacript back which contains a client ID
func parseForUrl(res *http.Response) (string, error) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
urlRegex := regexp.MustCompile(`<script crossorigin src="(.*?)"`)
regExUrls := urlRegex.FindAllStringSubmatch(string(body), -1)
var urls []string
for _, arr := range regExUrls {
urls = append(urls, arr[1])
}
return urls[len(urls)-1], nil
}
func validateClientId(clientId string) (bool, error) {
if clientId == "" {
return false, nil
}
params := url.Values{
"client_id": []string{clientId},
// this is my profile so I know it is a valid URL
"url": []string{"https://soundcloud.com/brian-brenner-4"},
}
// there might be a request that is faster to check, but this works
res, err := client.Get("https://api-v2.soundcloud.com/resolve?" + params.Encode())
if err != nil {
return false, err
}
if res.StatusCode == 200 {
return true, nil
}
// 401 means clientId is invalid
if res.StatusCode == 401 {
return false, nil
}
return false, errors.New("Recieved " + strconv.Itoa(res.StatusCode) + " in validateClientId")
}
func getClientId() (string, error) {
log.Println("Fetching client-id")
res, err := getRequest("https://soundcloud.com/")
if err != nil {
return "", err
}
defer res.Body.Close()
url, err := parseForUrl(res)
if err != nil {
return "", err
}
idRes, err := getRequest(url)
if err != nil {
return "", err
}
defer idRes.Body.Close()
idBody, err := ioutil.ReadAll(idRes.Body)
if err != nil {
return "", err
}
idRegex := regexp.MustCompile(`,client_id:"(.*?)"`)
idVal := idRegex.FindStringSubmatch(string(idBody))
if idVal == nil || len(idVal) < 2 {
return "", errors.New("Unable to find client_id in regex")
}
return idVal[1], nil
}
func getUserId(profileUrl string, clientId string) (string, error) {
params := url.Values{
"client_id": []string{clientId},
"url": []string{profileUrl},
}
res, err := client.Get("https://api-v2.soundcloud.com/resolve?" + params.Encode())
if err != nil {
return "", err
}
// invalid user name
if res.StatusCode == 404 {
return "", &APIError{status: http.StatusBadRequest, message: "No user was found with the provided username"}
}
if res.StatusCode != 200 {
return "", errors.New("Non-200 in getUserId request")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
var user = new(User)
err = json.Unmarshal(body, &user)
if err != nil {
return "", err
}
return strconv.Itoa(user.ID), nil
}
func getUserLikes(userId string, clientId string) ([]string, error) {
params := url.Values{
// this seems to have no limit, but don't want soundcloud to block me or anything
// 200 is the limit on apiv1
"limit": []string{"10000"},
"client_id": []string{clientId},
}
// The soundcloud API returns paginated requests by adding a next_href property to the response. We follow
// this till it becomes nil, meaning we've reached the end of the pagination
finalUrls, nextUrl, err := likesRequest("https://api-v2.soundcloud.com/users/" + userId + "/likes?" + params.Encode())
if err != nil {
return nil, err
}
for nextUrl != "" {
urls, localNextUrl, err := likesRequest(nextUrl + "&client_id=" + clientId)
if err != nil {
return nil, err
}
finalUrls = append(finalUrls, urls...)
nextUrl = localNextUrl
}
shuffled := make([]string, len(finalUrls))
rand.Seed(time.Now().UTC().UnixNano())
perm := rand.Perm(len(finalUrls))
for i, v := range perm {
shuffled[v] = finalUrls[i]
}
return shuffled, nil
}