Drupal Last.fm module - now works with Drupal 5!

Another update: I noticed today that this module had disabled my site. I'm not sure why, but I think it may be because Last.fm wasn't responding. I'm still using it, but if you have it on your site and everything goes bad, it might be because Last.fm isn't available. I manually disabled it in the system table for my Drupal install and my site came back to life, so I know it was this module that caused the problem. I need to do some more debugging before it's suitable for production, so use at your own risk, or exclude the block from the admin page so at least you can disable the module if it causes problems.

Update: I've just updated this slightly, so that the cached track listing is used if your recent tracks from Last.fm is blank. This avoids you getting an empty listing if you haven't been listening to music for a while. I also updated it a second time to prevent errors occurring if you get broken XML back from Last.fm (which happened to me once, and caused my whole site to crash).

I've had a couple of requests for my Last.fm Drupal module, in response to a previous blog post. This was also a good opportunity to learn how to write modules for Drupal 5 and learn the new forms API, which I've now done.

The new version is attached to the bottom of this post. It requires Drupal 5 and PHP 5 with the DOM extension enabled (this is used for parsing the XML feed from Last.fm). It won't check whether the DOM extension is enabled (it's not that clever yet), so it could cause issues (like your site breaking) if you try to install it on a system without PHP 5 and/or the DOM extension.

Install it as a standard module (drop into the modules directory, enable in Administer > Site building > Modules). I've integrated it with the admin. system (under Administer > Site configuration > Last.fm module settings), so you'll need to configure it there before it works. The settings required are:

  • Your Last.fm username.
  • The number of tracks you want to show (1-10).
  • The period for which you want to cache the list (5 minutes to 1 hour), or no caching. The listing is cached in your Drupal root directory (under the filename lastfm_cached.html) for efficiency. In cases where Last.fm doesn't respond quickly enough, you will get an error message (rather than it just leaving your site hanging).

Then turn on the block with the title Recent tracks in the block admin. pages. And away you go! As proof it actually works, see the left-hand side of this site, where it's in action.

AttachmentSize
lastfm.zip2.04 KB

Comments

Last.fm and Tabbo module

Hi,

I've just finished a Last.fm and Tabbo module for Drupal 6... I don't know how much yours exactly works (don't have a working D5 installation around and your demo is either turned off or malfunctioning), but it seems to be similar (one difference is that mine also supports Tabbo.

P.S. You need some sort of anti-spam measures. Captcha + reCAPTCHA modules have been working fine for me (one site got literally thousands of spam comments. I deleted them all and installed those two modules - not even one bastard got through).

Don't I know I need those

Don't I know I need those modules.

I turned off the Last.fm module, just because it made my site crash if Last.fm timed out, unless I hacked in some changes to the Drupal HTTP client code. Which I didn't fancy maintaining. Glad to hear you've done some work on a new module for it. Good luck with it!

Problem with multisite?

Hey there,

Very cool module - thanks for taking the time to work on it. I've only tried it on a multisite install so far and am getting an error:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in .../sites/default/modules/lastfm/lastfm.module on line 176

I realize this is a very early release, so it's not that big a deal, but should I be getting this error? I tried moving it to sites/domain.com/modules and got the same error. I have not tried it in a single Drupal install yet. Thanks!

Hello Arp. Are you using PHP

Hello Arp. Are you using PHP 4? I developed on PHP 5 and haven't tested on PHP 4 as I never use it. I get the feeling it might be due to that. In PHP 4, you can't chain together method invocations on objects, as this module does on the line where the error occurs. If you can, try running it under PHP 5 and see whether the problem goes away. I need to do some work to make it PHP 4 compatible, as I believe Drupal 5 still supports that PHP version.

Sponsorship and/or Development Help

Hi Elliot. We've got a client that is interested in doing Last.fm/Drupal integration on their site. They would probably sponsor a bunch of fixes/features for this module. So if you're looking for help/money for developing the modules (and perhaps not having it crash your site!), please contact me and I can give you more details.

Submitting to drupal.org?

Have you considered putting this into CVS on drupal.org? It's a module that quite a few others have mentioned a need for, and I'm pretty sure some other folks would be able to pitch in with patches for things like multi-user support. It could take advantage of drupal's built in caching system pretty easily, too... (http://www.lullabot.com/articles/a_beginners_guide_to_caching_data)...

Hi Jeff. I think that's a

Hi Jeff. I think that's a good idea, and I'd definitely be willing to do it. Only thing is I've got quite a bit on at the moment, so may not get round to it for a week or so. Any tips on the best place to start when thinking about contributing to Drupal?

The caching idea is also a great one. Looks like I could easily use that instead of my hokey caching to the filesystem. Thanks for the reference and the clear article.

Is it possible to have it for each user??

Basically, I want everyone that signs in to have the option to enter their last.FM username and have their feed display in their homepage AND in their user's page. Is this possible?

I imagine this is possible,

I imagine this is possible, but it's not something I need or have attempted yet. If you have patches, I'd be happy to incorporate them.

where is the code

you said:
"he new version is attached to the bottom of this post. "

I don't see it.

It's here. It only shows up

It's here. It only shows up in the full view of the post.

Thanks

Thanks a lot.

You're welcome, Artur.

You're welcome, Artur.