Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from soundcloud import Client
- from requests import get
- from os.path import join as path_join
- class Downloader:
- def __init__(self, client_id):
- self.client = Client(client_id=client_id)
- def download_track(self, track_id, location):
- track = self.client.get('/tracks/{id}'.format(id=track_id))
- stream = self.client.get(track.stream_url, allow_redirects=False)
- with open(location, 'wb') as f:
- f.write(get(stream.location).content)
- def download_all_user_tracks(self, user, directory):
- print('Downloading all tracks from {}.'.format(user.username))
- user_tracks = self.client.get('/users/{user_id}/tracks'.format(user_id=user.id))
- print('Downloading {} track{}.'.format(len(user_tracks), 's' if len(user_tracks) != 1 else ''))
- for track in user_tracks:
- print('Downloading {}.'.format(track.title))
- title = track.title.replace('/', ':')
- self.download_track(track.id, path_join(directory, title + '.mp3'))
- def get_user(self, user_link):
- user = self.client.get('/resolve', url=user_link)
- return user
- def download_user(self, user_link, directory):
- self.download_all_user_tracks(self.get_user(user_link), directory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement