我最近第一次开始使用apis,并且在使用soundcloud时遇到了麻烦。我试图生成喜欢特定曲目的用户列表所喜欢的曲目列表。我有代码单独作为get_favtracks和get_idlist工作,但是当我运行trackfavtracks时,我从服务器得到502错误。
以下是代码:
import soundcloud
#gets a list of favorited track ids for a given user id
#only returns IDs for tracks that are accessible to API
def get_favtracks(uid):
tracklist = []
client = soundcloud.Client(client_id='f3b669e6e4509690939aed943c56dc99')
favtracks = client.get('/users/'+str(uid)+'/favorites', order='id',limit=page_size, linked_partitioning=1)
for track in favtracks.collection:
tracklist.append(track.id)
#Same pagination code as get_idlist
if not hasattr(favtracks, 'next_href'):
None
else:
#if it does have multiple pages, we have to keep querying until we get a favlist object
#that doesn't have a next_href attribute.
newlink = favtracks.next_href
try:
while full == False:
newlist = client.get(newlink)
#for each page we get we add all the id's to the idlist
for track in newlist.collection:
tracklist.append(track.id)
if not hasattr(favtracks, 'next_href'):
full == True
else:
newlink = newlist.next_href
except AttributeError:
None
return tracklist
#Gets the id list of likers of a given track url
def get_idlist(page):
idlist = []
#create the client with the clientID
client = soundcloud.Client(client_id='f3b669e6e4509690939aed943c56dc99')
#get a track object from soundcloud using /resolve and by sending them a track url
track = client.get('/resolve', url=page)
#get a user object from soundcloud using the user_id attribute of the track object that we just got
user = client.get('/users/'+str(track.user_id))
#Print the username and title of the track
print(user.username)
print(track.title)
#get the first list of likers of the track id, set the limit the the page_size variable(defined above), idk what order does
favlist = client.get('/tracks/'+str(track.id)+'/favoriters', order='id',limit=page_size, linked_partitioning=1)
#add each user id from the favlist to the idlst
for user in favlist.collection:
idlist.append(user.id)
#if the track favlist object given to us by soundcloud has the next_href attribute
#it means it has multiple pages. If it doesn't have multiple pages we're done
if not hasattr(favlist, 'next_href'):
None
else:
#if it does have multiple pages, we have to keep querying until we get a favlist object
#that doesn't have a next_href attribute.
newlink = favlist.next_href
try:
while full == False:
newlist = client.get(newlink)
#for each page we get we add all the id's to the idlist
for user in newlist.collection:
idlist.append(user.id)
if not hasattr(favlist, 'next_href'):
full == True
else:
newlink = newlist.next_href
except AttributeError:
None
return idlist
#define the page_size variable, which is a max 200 (set by SC), and determines how many
#results are one the pages that soundcloud is returning.
#You don't HAVE to define a page_size variable, you can just put in 200. but the tut did this
page_size = 200
full = False
url1 = 'https://soundcloud.com/imnaut/bad-gal-naut-bootleg'
uid1 = 10646255
#total likes = 2
url2 = 'https://soundcloud.com/etherealfamily/capt-pizza-faded'
#https://soundcloud.com/pr0j3c7alpha
uid2 = 17933002
url3 = 'https://soundcloud.com/michael_mason/let-it-fall'
uid3 = 31531684
#total likes = 657
#create a list of data to test on stuff later
url4 = 'https://soundcloud.com/ragelogic/why-dont-we-talk-anymore'
uid4 = 6523547
#total likes = 7916
url5 = 'https://soundcloud.com/zomboy/miles-away-ft-nefera'
uid5 = 2323997
#total likes = 25711
#takes a track url and returns the combined list of likes for every person that liked that song
def trackfavtracks(page):
dictoftracks = {}
client = soundcloud.Client(client_id='f3b669e6e4509690939aed943c56dc99')
listofids = get_idlist(page)
for id1 in listofids:
listoftracks = get_favtracks(id1)
for id2 in listoftracks:
if id2 not in dictoftracks.keys():
dictoftracks[id2] = 1
if id2 in dictoftracks.keys():
dictoftracks[id2] += 1
return dictoftracks
#print(len(get_idlist(url2)))
#print(len(get_favtracks(uid2)))
print(len(trackfavtracks(url2)))
print("Done")