Skip to content

Instantly share code, notes, and snippets.

@bert2002
Created August 18, 2014 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bert2002/f4c15b47943bb6109189 to your computer and use it in GitHub Desktop.
Save bert2002/f4c15b47943bb6109189 to your computer and use it in GitHub Desktop.
download all of your favorite songs from soundcloud
#!/usr/bin/perl
# script: download favorite songs from your soundcloud stream
# author: Steffen Wirth <s.wirth@itbert.de>
use WebService::Soundcloud;
use XML::Simple;
use HTTP::Cookies;
use LWP::Simple;
use Encode qw(encode_utf8);
no warnings 'utf8';
### settings ###
# create a new app -&gt; 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/";
# download service
my $url = "http://anything2mp3.com/de";
### script ###
my $cookie_jar = HTTP::Cookies->new(
file => 'lwp_cookies.txt',
autosave => 1,
ignore_discard => 1,
);
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
$ua->timeout(30);
$ua->cookie_jar($cookie_jar);
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'};
my $permalink = $item->{'permalink-url'};
# title and file
$title =~ s/\ /_/g;
my $dest_file = $file_path . $id . "_" . $title . ".mp3";
$dest_file = encode_utf8($dest_file);
# only download songs that are downloadable
if ($downloadable eq "true") {
unless (-e $dest_file) {
print "DOWNLOAD: $title" . "\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" . "\n";
}
} else {
# lets download tracks that we are not supposed to
unless (-e $dest_file) {
print "D0WNL04D $title" . "\n";
my $request = $ua->post($url,
{
url => $permalink,
op => 'Convert',
form_build_id => 'form-iKcdS_GJM5mRuRicuFJKS7wGB8oR7zbY6YbVeV4cjtM', # not sure how long this is valid
form_id => 'videoconverter_form',
});
my $html = $ua->get($url . '/kostenlose-online-soundcloud-youtube-mp3-converter');
$html = $html->content;
while ($html =~ m@(((http://anything2mp3.com/de/system/temporary/mp3/))\S+[^.,!? ])@g) {
$dl = $1;
$dl =~ s/\>\<span\>Click//g;
}
($file) = $dl =~ m!([^/]+)$!;
$file =~ s/\?download=1//g;
my $track = $ua->get($dl);
my $sound = $track->content;
open (TRACK, ">>$dest_file");
print TRACK $sound;
close(TRACK);
} else {
print "IGNORING TRACK $title" . "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment