master
flyingrub 2014-10-23 16:14:29 +02:00
parent 1b18d94200
commit 48faf8ac54
1 changed files with 56 additions and 30 deletions

86
scdl.py
View File

@ -4,7 +4,7 @@
Usage:
scdl.py -l <track_url> [--hidewarnings]
scdl.py --me [--hidewarnings]
scdl.py --myprofile [--hidewarnings]
scdl.py --mystream [--hidewarnings]
scdl.py --allmytrack [--hidewarnings]
scdl.py -h | --help
scdl.py --version
@ -51,11 +51,11 @@ def main():
if arguments["-l"]:
parse_url(arguments["<track_url>"])
elif arguments["--me"]:
my_stream()
elif arguments["--myprofile"]:
my_profile()
who_am_i()
elif arguments["--mystream"]:
download_my_stream()
elif arguments["--allmytrack"]:
allmytrack()
download_all_profile_track()
def get_config():
@ -69,7 +69,24 @@ def get_config():
path = config['scdl']['path']
os.chdir(path)
def allmytrack():
def who_am_i():
"""
display to who the current token correspond, check if the token is valid
"""
client = soundcloud.Client(access_token=token)
# make an authenticated call
try:
current_user = client.get('/me')
except:
print('Invalid token...')
sys.exit(0)
print('Hello',current_user.username, '!')
def download_all_profile_track():
"""
Download artist track &/or repost
"""
offset=0
client = soundcloud.Client(access_token=token)
# make an authenticated call
@ -77,27 +94,17 @@ def allmytrack():
response = wget.download("https://api.sndcdn.com/e1/users/%s/sounds.json?limit=1&offset=%d&client_id=9dbef61eb005cb526480279a0cc868c4" % (user_id, offset))
print(response)
def my_stream():
def download_my_stream():
"""
Download the stream of the current user
"""
client = soundcloud.Client(access_token=token)
# make an authenticated call
current_user = client.get('/me')
print('Hello',current_user.username, '!')
activities = client.get('/me/sounds')
activities = client.get('/me/activities')
print(activities.type)
def settags(track):
"""
Set the tags to the mp3
"""
print("Settings tags...")
user = client.get('/users/' + str(track.user_id), allow_redirects=False)
audiofile = my_eyed3.load(filename)
audiofile.tag.artist = user.username
audiofile.tag.album = track.title
audiofile.tag.title = track.title
audiofile.tag.save()
def get_item(track_url):
"""
Fetches metadata for an track or playlist
@ -124,19 +131,14 @@ def parse_url(track_url):
download_track(item)
elif item.kind == 'user':
print("Found an user profile")
download_user(item)
download_user_favorites(item)
elif item.kind == "playlist":
print("Found a playlist")
for track_raw in item.tracks:
mp3_url = get_item(track_raw["permalink_url"])
if item:
download_track(mp3_url)
else:
print("Could not find track " + track_raw["title"])
download_playlist(item)
else:
print("Unknown item type")
def download_user(user):
def download_user_favorites(user):
"""
Fetch users data
"""
@ -153,6 +155,17 @@ def download_user(user):
end_of_tracks =True
offset += 10
def download_playlist(playlist):
"""
Download a playlist
"""
for track_raw in playlist.tracks:
mp3_url = get_item(track_raw["permalink_url"])
if item:
download_track(mp3_url)
else:
print("Could not find track " + track_raw["title"])
def download_track(track):
"""
Downloads a track
@ -183,6 +196,19 @@ def download_track(track):
print('')
print(title + ' Downloaded.')
def settags(track):
"""
Set the tags to the mp3
"""
print("Settings tags...")
user = client.get('/users/' + str(track.user_id), allow_redirects=False)
audiofile = my_eyed3.load(filename)
audiofile.tag.artist = user.username
audiofile.tag.album = track.title
audiofile.tag.title = track.title
audiofile.tag.save()
def signal_handler(signal, frame):
"""
handle keyboardinterrupt