Created
July 30, 2010 10:20
-
-
Save hubgit/500278 to your computer and use it in GitHub Desktop.
find a track in Amazon's MP3 download store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// documentation: http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=19 | |
class Amazon { | |
// Fill these in; the associate tag is optional | |
private $key = ''; // https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key | |
private $secret = ''; // https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key | |
private $tag = ''; // https://affiliate-program.amazon.com/ | |
private $host = 'ecs.amazonaws.com'; | |
private $path = '/onca/xml'; | |
function sign($params){ | |
uksort($params, 'strnatcmp'); | |
foreach ($params as $key => &$value) | |
$value = $key . '=' . rawurlencode($value); // http_build_query uses urlencode rather than rawurlencode | |
return base64_encode(hash_hmac('sha256', implode("\n", array('GET', $this->host, $this->path, implode('&', $params))), $this->secret, TRUE)); | |
} | |
function lookup($request) { | |
$query = implode(' - ', array($request['artist'], $request['album'], $request['track'])); | |
$params = array( | |
'Service' => 'AWSECommerceService', | |
'Version' => '2009-10-01', | |
'Timestamp' => date(DATE_ISO8601), | |
'AWSAccessKeyId' => $this->key, | |
'AssociateTag' => $this->tag, | |
'Operation' => 'ItemSearch', | |
'SearchIndex' => 'MP3Downloads', | |
'ResponseGroup' => 'ItemAttributes,Images', | |
'Keywords' => $query, | |
); | |
$params['Signature'] = $this->sign($params); | |
$xml = simplexml_load_file('http://' . $this->host . $this->path . '?' . http_build_query($params)); | |
if (!is_object($xml) || (string) $xml->Items->Request->IsValid != 'True') | |
return array(); | |
$items = array(); | |
if (!empty($xml->Items->Item)){ | |
foreach ($xml->Items->Item as $item){ | |
$asin = (string) $item->ASIN; | |
$attr = $item->ItemAttributes; | |
$meta = array( | |
'artist' => (string) $attr->Creator, | |
'track' => (string) $attr->Title, | |
'duration' => (string) $attr->RunningTime, | |
'trackno' => (string) $attr->TrackSequence, | |
'sample' => 'http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=' . $asin, | |
'info' => (string) $item->DetailPageURL, | |
); | |
foreach (array('LargeImage', 'MediumImage', 'SmallImage') as $image){ | |
if (isset($item->{$image})){ | |
$meta['image'] = (string) $item->{$image}->URL; | |
break; | |
} | |
} | |
$items[] = $meta; | |
} | |
} | |
return $items; | |
} | |
} | |
$api = new Amazon; | |
$items = $api->lookup(array('artist' => 'Nirvana', 'album' => 'Nevermind', 'track' => 'Smells Like Teen Spirit')); | |
print_r($items); | |
/* | |
Array | |
( | |
[0] => Array | |
( | |
[artist] => Nirvana | |
[track] => Smells Like Teen Spirit | |
[duration] => 301 | |
[trackno] => 1 | |
[sample] => http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B000V639BK | |
[info] => http://www.amazon.com/Smells-Like-Teen-Spirit/dp/B000V639BK%3FSubscriptionId%3Dexample%26tag%3D3Dexample%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000V639BK | |
[image] => http://ecx.images-amazon.com/images/I/51ntKGgKiDL.jpg | |
) | |
[...] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment