Skip to content

Commit

Permalink
Resolve users IP, then filter spotify results. Avoids the dreaded 'Un…
Browse files Browse the repository at this point in the history
…available in your country'.
  • Loading branch information
Jonty committed Aug 24, 2011
1 parent 1e9261e commit b568b1a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ Known Bugs
----------

* Sporadically you might get tracks playing back in the wrong order, this is because the resolution callbacks are async and very occasionally return in the wrong order.
* Occasionally a track might fail to play due to country restrictions. This is because I've not actually implemented them - very VERY few tracks are not available in all current territories.
42 changes: 33 additions & 9 deletions spotiproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,45 @@

$returnData = array();

$country = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);

if (isset($aData['track']) && $aData['track']) {
if (is_array($aData['track'])) {
$aTrack = (array) array_shift($aData['track']);
$aTracks = $aData['track'];
} else {
$aTrack = (array) $aData['track'];
$aTracks = array($aData['track']);
}

$aArtist = (array) $aTrack['artist'];
foreach ($aTracks as $track) {

$aTerritories = explode(' ', (string) $track->album->availability->territories);

if (in_array($country, $aTerritories)){

$aArtist = $track->artist;
$artistName = '';

$returnData = array(
'uri' => $aTrack['@attributes']['href'],
'name' => $aTrack['name'],
'artist' => $aArtist['name'],
'length' => $aTrack['length'],
);
if ($track->artist->count() > 1) {
$aArtists = array();
foreach ($track->artist as $artist) {
$aArtists []= $artist->name;
}

$artistName = implode(', ', $aArtists);
} else {
$artistName = $track->artist->name;
}

$returnData = array(
'uri' => (string) $track['href'],
'name' => (string) $track->name,
'artist' => (string) $artistName,
'length' => (int) $track->length,
);

break;
}
}

}

Expand Down

0 comments on commit b568b1a

Please sign in to comment.