Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. from soundcloud import Client
  2. from requests import get
  3. from os.path import join as path_join
  4.  
  5. class Downloader:
  6. def __init__(self, client_id):
  7. self.client = Client(client_id=client_id)
  8.  
  9. def download_track(self, track_id, location):
  10. track = self.client.get('/tracks/{id}'.format(id=track_id))
  11. stream = self.client.get(track.stream_url, allow_redirects=False)
  12. with open(location, 'wb') as f:
  13. f.write(get(stream.location).content)
  14.  
  15. def download_all_user_tracks(self, user, directory):
  16. print('Downloading all tracks from {}.'.format(user.username))
  17. user_tracks = self.client.get('/users/{user_id}/tracks'.format(user_id=user.id))
  18. print('Downloading {} track{}.'.format(len(user_tracks), 's' if len(user_tracks) != 1 else ''))
  19. for track in user_tracks:
  20. print('Downloading {}.'.format(track.title))
  21. title = track.title.replace('/', ':')
  22. self.download_track(track.id, path_join(directory, title + '.mp3'))
  23.  
  24. def get_user(self, user_link):
  25. user = self.client.get('/resolve', url=user_link)
  26. return user
  27.  
  28. def download_user(self, user_link, directory):
  29. self.download_all_user_tracks(self.get_user(user_link), directory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement