forked from LukePetruzzi/DailyHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFromSoundcloud.py
64 lines (46 loc) · 1.86 KB
/
getFromSoundcloud.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
import requests
import json
import my_config as mc
#from bs4 import BeautifulSoup
def getFromSoundcloud():
# to get from a specific genre, make
parameters = {'kind': 'trending', 'genre': 'soundcloud:genres:all-music', 'limit': '10', 'client_id': mc.soundcloud_clientId}
headers = {'Content-Type': 'application/json'}
r = requests.get('https://api-v2.soundcloud.com/charts', params=parameters, headers=headers)
#print("soundcloudRESPONSE_CODE:", r.status_code)
# only continue if got the stuff successfully
if r.status_code != 200:
return None
#print(json.dumps(r.json(), indent=4, sort_keys=True))
results = r.json()['collection']
# create a list for each item that is a dict of data
resultList = list()
for result in results:
# create a dictionary for the current result
singleDict = {}
# print the tracks' title
singleDict['title'] = result['track']['title']
# print the track's artist
singleDict['artist'] = result['track']['user']['username']
# print URL to thumbnail
singleDict['thumbnail'] = result['track']['artwork_url']
# print the link to the song
singleDict['url'] = result['track']['permalink_url']
resultList.append(singleDict)
# add the full dictionary with title of the website pulling from
outerDict = {}
outerDict['Soundcloud'] = resultList
return outerDict
#interesting old bs4 attempt
# r = requests.get('https://soundcloud.com/charts/new?genre=all-music.json')
#
# soup = BeautifulSoup(r.text, "html.parser")
#
# count = 0
# for link in soup.find_all(lambda t: t.get('itemprop','').startswith('name')):
#
# link = str(link)
# # get the link to the actual song
# tuple = link.partition('<a href=\"')
# trackURI = tuple[2].partition("\"")[0]
# print('https://soundcloud.com' + trackURI)