November 22, 2008, Saturday, 326

BTPWS-Album-Recommendation

From DBWiki

Jump to: navigation, search

Music Album Recommendation Web Service

Bitmunk allows anybody to access our album music recommendation service. This web service allows you to post the name of an artist that you like and a list of other related albums that are available on Bitmunk are returned to you. The URL for the recommendation web service is:

http://www.bitmunk.com/api/xml/recommend/albums

Your POST should contain one Bitmunk Contributor object serialized to XML. An example of Contributor XML is:

<contributor version="2.0">
   <name>Gogol Bordello</name>
</contributor>

The formatted POST request should look like this:

POST http://www.bitmunk.com/api/xml/recommend/albums HTTP/1.1
Host: www.bitmunk.com
Content-Type: text/xml
Content-Length: 74

<contributor version="2.0">
   <name>Gogol Bordello</name>
</contributor>

The following XML will be returned to you:

<?xml version="1.0" encoding="UTF-8"?>
<xml_response version="1.0">
 <media_info_list version="2.0">
  <media_info version="2.0" id="6492960" type="audio_album">
   <owner_id>58589</owner_id>
   <sellable>true</sellable>
   <status>claimed</status>
   <creator>Geoff Berner</creator>
   <title>The Wedding Dance of the Widow Bride</title>
   <sample_range>10:40</sample_range>
   <parent_media_ids></parent_media_ids>
   <child_media_ids></child_media_ids>
   <details>
    <release_date>2007-01-19</release_date>
    <category>audio</category>
    <distribution_formats></distribution_formats>
    <banned_countries></banned_countries>
   </details>
   <royalties>
    <payee version="2.0" id="10105">
     <amount version="2.0" currency="USD">5.965</amount>
     <source>sva</source>
     <reason>CD Baby Artist Royalty</reason>
     <media_id>6492960</media_id>
    </payee>
    <payee version="2.0" id="10104">
     <amount version="2.0" currency="USD">0.535</amount>
     <source>CD Baby</source>
     <reason>CD Baby 9% Distribution Fee</reason>
     <media_id>6492960</media_id>
    </payee>
   </royalties>
  </media_info>
   .
   .
   .
 </media_info_list>
</xml_response>

Code

You can use the following Python script to access the web service. This is a demonstration of how easy it is to use the Bitmunk Music Recommendation web service.

Copy the following Python script to a file called btp.py and make the script executable:

#!/usr/bin/env python
# This script calls a given Bitmunk service using the BTP
# protocol (HTTP POST).
import sys, httplib

##
# Calls a Bitmunk XML Web API service given a URL and a document to
# post to the given URL.
#
# @param url the url to post the document to on the local website.
# @param document the XML document text to POST to the given URL.
def call_bitmunk_ws(url, document):
    headers = {"Content-type": "text/xml",
               "Accept": "*/*"}
    conn = httplib.HTTPConnection("www.bitmunk.com:80")
    conn.request("POST", url, document, headers)
    response = conn.getresponse()
    print response.status, response.reason
    data = response.read()
    print data
    conn.close()

# The main entry point for the script
if __name__ == "__main__":
    url = sys.argv[1]
    xmlfile = open(sys.argv[2], 'r')
    document = xmlfile.read()
    xmlfile.close()
    
    call_bitmunk_ws(url, document)

Create the following data file, you can name the file contributor.xml:

<contributor version="2.0">
   <name>Gogol Bordello</name>
</contributor>

Run the following command:

python btp.py http://www.bitmunk.com/api/xml/recommend/albums contributor.xml

The XML text printed to the console should be the list of audio albums that are similar to the artist name that was posted to the URL.