XML

Linking to Tracks in the iTunes Store

Although it would certainly still be an interesting example if I just showed you how to transform the iTunes music library into a format that can be shared and viewed on the Web, that doesn't serve enough of a practical purpose. It doesn't do you much good to share your music library with others unless they have a means of accessing tracks within the library and playing or purchasing the tracks themselves. For this reason, it's important to include links back to the iTunes music store for all of the tracks in your music library. And if you sign up to become an iTunes affiliate, you can even earn commissions on other people buying songs from your music list.

The trick to tying your music library to the iTunes music store involves being able to generate links to individual tracks within the store. These links vary a little based upon whether or not you are an iTunes affiliate attempting to earn commissions or if you just want to provide a link without any financial angle. I'm going to assume the latter because becoming an iTunes affiliate is somewhat of another topic.

To learn more about becoming an iTunes affiliate, visit the iTunes Affiliates web site at http://www.apple.com/itunes/affiliates/.

There are essentially three different kinds of links you're going to focus on in regard to the iTunes music store and your music library:

  • Link to a track (song)

  • Link to an album (CD)

  • Link to an artist

As you can see, these types of links are increasingly more general as you go from track to album to artist. The idea is that you can give visitors to your music library page the option of looking up a specific song on iTunes or browsing more generally based upon the album or the artist.

Every link to the iTunes music store begins with a common base search URL:

itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?

When entered by itself as you see it here, this link will simply launch iTunes and lead you to the main iTunes search page, as shown in Figure 13.2.

Figure 13.2. The base search URL for iTunes leads you to the main iTunes search page.

The figure should give you a clue as to how you're going to build links into the iTunes music store. By appending search fields onto the base URL, you are effectively initiating an iTunes search via a URL. There are three main search parameters you're interested in when it comes to initiating such a search:

  • songTerm Search for a song (track)

  • albumTerm Search for an album (CD)

  • artistTerm Search for an artist

You can use any combination of these search parameters to come up with a specific search URL that hones in on matching iTunes music. For example, to search for a specific song by a specific artist that appears on a specific album, you'll want to specify all three parameters. Following is an example of such a URL:

itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?songTerm=
Hoist That Rag&albumTerm=Real Gone&artistTerm=Tom Waits

Keep in mind that in XML code you must convert ampersand (&) characters to the & character entity.

In this example, the song "Hoist That Rag" on the album Real Gone by Tom Waits is searched on iTunes. The resulting song as found in iTunes appears in Figure 13.3.

Figure 13.3. An iTunes search URL for a specific song results in the song being selected in iTunes.

Of course, you can be more general if you want a search to focus on an individual album or artist, as opposed to a specific song. Following is a URL that searches for the artist Trashcan Sinatras.

itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?
artistTerm=Trashcan Sinatras

Just in case you've been following along without actually trying any of the examples, please feel free to enter some of these URLs into a real web browser to try them out. Also make sure to try altering the URLs to search for some music of your own. Of course, you'll need to have iTunes installed in order for any of the search URLs to work.