Permalink
Browse files

added Amazon MP3 preview resolver

  • Loading branch information...
1 parent 441c237 commit d6571cfdd0a6306c215ce4909d22b133557486c7 alf committed with Dec 14, 2009
Showing with 95 additions and 0 deletions.
  1. +12 −0 contrib/amazon/amazon-resolver.conf.example
  2. +83 −0 contrib/amazon/amazon-resolver.php
@@ -0,0 +1,12 @@
+<?php
+
+// get these keys from https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=access-key
+
+$conf = array(
+ 'key' => '', // Amazon AWS Access Key - required
+ 'secret' => '', // // Amazon AWS Secret Access Key - required
+ //'tag' => 'amazonbrowser-20', Amazon Associates tag, for referrals
+ //'host' => 'ecs.amazon.com', // UK = 'ecs.amazon.co.uk'
+);
+
+
@@ -0,0 +1,83 @@
+#!/usr/bin/env php
+<?php
+
+require dirname(__FILE__) . '/../phpresolver/playdarresolver.php';
+
+class AmazonResolver extends PlaydarResolver {
+ protected $name = 'Amazon MP3 Preview resolver';
+ protected $targetTime = 500;
+ protected $weight = 20;
+ protected $localonly = FALSE;
+
+ private $key;
+ private $secret;
+
+ private $host = 'ecs.amazonaws.com';
+ private $path = '/onca/xml';
+ private $tag = 'amazonbrowser-20';
+
+ function __construct($conf){
+ parent::__construct();
+
+ foreach ($conf as $key => $value)
+ $this->{$key} = $value;
+ }
+
+ 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 resolve($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',
+ '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;
+
+ $items[] = array(
+ 'score' => 1,
+ 'source' => 'Amazon',
+ 'artist' => (string) $attr->Creator,
+ 'track' => (string) $attr->Title,
+ 'duration' => 30, // (string) $attr->RunningTime,
+ 'trackno' => (string) $attr->TrackSequence,
+ 'url' => 'http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=' . $asin,
+ );
+ }
+ }
+ return $items;
+ }
+}
+
+require dirname(__FILE__) . '/amazon-resolver.conf';
+$resolver = new AmazonResolver($conf);
+
+//exit(print_r($resolver->resolve((object) array('artist' => 'Yeah Yeah Yeahs', 'track' => 'Heads Will Roll'))));
+
+$resolver->sendResponse($resolver->getSettings());
+$resolver->handleRequest(fopen("php://STDIN",'r'));
+

0 comments on commit d6571cf

Please sign in to comment.