Update support for Amazon coverart grabbing (patch from Václav Nováček <waclaw66...
authorJoshua Wise <joshua@h2so4.joshuawise.com>
Sun, 28 Feb 2010 07:11:02 +0000 (02:11 -0500)
committerJoshua Wise <joshua@h2so4.joshuawise.com>
Sun, 28 Feb 2010 07:11:02 +0000 (02:11 -0500)
player/aws_signed_request.php [new file with mode: 0644]
player/metadata.php
player/metadata_cover.php

diff --git a/player/aws_signed_request.php b/player/aws_signed_request.php
new file mode 100644 (file)
index 0000000..5c685a9
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+function aws_signed_request($region, $params, $public_key, $private_key)
+{
+    /*
+    Copyright (c) 2009 Ulrich Mierendorff
+
+    Permission is hereby granted, free of charge, to any person obtaining a
+    copy of this software and associated documentation files (the "Software"),
+    to deal in the Software without restriction, including without limitation
+    the rights to use, copy, modify, merge, publish, distribute, sublicense,
+    and/or sell copies of the Software, and to permit persons to whom the
+    Software is furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+    DEALINGS IN THE SOFTWARE.
+    */
+    
+    /*
+    Parameters:
+        $region - the Amazon(r) region (ca,com,co.uk,de,fr,jp)
+        $params - an array of parameters, eg. array("Operation"=>"ItemLookup",
+                        "ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
+        $public_key - your "Access Key ID"
+        $private_key - your "Secret Access Key"
+    */
+
+    // some paramters
+    $method = "GET";
+    $host = "ecs.amazonaws.".$region;
+    $uri = "/onca/xml";
+    
+    // additional parameters
+    $params["Service"] = "AWSECommerceService";
+    $params["AWSAccessKeyId"] = $public_key;
+    // GMT timestamp
+    $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
+    // API version
+    $params["Version"] = "2009-03-31";
+    
+    // sort the parameters
+    ksort($params);
+    
+    // create the canonicalized query
+    $canonicalized_query = array();
+    foreach ($params as $param=>$value)
+    {
+        $param = str_replace("%7E", "~", rawurlencode($param));
+        $value = str_replace("%7E", "~", rawurlencode($value));
+        $canonicalized_query[] = $param."=".$value;
+    }
+    $canonicalized_query = implode("&", $canonicalized_query);
+    
+    // create the string to sign
+    $string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
+    
+    // calculate HMAC with SHA256 and base64-encoding
+    $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
+    
+    // encode the signature for the request
+    $signature = str_replace("%7E", "~", rawurlencode($signature));
+    
+    // create request
+    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
+    
+    // do request
+    $response = @file_get_contents($request);
+    
+    if ($response === False)
+    {
+        return False;
+    }
+    else
+    {
+        // parse XML
+        $pxml = simplexml_load_string($response);
+        if ($pxml === False)
+        {
+            return False; // no xml
+        }
+        else
+        {
+            return $pxml;
+        }
+    }
+}
+?>
index 5da7607..41e3e9c 100644 (file)
        /* how much time must pass before we try searching for cover art again */
        $COVER_SEARCH_AGAIN = 86400;
 
-       $amazon_base_url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId="
-                   . "15BH771NY941TX2NKC02";
-       $amazon_review_url = $amazon_base_url . "&ResponseGroup=EditorialReview&Operation=";
+       include("aws_signed_request.php");
        require_once("../inc/base.php");
        require_once("metadata_cover.php");
        
+       function amazonlink($params)
+       {
+               $params["SubscriptionId"] = "15BH771NY941TX2NKC02";
+               $res = aws_signed_request("com", $params, "Access Key ID", "Secret Access Key");
+               
+               return $res;
+       }
+       
        /* metadata should not require locking of session */
        session_write_close();
 
        /* Queries amazon with the specified url, strict serach first and then a more careless one, 
         * will urlencode artist and albumname 
         * returns xml document or false upon failure */
-       function amazon_album_query($base_url, $artist, $album) {
+       function amazon_album_query($params) {
                $stype = array("Title", "Keywords");
                $artist = urlencode($artist);
                $album = urlencode($album);
                foreach($stype as $st) {
                        if(!amazon_wait())
                                return false;
-                       $xml = @simplexml_load_string(@file_get_contents($base_url . "&Artist=$artist&$st=$album"));
+                       $xml = amazonlink($params);
                        if($xml&&isset($xml->Items[0])&&isset($xml->Items[0]->Item[0]))
                                return $xml;
                }
 
 
        function get_cover() {
-               global $COVER_SEARCH_AGAIN, $amazon_base_url,$cover_providers;
+               global $COVER_SEARCH_AGAIN, $cover_providers;
 
                list($fp, $artist, $album) = init_album_artist_or_die();
 
        }
 
        function get_review() {
-               global $amazon_review_url, $COVER_SEARCH_AGAIN;
+               global $COVER_SEARCH_AGAIN;
 
                list($fp, $artist, $album) = init_album_artist_or_die();
 
                        }
 
                        if($xml&&isset($xml->asin[0])) {
-                               $res = @file_get_contents($amazon_review_url . "ItemLookup&IdType=ASIN&ItemId=" . urlencode($xml->asin[0]));
-                               if($res)
-                                       $res = @simplexml_load_string($res);
+                               $res = amazonlink(array("Operation"=>"ItemLookup", "IdType"=>"ASIN", "ItemId"=>"urlencode($xml->asin[0])"));
                                $asin = false;
                        }
                        else {
-                               $res = @amazon_album_query($amazon_review_url . "ItemSearch&SearchIndex=Music&Artist=" , $artist , $album);
+                               $res = @amazon_album_query(array("Operation"=>"ItemSearch", "SearchIndex"=>"Music", "Artist"=>"$artist", "Album"=>"$album"));
                        }
                        if($res) {
                                if($res&&isset($res->Items[0])&&isset($res->Items[0]->Item[0])) {
index 10e7b20..38d2947 100644 (file)
@@ -17,7 +17,6 @@
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-       $amazon_cover_url = $amazon_base_url . "&Operation=ItemSearch&SearchIndex=Music&ResponseGroup=Images";
        $cover_providers = array("directory_cover", "amazon_search_cover");
 
        /* If you want to use this you'll need php-gd as well */
@@ -30,7 +29,7 @@
        function amazon_search_cover($artist, $album) {
                global $amazon_cover_url, $metadata_dir;
 
-               $xml = amazon_album_query($amazon_cover_url, $artist, $album);
+               $xml = amazon_album_query(array("Operation"=>"ItemSearch", "SearchIndex"=>"Music", "ResponseGroup"=>"Images", "Artist"=>"$artist", "Album"=>"$album"));
                if($xml) {
                        if(isset($xml->Items[0])&&isset($xml->Items[0]->Item[0])) {
                                $item = $xml->Items[0]->Item[0];
This page took 0.021062 seconds and 4 git commands to generate.