Instantly share code, notes, and snippets.

Embed
What would you like to do?
download favorite songs from your soundcloud stream
#!/usr/bin/perl
# script: download favorite songs from your soundcloud stream
# author: Steffen Wirth
use WebService::Soundcloud;
use XML::Simple;
no warnings 'utf8';
# create a new app -> soundcloud.com/you/apps/new
my $client_id = "YOUR_CLIEND_ID";
my $client_secret = "YOUR_CLIENT_SECRET";
# soundcloud username and password
my $username = "YOUR_USERNAME";
my $password = "YOUR_PASSWORD";
# download path
my $file_path = "/tmp/";
my $scloud = WebService::Soundcloud->new($client_id, $client_secret,
{ username => $username, password => $password, response_format => 'xml' }
);
# get access token
my $access_token = $scloud->get_access_token();
my $oauth_token = $access_token->{access_token};
# get favorites tracks
my $followings = $scloud->get('/users/' . $username . '/favorites');
$xml = XML::Simple->new;
$xml = XMLin($followings->content);
foreach my $item (@{$xml->{track}}) {
my $id = $item->{id}->{content};
my $title = $item->{title};
my $downloadable = $item->{downloadable}->{content};
my $downloadurl = $item->{'download-url'};
# only download songs that are downloadable
if ($downloadable eq "true") {
# download track
$title =~ s/\ /_/g;
my $dest_file = $file_path . $id . "_" . $title . "mp3";
unless (-e $dest_file) {
print "DOWNLOAD: $title ($id)" . "\n";
#my $path = $scloud->download($id, $file_path);
# verrrrrrry ugly way, but $scloud->download(); is not working.
# have fun with big files :)
my $track = $scloud->get($downloadurl);
my $sound = $track->content;
open (TRACK, ">>$dest_file");
print TRACK $sound;
close(TRACK);
} else {
print "IGNORING TRACK $title ($id)" . "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment