Last.fm activity now in Complexlife

Last.fm is one of the top, if not the best music service out there and it’s something I’ve been using for quite a while now. While last.fm includes things like groups, friends, forums and other aspects of a social network, your activities in these was never provided. In the latest version these activities (new friends, loved songs etc) were shown in the form of a little block in the sidebar but unfortunately there has been no way to grab that for use in Complexlife.

I knew there is a way to get these activities somehow as Friendfeed does show you when you love a track but having asked directly about it in the forum, I was told that getting this in a feed was just not an option.

Fortunately web-mastered stepped up and created a yahoo pipe which grabs the last.fm latest activities API and returns it as a feed. Very useful but unfrotunately I noticed that it wasn’t exactly in the format I needed. If I used that in Complexlife then you would only see the title (a generic “New activity from <username>”) and having no date info in the feed, b0rked the sort by date of Simplepie.

Thus I had to modify the pipe in a way that

  1. Made the content become the title of the post (removing any html which might break Complexlife)
  2. Provide the date in the RSS so that the item can be sorted

Creating the title

It took me a while to figure it out, but making the content become the title was the easiest part of it all. Since yahoo pipes provides a handy renaming function, all I needed to do is tell it to rename the content as the title

Renaming the content as Title
Renaming the content as Title

The first part copies the whole content as the date field. I will later use this get the date. The second part renames the current content to the title so that it is displayed in whole. If I didn’t do it, I would get only part of the content displayed with elipsis after 30 characters or so.

Now I need to remove all the html code the activity stream has in. As it includes links to my profile as well as to artists and tracks, this would break the link complexlife puts for each row. The only way to remove the unecessary parts is of course through regular expressions and yahoo pipes gives exactly that function.

Removing the unecessary parts from the content
Removing the unnecessary parts from the content

This part actually goes before the renaming/copying as I remove various parts I also don’t need from the date field. Thus when I copy, I save duplicating the work.
The most interesting part is the previous the last field, wherein I tell it to remove all html tags (anything between < and >) and all their settings (i.e. target=”_blank”). Thus I’m left with the title in plain text.

Creating the date

This was a more tricky part as not only does the last.fm activity stream API consider it a good idea to put the date on the content but it also puts it in as a relevant entry to the current date.

Putting the date in the correct field was the easy part as all I needed to do was copy the content in the date field and remove everything except the date. As right before the date there was a fullstop, it was fairly simple to use another regex to delete anything until the last fullstop

The regular expression removes everything until the last fullstop
With a little regex magic, anything except the date is gone

The more difficult part was to make the relevant fuzzy date to become something that is expected to be in an RSS feed, which is something very specific in time and format. Fortunately, I noticed that there is a specific module that does exactly that: Date builder. How convenient.

Unfortunately this module accepts only strings so I could not really parse my whole feed through it unless I had a loop. Oh wait…

With a loop I can make all date items in the correct format
With a loop I can make all date items in the correct format

Sweetness. Unfortunately one problem remains which is that because the date provided is always fuzzy, I end up getting a rolling date for the item each time the pipe is run. For example, an item which has a date of “one month ago” today will point to Aug 28, tomorrow on Aug 29 etc. There’s not much I can do about it other than wait until (and if) last.fm deems it worthy to give us a proper feed.

And now my new shiny feed is in exactly the format I need to have for Complexlife to use it. A little hacking later and version 0.9.8 is ready to go. The last.fm activity stream is finally lifestreamable 🙂

Reblog this post [with Zemanta]