-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsoundcloudpy.py
449 lines (325 loc) · 18 KB
/
soundcloudpy.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import requests
import json
from bs4 import BeautifulSoup
BASE_URL = "https://api-v2.soundcloud.com"
class Soundcloud:
def __init__(self, o_auth, client_id):
#: Client id for soundcloud account(must be 32bytes length)
if len(client_id) != 32:
raise ValueError("Not valid client id")
self.client_id = client_id
#: O-Auth code for requests headers
self.o_auth = o_auth
# To get the last version of Firefox to prevent some type of deprecated version
json_versions = dict(requests.get("https://product-details.mozilla.org/1.0/firefox_versions.json").json())
firefox_version = json_versions.get('LATEST_FIREFOX_VERSION')
#: Default headers that work properly for the API
#: User-Agent as if it was requested through Firefox Browser
self.headers = {"Authorization" : o_auth, "Accept": "application/json",
"User-Agent": f"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:{firefox_version}) Gecko/20100101 Firefox/{firefox_version}"}
# Version of soundcloud app
app_json = requests.get("https://soundcloud.com/versions.json")
self.app_version = dict(app_json.json()).get('app')
# ---------------- USER ----------------
def get_account_details(self):
req = requests.get(f"{BASE_URL}/me", headers=self.headers)
return req.json()
def get_user_details(self, user_id):
"""
:param user_id: id of the user requested
"""
req = requests.get(f"{BASE_URL}/users/{user_id}?client_id={self.client_id}", headers=self.headers)
return req.json()
def get_followers(self, limit=500):
"""
:param limit: max numbers of follower accounts to get
"""
req = requests.get(f"{BASE_URL}/me/followers/ids?linked_partitioning=1&client_id={self.client_id}&limit={limit}&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_following(self, limit=12):
"""
:param limit: max numbers of following accounts
"""
own_user_id = dict(json.loads(self.get_account_details())).get('id')
req = requests.get(f"{BASE_URL}/users/{own_user_id}/followings?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_recommended_users(self, limit=5):
"""
:param limit: max numbers of follower accounts to get
"""
req = requests.get(f"{BASE_URL}/me/suggested/users/who_to_follow?view=recommended-first&client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def follow(self, user_id):
"""
:param user_id: id of the user requested
"""
req = requests.post(f"{BASE_URL}/me/followings/{user_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.status_code
def unfollow(self, user_id):
"""
:param user_id: id of the user requested
"""
req = requests.delete(f"{BASE_URL}/me/followings/{user_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.status_code
def send_message(self, user_id, message):
"""
:param user_id: id of the user requested
:param message: string with the content of the message
"""
body = {
"contents": message
}
own_user_id = dict(json.loads(self.get_account_details())).get('id')
req = requests.post(f"{BASE_URL}/users/{own_user_id}/conversations/{user_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers, json=body)
return req.json()
def delete_conversation(self, user_id):
"""
:param user_id: id of the user requested
"""
own_user_id = dict(json.loads(self.get_account_details())).get('id')
req = requests.delete(f"{BASE_URL}/users/{own_user_id}/conversations/{user_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_repost_from_user(self, user_id, limit):
"""
:param user_id: id of the user requested
:param limit: number of repost to get
"""
req = requests.get(f"{BASE_URL}/stream/users/{user_id}/reposts?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_related_artists(self, user_id, limit=12):
"""
Related artists from an user_id of an artist
:param user_id: id of the user requested
:param limit: number of repost to get
"""
req = requests.get(f"{BASE_URL}/users/{user_id}/relatedartists?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
# ---------------- TRACKS ----------------
def get_last_track_info(self, limit=1):
"""
:param limit: number of last tracks reproduced
"""
req = requests.get(f"{BASE_URL}/me/play-history/tracks?client_id={self.client_id}&limit={limit}", headers=self.headers)
return req.json()
def get_track_likes_users(self, track_id):
"""
:param track_id: track id
"""
req = requests.get(f"{BASE_URL}/tracks/{track_id}/likers?client_id={self.client_id}", headers=self.headers)
return req.json()
def get_track_details(self, track_id):
"""
:param track_id: track id
"""
req = requests.get(f"{BASE_URL}/tracks?ids={track_id}&client_id={self.client_id}", headers=self.headers)
return req.json()
def get_tracks_liked(self, limit=50):
"""
:param limit: number of tracks to get
"""
req = requests.get(f"{BASE_URL}/me/track_likes/ids?client_id={self.client_id}&limit={limit}&app_version={self.app_version}", headers=self.headers)
return req.json()
def like_a_track(self, track_id):
# To like a track we need the account id
user_id = dict(json.loads(self.get_account_details())).get('id')
req = requests.put(f"{BASE_URL}/users/{user_id}/track_likes/{track_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
# Return "OK" if successful request
return req.json()
def unlike_a_track(self, track_id):
# To unlike a track we need the account id
user_id = dict(self.get_account_details()).get('id')
req = requests.delete(f"{BASE_URL}/users/{user_id}/track_likes/{track_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
# Return "OK" if successful request
return req.text
def repost_track(self, track_id):
req = requests.put(f"{BASE_URL}/me/track_reposts/{track_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
# Return the status code of the request
return req.status_code
def unrepost_track(self, track_id):
req = requests.delete(f"{BASE_URL}/me/track_reposts/{track_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
# Return the status code of the request
return req.status_code
def get_track_by_genre_recent(self, genre, limit=10):
"""
:param genre: string of the genre to get tracks
:param limit: limit of playlists to get
"""
req = requests.get(f"{BASE_URL}/recent-tracks/{genre}?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_track_by_genre_popular(self, genre, limit=10):
"""
:param genre: string of the genre to get tracks
:param limit: limit of playlists to get
"""
req = requests.get(f"{BASE_URL}/search/tracks?q=&filter.genre_or_tag={genre}&sort=popular&client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
def get_track_id_from_permalink(self, url_track):
"""
:param url_track: string with the url like: "https://soundcloud.com/postmalone/post-malone-hateful"
"""
req = requests.get(url_track)
scrap = str(BeautifulSoup(req.content, 'html.parser').find('meta', property='al:android:url').get('content'))
index = str(scrap).find(":", 14, -1)
track = scrap[index+1:]
return track
def get_tracks_from_user(self, user_id, limit=10):
"""
:param user_id: id of the user to get his tracks
:param limit: number of tracks to get from user
"""
req = requests.get(f"{BASE_URL}/users/{user_id}/tracks?representation=&client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_popular_tracks_user(self, user_id, limit=12):
"""
:param user_id: id of the user to get his tracks
:param limit: number of tracks to get from user
"""
req = requests.get(f"{BASE_URL}/users/{user_id}/toptracks?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
# ---------------- PLAYLISTS ----------------
def get_account_playlists(self):
req = requests.get(f"{BASE_URL}/me/library/all?client_id{self.client_id}", headers=self.headers)
return req.json()
def get_playlist_details(self, playlist_id):
"""
:param playlist_id: playlist id
"""
req = requests.get(f"{BASE_URL}/playlists/{playlist_id}?representation=full&client_id={self.client_id}", headers=self.headers)
return req.json()
def create_playlist(self, title, track_list, public=False, description=None):
"""
:param title: str of the playlist title
:param track_list: python list with the tracks_ids to be in the new playlist
:param public: boolean (True if public/False if private)
:param description: description of the playlist
"""
# If there is no track on the list throw an error
if len(track_list) == 0:
raise ValueError("Empty list for creating playlist")
# Public or Private depends on what you selected (Private default)
privacy = "public" if public else "private"
# Body for POST requests of playlist
body = {
"playlist": {
"title": title,
"sharing": privacy,
"tracks": track_list,
"_resource_type": "playlist"
}
}
# If there is a description include it
if description != None:
body["playlist"].update({'description': description})
req = requests.post(f"{BASE_URL}/playlists?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers, json=body)
return req.json()
def delete_playlist(self, playlist_id):
req = requests.delete(f"{BASE_URL}/playlists/{playlist_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_playlists_liked(self, limit=50):
"""
:param limit: limit of recommended playlists make for you
"""
req = requests.get(f"{BASE_URL}/me/playlist_likes/ids?limit={limit}&linked_partitioning=1&client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.json()
def like_playlist(self, playlist_id):
"""
:param playlist_id: playlist id
"""
user_id = dict(json.loads(self.get_account_details())).get('id')
req = requests.put(f"{BASE_URL}/users/{user_id}/playlist_likes/{playlist_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
# Return "OK" if like successful
return req.text
def unlike_playlist(self, playlist_id):
"""
:param playlist_id: playlist id
"""
user_id = dict(json.loads(self.get_account_details())).get('id')
req = requests.delete(f"{BASE_URL}/users/{user_id}/playlist_likes/{playlist_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
# Return "OK" if like successful
return req.text
def repost_playlist(self, playlist_id):
"""
:param playlist_id: playlist id
"""
req = requests.put(f"{BASE_URL}/me/playlist_reposts/{playlist_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.status_code
def unrepost_playlist(self, playlist_id):
"""
:param playlist_id: playlist id
"""
req = requests.delete(f"{BASE_URL}/me/playlist_reposts/{playlist_id}?client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.status_code
def get_playlists_by_genre(self, genre, limit=10):
"""
:param genre: string of the genre to get tracks
:param limit: limit of playlists to get
"""
req = requests.get(f"{BASE_URL}/playlists/discovery?tag={genre}&client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_playlists_from_user(self, user_id, limit=10):
"""
:param user_id: user id to get his playlists
:param limit: limit of playlists to get
"""
req = requests.get(f"{BASE_URL}/users/{user_id}/playlists_without_albums?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
# ---------------- MISCELLANEOUS ----------------
def get_recommended(self, track_id):
"""
:param track_id: track id to get recommended tracks from this
"""
req = requests.get(f"{BASE_URL}/tracks/{track_id}/related?client_id={self.client_id}", headers=self.headers)
return req.json()
def get_stream_url(self, track_id):
"""
:param track_id: track id
"""
full_json = list(json.loads(self.get_track_details(track_id)))
media_url = full_json[0]['media']['transcodings'][0]['url']
track_auth = full_json[0]['track_authorization']
stream_url = f"{media_url}?client_id={self.client_id}&track_authorization={track_auth}"
req = requests.get(stream_url, headers=self.headers)
return dict(req.json()).get('url')
def get_comments_track(self, track_id, limit=100):
"""
:param track_id: track id for get the comments from
:param limit: limit of tracks to get
:add: with the "next_href" in the return json you can keep getting more comments than the limit
"""
req = requests.get(f"{BASE_URL}/tracks/{track_id}/comments?threaded=0&filter_replies=1&client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_mixed_selection(self, limit=5):
"""
:param limit: limit of recommended playlists make for you
"""
req = requests.get(f"{BASE_URL}/mixed-selections?variant_ids=&client_id={self.client_id}&limit={limit}&app_version={self.app_version}", headers=self.headers)
return req.json()
def search_tracks(self, query_string, limit=10):
"""
:param query_string: string to search on souncloud for tracks
:param limit: limit of recommended playlists make for you
"""
req = requests.get(f"{BASE_URL}/search?q={query_string}&variant_ids=&facet=model&client_id={self.client_id}&limit={limit}&offset=0&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_suscribe_feed(self, limit=10):
req = requests.get(f"{BASE_URL}/stream?offset=10&limit={limit}&promoted_playlist=true&client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_album_from_user(self, user_id, limit=5):
"""
:param user_id: user to get the albums from
:param limit: numbers of albums to get from the user
"""
req = requests.get(f"{BASE_URL}/users/{user_id}/albums?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_all_feed_user(self, user_id, limit=20):
"""
:param user_id: user to get the albums from
:param limit: numbers of items to get from the user's feed
"""
req = requests.get(f"{BASE_URL}/stream/users/{user_id}?client_id={self.client_id}&limit={limit}&offset=0&linked_partitioning=1&app_version={self.app_version}", headers=self.headers)
return req.json()
def get_unread_conversations(self, limit=20):
"""
:param limit: number of unread conversations per request
"""
req = requests.get(f"{BASE_URL}/users/{self.get_account_details().get('id')}/conversations/unread?force=1&limit={limit}&offset=0&linked_partitioning=1&client_id={self.client_id}&app_version={self.app_version}", headers=self.headers)
return req.json()