2.1 Say Hello to RJ! (Profile Data)

First off, let's import the module and fetch some user profile data in XML format:

>>> import lastfm
>>> profile_xml = lastfm.user.profile_information("RJ", format=lastfm.FORMAT.xml)
>>> print profile_xml
<?xml version="1.0" encoding="UTF-8"?>
<profile id="1000002" cluster="2" username="RJ">
  <url>http://www.last.fm/user/RJ/</url>
  <realname>Richard Jones</realname>
  <mbox_sha1sum>1b374543545e01bc8d555a6a57c637f61f999fdf</mbox_sha1sum>
  <registered unixtime="1037793040">Nov 20, 2002</registered>
  <age>24</age>
  <gender>m</gender>
  <country>United Kingdom</country>
  <playcount>44547</playcount>
  <avatar>http://static.last.fm/avatar/0f4bda3a8e49e714c26ef610e2893454.jpg</avatar>
</profile>
>>>

Easy, isn't it. But it would be much easier, if we've got it objectified already. So let's request an etree object as return value:

>>> profile_dom = lastfm.user.profile_information("RJ", format=lastfm.FORMAT.etree)
>>> profile_dom
<<< <etree._ElementTree object at 0xb74be70c>
>>> profile_dom.getroot().get("username")
<<< 'RJ'
>>> profile_dom.xpath("//*/age")[0].text
<<< '24'
>>>

When you're requesting an ElementTree, the retrieved data will be parsed by the etree module without any modifications. You will have access to all fetched attributes and elements, but all values will be strings. Another way to retrieve pythonic return values is to request a Python object as provided by the lastfm module. These objects do some type conversion for you and provide additional methods for each object.

>>> profile = lastfm.user.profile_information("RJ")
>>> profile
<<< <Profile username='RJ'>
>>> profile.realname
<<< 'Richard Jones'
>>> profile.age
<<< 24
>>> profile.registered
<<< datetime.datetime(2002, 11, 20, 12, 50, 40)

As you can see, the format keyword isn't required, because it's the default format for all services that provide XML data and some of the values have been converted from strings to more pythonic types.

Note: Depending on the user's profile some of the available attributes have a None value, i.e. if a user hasn't give a real name, the realname attribute will be None.

The next section covers some of the methods provided by the Profile class.