Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Amazon Product API to get ASIN of MP3 download using artist and title

I am new using Amazon's Product Advertising API, and for my first project with it, I am trying to get the ASIN of the MP3 download product of a particular song, based on the artist and title. I will eventually be using these ASINs to populate an Amazon MP3 Clips Widget.

I am using the PHP class from CodeDiesel.com to get started. It works just fine, and I have added the following function:

    public function searchMusic($artist, $title) {
        $parameters = array("Operation"     => "ItemSearch",
                            "Artist"        => $artist,
                            "Title"         => $title,
                            "SearchIndex"   => "Music",
                            "ResponseGroup" => "Medium");
        $xml_response=$this->queryAmazon($parameters);
        return $xml_response;
    }

Now, the trouble is, I can only seem to get albums this way. For instance, if I put in "Robert Randolph" for the artist, and "Colorblind" for the title, I get Robert Randolph and the Family Band's Colorblind album. If I search for a particular track, such as "Thrill Of It", Amazon can't find anything.

So what I need to do is first figure out how to make a query for track title. Then I need to figure out how to limit my results to just MP3 downloads. How can I do this?

If there is documentation on the topic, can you point me in the right direction? I have been reading through it, but don't see any parameters for what I want.

Thank you for your time.

like image 541
Brad Avatar asked Feb 16 '11 18:02

Brad


2 Answers

The documentation is oddly organized, and I have a hard time finding relevant information myself.

To look up mp3s, you have to change your SearchIndex parameter to 'MP3Downloads'. Then, instead of using "Artist" and "Track", you have to use "Keywords". Combine the artist and track values into one string for the "Keywords" property value. Also, try "MusicTracks" for SearchIndex, as you might get different results there as well.

This is a snippet from a working system I have that does a similar type of lookup.

    $params = Array(
        "Operation"=>'ItemSearch',
        "SearchIndex"=>'MP3Downloads',
        "ResponseGroup"=>'ItemAttributes,Tracks,Images',
        "Keywords"=>$track['title'].' '.$artist['name']
    );
like image 87
Chris Baker Avatar answered Oct 24 '22 08:10

Chris Baker


This was a problem I had as well. I worked from the same example which I heavily modified and incorporated into a whole collection of classes which I made mashups out of. What I really want is the previews from Amazon given an artist and album title. I'm going to keep working on it, now that I still have too much time on my hands. My mashup codebase is here:

My Mashup's and codebase

I keep getting referrals from this question, my solution is basically the same as Chris's, I have two methods/functions that do this in my code:

        /**
     * Return the tracks found on an album, have to page to get them all which this method does not do.
     * 
     * @param string $albumTitle 
     * @param string $artist
     * @return mixed simpleXML object
     */
    public function getMP3sForAlbumByArtist($albumTitle, $artist)
    {
        $searchTerm = $albumTitle . ' ' . $artist;
        $parameters = array("Operation"   => "ItemSearch",
                            "Keywords"    => $searchTerm,
                            "SearchIndex" => AmazonProductAPI::CATEGORY_MP3_DOWNLOADS,
                            "ResponseGroup" => AmazonProductAPI::RESPONSE_GROUP_TRACKS);

        $xml_response = $this->queryAmazon($parameters);

        return $xml_response;
    }



    /**
     * Return the tracks found on a song title and artist
     * 
     * @param string $songTitle 
     * @param string $artist
     * @return mixed simpleXML object
     */
    public function getMP3ForSongByArtist($songTitle, $artist)
    {
        $searchTerm = $songTitle . ' ' . $artist;
        $parameters = array("Operation"   => "ItemSearch",
                            "Keywords"    => $searchTerm,
                            "SearchIndex" => AmazonProductAPI::CATEGORY_MP3_DOWNLOADS,
                            "ResponseGroup" => AmazonProductAPI::RESPONSE_GROUP_TRACKS);

        $xml_response = $this->queryAmazon($parameters);

        return $xml_response;
    }

My code is downloadable from the above link or is in GitHub, it is based on older code examples I've updated. It is running OK on my host, but iTunes is better for previews of songs.

like image 33
Muskie Avatar answered Oct 24 '22 07:10

Muskie