Skip to content

Instantly share code, notes, and snippets.

@chrisatomix
Forked from navinpai/download_bandcamp.php
Last active December 29, 2015 09:59
Show Gist options
  • Save chrisatomix/7653632 to your computer and use it in GitHub Desktop.
Save chrisatomix/7653632 to your computer and use it in GitHub Desktop.
Modified to support different data format, cleaner filenames and basic error checking.
<?php
/* I'm absolutely in love with BandCamp as a platform for musicians, both in terms of concept and UI/UX and the artists on on it.
The music is awesome, so wrote this small script to scrape the music off the album pages on BandCamp to your computer. Yes, they're 128K MP3s, not the higher def that the site offers for purchase, but beggars can't be choosers right? :P
Simple script, basic CURL-ing :)... Downloads songs to the songs folder. So folder structure should be:
|-download_bandcamp.php
|--songs
Coded while listening to 'As Of Yet Unnamed' by Mayhem, who's a personal inspiration (He's Renard Queenston AKA Renard, Adraen, Azrael, Furries in a Blender (formerly named Emoticon), Jackal Queenston, Kitsune, Klippa, Mayhem and NegaRen), for the numerous avatars of music he plays, each so awesome! :)
Check him out on: http://www.lapfoxtrax.com/ :)
*/
$playlist='http://lapfox.bandcamp.com/album/undertones'; //CHANGE LINK OF THE ALBUM .... Works ONLY FOR ALBUMS.... Scraping single songs, please figure out for yourself... It's very similar :)
if(preg_match('/[http:\/\/]{0,1}[A-Za-z0-9-]+.bandcamp.com\/album\/[a-zA-Z0-9-]+/',$playlist)) {
$curl = curl_init($playlist);
curl_setopt($curl, CURLOPT_URL, $playlist);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$header = curl_exec($curl);
curl_close($curl);
list($discard,$iwant)=explode('trackinfo :',$header);
list($thisisit,$discard)=explode('playing_from',$iwant);
$x='"trackinfo" :'.substr($thisisit,0,-6);
$y=preg_replace('/"(duration|track_num|is_downloadable|streaming|sizeof_lyrics|id)\"[:][0-9]+(\.[0-9]{1,5})?,/ ','',$x);
$z=preg_replace('/:(false|true|null)/',':"erm"',$y);
//var_dump(json_decode('{'.$z.'}'));
$songs=(array)json_decode('{'.$z.'}');
for ($i = 0; $i < count($songs["trackinfo"]); $i++) {
echo $songs["trackinfo"][$i]->title."\r\n";
$key = 'mp3-128';
$filename = 'songs/'.sprintf('%02d',$i+1).' - '.str_replace('?','',$songs["trackinfo"][$i]->title).'.mp3';
if(!file_exists($filename)) {
$musicfile = file_get_contents($songs["trackinfo"][$i]->file->$key);
file_put_contents($filename,$musicfile);
}
}
}
else
die("Not an album");
/*
PS Do read: http://bandcamp.com/faq#steal
And if you like an artist, do support them by buying their music... I plan to do that (when I have some money, ofcourse! :P)
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment