LWP::UserAgent::iTMS_Client - libwww-perl client for Apple iTunes music store


    use LWP::UserAgent::iTMS_Client;
    # search the Store   
    my $ua = LWP::UserAgent::iTMS_Client->new;
    my $listings = $ua->search( song => 'apples' );
    foreach my $song (@{$listings}) { print $song->{songName} }

    $listings = $ua->search(artist => 'Vangelis', song => 'long', 
      genre => 'Electronic');
    foreach my $a (@{$results2}) { 
      foreach (sort keys %$a) { print "$_ => ", $a->{$_}, "\n" } 

    # get my authorization keys
    my $ua = new LWP::UserAgent::iTMS_Client(
        account_type => 'apple',
        user_id => '',
        password => 'password',


This perl module implements a user agent which can interact with the Apple iTunes Music Store (iTMS). For example, this module could allow a perl program that would automatically get samples of new albums by a particular artist, or buy everything on a Top Ten list weekly.

LWP::UserAgent::iTMS_Client is a sub-class of LWP::UserAgent and implements the methods of UserAgent, but does so using Apple's officially undocumented protocols. Because these protocols change with revisions to iTunes, the modules may occasionally lag Apple's changes until this module, too, is updated.


    # set up new instance for anon search
    my $ua = LWP::UserAgent::iTMS_Client->new;

    # set up for login
    my $ua = new LWP::UserAgent::iTMS_Client(
        account_type => 'apple',
        user_id => '',
        password => 'password',
        gu_id => 'CF1121F1.13F11411.11B1F151.1611F111.1DF11711.11811F19.1011F1A1',

    Create a new LWP::UserAgent::iTMS_Client object.
    Options are:

        Either 'apple' or 'aol', this determines where the authentication 
        password is to be checked--on an AOL user database or with Apple's 
    user_id (required)
        User name, usually an email address.
        A user identifier used by iTMS for authentication. May be useful if
        accessing local key files.
        A user's machine identifier used by iTMS for authorization.
    password (required)
        The user's own (user typed, in iTunes) password.
        Default error handling is to croak(). This allows alternate behavior by
        passing the name of a routine which takes a single scalar argument, 
        the error message.

        This is a mandatory wait before deauthorizing a machine after
        a virtual machine is used to get user keys.
        The country where the user's account is based. Determines purchase 
        currency, etc.
        The directory where the drms keys subdirectory is located. Generally it
        may be best to allow the module to locate this by default.
        Determines if the drms subdirectory is called '.drms' or 'drms', again,
        best left to the default.
        Generally '/'. Path separator for the local OS.
        The default location for downloaded music files.
      '?term=beautiful balloon');

    Sends a request to the iTunes Music Store. The first argument is the URL, 
    the second is the parameter string.Handles encryption and compression, 
    then returns an HTTP::Response object, as an overloaded method of the base 
    LWP::UserAgent. Generally not called directly unless you know what you 
    are doing. 
    my $results = $ua->search(song => 'Day', composer => 'Lennon');
    print "Results for song => Day, composer => Lennon:\n";
    foreach my $a (@{$results1}) { 
        foreach (sort keys %$a) { print "$_ => ", $a->{$_}, "\n" } 

    my $results2 = $ua->search(artist => 'Vangelis', song => 'long ago');
    print "\nResults for artist => Vangelis, song => long ago:\n";
    foreach my $a (@{$results2}) { 
        foreach (sort keys %$a) { print "$_ => ", $a->{$_}, "\n" } 

    The following types of searches should be supported: album, artist, 
    composer, song, genre, all. If used, 'all' should override other 
    Log in to the iTMS, using parameters given in new().

    Get the keys from the Store. Attempts to be compatible with key locations 
    used by default by the Videolan project's media player (FairKeys 
    compatibility). This should generally be used with a gu_id known by the 
    user, preferentially one given as a 
    gu_id => 11111111.11111111.11111111.11111111.11111111.11111111
    (6 8-digit hex numbers separated by periods) argument to new.

    Deauthorize the machine used to get the keys.
    Create a temporary machine ID (you need to have one of your 5 machine 
    usages for iTunes available), get the keys with this virtual machine's 
    authorization, then deauthorize. Note that since this may result in an 
    additional key being created, you should limit the number of times you 
    do this. If you generally only purchase music on one or two machines 
    that do not change ID's, and only play copied music on your other 
    (iPod?) machines, once downloading your keys may be enough. The program
    will display a progress bar betwwen key retrieval and deauthorization. The
    optional argument is to allow custom display of the wait period, which by
    default prints to stdout. The optional callback routine must accept a single 
    argument which may have the values 'begin', an integer between 0 and 100, 
    and 'end'.
    Purchase and download a song, by song id number or 'songId' as a search 
    result (use search to find the song and then get the songId from the 
    search result data structure, or "dict" hash reference). Should call the 
    B<download_songs> method automatically after the purchase.

    Download any songs pending for the user, including those just purchased. In order 
    to download songs purchased but not immediately downloaded, should be called 
    after B<pending_downloads> is called.
    my $hashref = $ua->pending_downloads;
    Get a hashref, keyed by downloadKey, of purchased, but not yet downloaded 
    songs. This data is also stored in the object, so that B<download_songs>
    may be called after this method call to download the songs found.
    Notify iTMS that downloads of purchased music during the login session 
    have been successful. After such notification, songs may not be available
    to be re-downloaded as entries with B<pending_downloads>.
    Download a preview for a song entry, if available. $song is a reference
    to the hash of data for a song returned by the search method.


The searches under 'artist' are currently crippled, due to a server issue. It seems that the artist name is seldom presently part of song metadata. There is a numeric artistId entry, but I don't currently have an index of artists for lookup.

Overuse of the purchase routine might allow you to spend more on music than you intended. This might be a bug, from the perspective of your budget. (Enjoy :).


LWP::UserAgent, Audio::M4P, Mac::iTunes, Net::iTMS


William Herrera


Questions, feature requests and bug reports should go to <>.


    Copyright (c) 2003-2005 William Herrera. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

4 POD Errors

The following errors were encountered while parsing the POD:

Around line 784:

'=item' outside of any '=over'

Around line 953:

You forgot a '=back' before '=head1'

Around line 966:

'=item' outside of any '=over'

Around line 968:

You forgot a '=back' before '=head1'