Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (119 sloc) 3.32 KB
<?php
class bandcampConnection {
private $bandcampKey;
private $bandName;
private $bandId;
private $bandcampApiUrl = 'http://api.bandcamp.com/api/';
private $bandcampApiVersion = 1;
private $discography = array();
function __construct($key = null, $bandName = null, $bandId = null) {
if(!$key || !$bandName) {
throw new Exception('You must specify a valid bandcamp API key or band name');
}
$this->bandcampKey = $key;
$this->bandName = strtolower($bandName);
if($bandId) {
$this->bandId = $bandId;
}
else {
// we'll need to fetch band_id from API directly
try {
$this->setBandId();
}
catch(Exception $e) {
throw $e;
}
}
}
public function getBandName() {
return $this->bandName;
}
public function getBandId() {
if(!$this->bandId) {
try {
$this->bandId = $this->setBandId();
}
catch(Exception $e) {
throw $e;
}
}
return $this->bandId;
}
private function setBandId() {
// this url declaration is a bit rubbish, but will do for now
$url = $this->bandcampApiUrl . 'band/' . $this->bandcampApiVersion . '/info?key=' . $this->bandcampKey . '&band_url=' . $this->bandName;
try {
$data = $this->getBandcampData($url);
$this->bandId = $data->band_id;
}
catch(Exception $e) {
throw $e;
}
}
public function getDiscography() {
if(!$this->discography) {
$url = $this->bandcampApiUrl . 'band/' . $this->bandcampApiVersion . '/discography?key=' . $this->bandcampKey . '&band_id=' . $this->bandId;
$discography = array();
try {
$ddata = $this->getBandcampData($url);
foreach($ddata->discography as $disc) {
$url = $this->bandcampApiUrl . 'album/2/info?key=' . $this->bandcampKey . '&album_id=' . $disc->album_id;
$rdata = $this->getBandcampData($url);
$discography[] = $rdata;
}
}
catch(Exception $e) {
throw $e;
return;
}
}
$this->discography = $discography;
return $discography;
}
public function getTracks($albumId) {
$get_album = false;
if(!$this->discography) {
$this->getDiscography();
}
$key = $this->_albumExists($albumId);
// since $key may be 0
if($key === false) {
throw new Exception('This album not in band discography');
return;
}
if(isset($this->discography[$key]->tracks)) {
return $this->discography[$key]->tracks;
}
else {
throw new Exception('Tracks could not be obtained for release');
}
}
/**
* Helper function that sends off various request to Bandcamp API
*/
private function getBandcampData($url) {
$stream = fopen($url, 'r');
if(!$stream) {
throw new Exception('Could not connect to Bandcamp server');
return;
}
$response = json_decode(stream_get_contents($stream));
if(!$response) {
throw new Exception('Did not get valid response');
return;
}
return $response;
}
/**
* helper function that iterates through the discography
* array to find an album with a particular id
*/
function _albumExists($albumId) {
foreach($this->discography as $key => $release) {
if($release->album_id == $albumId) {
return $key;
}
}
return false;
}
}
Something went wrong with that request. Please try again.