Created
August 14, 2020 23:30
-
-
Save mcprat/10d3d8f500db511bf2c0797b5ba2bf26 to your computer and use it in GitHub Desktop.
Soundcloud python API delete all tracks (python 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in the future look into not having to authenticate for other peoples public tracks | |
# using the id from the waveform url to add to http://media.soundcloud.com/stream/ | |
from time import sleep | |
import requests | |
import soundcloud | |
# The line that actually deletes tracks is commented out for safety | |
#instantiate session and get basic account status | |
SC = soundcloud.Client( | |
client_id='XXXXXXXXXXXXXXXXXXXXXXXXXX', | |
client_secret='XXXXXXXXXXXXXXXXXXXXXXXXXX', | |
username='[email protected]', | |
password='XXXXXXXXXXXXXXXXXXXXXX' | |
) | |
username = SC.get('/me').username | |
print 'Username: ',username | |
ID = SC.get('/me').id | |
print 'ID: ',ID | |
PubTrackCount = SC.get('/me').track_count | |
print 'Public Track Count: ',PubTrackCount | |
PrivTrackCount = SC.get('/me').private_tracks_count | |
print 'Private Track Count: ',PrivTrackCount | |
FavCount = SC.get('/me').public_favorites_count | |
print 'Liked Tracks: ',FavCount | |
PubPlaylistCount = SC.get('/me').playlist_count | |
print 'Public Playlists: ',PubPlaylistCount | |
PrivPlaylistCount = SC.get('/me').private_playlists_count | |
print 'Private Playlists: ',PrivPlaylistCount | |
#playlists = SC.get('/me/playlists') | |
#followings = SC.get('/me/followings') | |
#followers = SC.get('/me/followers') | |
#favorites = SC.get('/me/favorites') | |
# Get all tracks | |
TrackCount = PubTrackCount + PrivTrackCount | |
#print TrackCount | |
tracklist = [] | |
# first call to resource | |
tracks = SC.get('/me/tracks', order='created_at', limit = 200, linked_partitioning = 1) | |
# loops to get all tracks, 50 at a time | |
# conditions that arent working: | |
#while tracks.collection != None: | |
#while tracks.next_href != None: | |
while len(tracklist) < TrackCount: | |
#tracks = SC.get('/me/tracks', order='created_at', limit = 50, linked_partitioning = 1) | |
for track in tracks.collection: | |
tracklist.append(track) | |
#print tracklist | |
#print len(tracklist) | |
try: | |
tracks = SC.get(tracks.next_href, order='created_at', limit = 200, linked_partitioning = 1) | |
except AttributeError: | |
break | |
#print tracklist | |
#print len(tracklist) | |
# Get ID list, downloadable list, download URI list | |
idlist = [] | |
for track in tracklist: | |
idlist.append(track.id) | |
#print idlist | |
#print len(idlist) | |
ifdownloadable = [] | |
for track in tracklist: | |
ifdownloadable.append(track.downloadable) | |
#print ifdownloadable | |
#print len(ifdownloadable) | |
downURI = [] | |
for track in tracklist: | |
downURI.append(track.download_url) | |
#print downURI | |
#print len(downURI) | |
# Delete all tracks | |
# sleep in between to prevent HTTP 500 | |
# and retry if HTTP 500 occurs | |
# printing on successful API PUT Request | |
#except requests.exceptions.RequestException as e: | |
#print 'retrying...' | |
#continue | |
#class HTTPError(Exception): | |
#pass | |
for track in tracklist: | |
while True: | |
try: | |
sleep(2) | |
print 'deleted track number:',tracklist.index(track) | |
SC.delete(track.uri) | |
except requests.HTTPError: | |
break | |
break | |
print 'done!' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment