Archive for 'coding'

Just like starting over

Over the years, I have started a couple of last.fm projects. All have one thing in common: they provide a way to display covers on your site, and do it more elegantly than the standard last.fm widget. One of the things that really bugged me about my projects was the different pieces of code I maintain; and how difficult it becomes to support all these variants.

As we’re approaching the new year, here is my resolution: one codebase for all, starting today with a basic object that can be used as-is, or with some additional programming, for platforms as Wordpress and Drupal. This also means that my Wordpress plugins will be built on this code once it gets out of beta (hey, even Chrome did!).

In case you’re wondering: yes, the covers on the top right of this page are there thanks to this new code. And no, it’s not a Wordpress plugin or widget yet, I wrote it directly in themes/sidebar.php

I’m releasing this code under GPL, so it is available to use in your own projects. If you do, I ask you to

  1. keep my info in the javascript file
  2. change the last.fm API key
  3. let me know where you are going with it

Once installed, the javascript hands you one object you can configure and run, like this:

jQuery(document).ready( function() {

  // last.fm username
  lastFmRecords.setUser('hondjevandirkie');
  // id of the div element where script can add covers
  lastFmRecords.setPlaceholder('lastfmrecords');
  // image to show while loading images or when image not found
  lastFmRecords.setDefaultThumb('./lastfm_logo.jpg');
  // TODO: value is ignored at the moment
  lastFmRecords.setPeriod('smart');
  // TODO: value is ignored at the moment
  lastFmRecords.setStyle('lightbox');
  // number of covers, at the moment max. 50
  lastFmRecords.setCount(20);
  // refresh interval (get fresh data from last.fm)
  lastFmRecords.setRefreshMinutes(1);
  // GMT offset
  lastFmRecords.setTimeOffset(1);
  // log to console
  lastFmRecords.debug();

  // run
  lastFmRecords.init();
});

Most options come from my earlier projects; the refresh is new. And best of all, I found out how to get JSON from last.fm. This means jQuery can make the requests, so all requests now go directly from the browser to last.fm, without a data provider in PHP on your own server.

So what do you need to get this running?

  • include jQuery
  • include last.fm.records.js
    (see download link below)
  • a last.fm username (you’re free to use mine when you like Randy Newman)
  • a div on the page where the script can put the HTML in
    (default: <div id=”lastfmrecords” />)

You can download the beta of Last.Fm Records 3.1 here.

Geode

Mozilla is working on location awareness in browsers connected through a wireless network. For Firefox 3 a plugin was released, and it was suggested it will be in the core of Firefox 3.1. It seems silly to let your browser determine where your laptop is — as a co-worker put it, it’s at work or on his lap at home. And I can see the problem some people have with putting these extras in the core of a browser, especially when privacy is concerned.

Surely this won’t have the same use it has on mobile devices. But it finally makes the NSFW warnings superfluous.

Anyway, I enjoyed myself writing a demo showing wiki articles on places in your neighborhood.

mazeQuery

On “Signals vs. Noise” at 37signals a challenge was published — first person to solve the (image of the) maze gets a $50 dollar discount. The first (funny) comment was “I think there’s a jQuery plugin for that.” and this triggered something in me: I had to solve this in a browser.

The solution I built is solved in one browser for now, Firefox. The javascript copies the image to a canvas element and paints the solution on the canvas. Proudly I present: mazeQuery.

Loop alert

My stats software Woopra tells me Safari is now the browser for 10.6% of my visitors; it was 0.9% last week. As both Safari and Chrome are based on Webkit, it seems Woopra doesn’t report on Chrome yet.

This evening I was frustrated with an array in jQuery and as I was working in Chrome, I couldn’t use Firebug. So I tried to debug the old fashioned way: by displaying an alert in the each() loop. On the second alert box, Chrome added a checkbox: ‘Prevent this page from creating additional dialogs’. Really helpful.

Chrome

Just got home, downloaded Chrome and made sure my (heavy on the javascript) plugin Run For Cover works ok. It does. That’s egocentric browsertesting for ya.

It’s nice to have a couple of tabs open without the airconditioning going into overdrive. Let’s explore.

Run for cover 2.0.1

Back from my holiday in Germany (very nice, thank you). I’ve updated Run for cover to version 2.0.1 2.0.2 2.0.3, slightly less in beta now. It’s running in the sidebar. Changes made:

  • make it work in IE
  • option to show album title and artist next to image
  • option to use it with lightbox plugin (not included)

I’ll be answering some of the questions in the comments later, but now it’s Sunday evening and I’m off to bed to have a good night of sleep before my first day at work tomorrow.

Run for cover 2.0

The Run for cover widget has been abandoned for ages, as I only supported my Last.Fm Records plugin for Wordpress. Two things make me publish a new version of this Wordpress widget: disappointment from people stuck with PHP4 and therefore unable to use the new version of Last.Fm Records — and the joy of parsing XML with of jQuery…

Run For Cover shows the cds you listened to recently, as you can see in the sidebar. All data is real time, so the widget shows you if you are listening to a track at this moment. It’s still a rough version, especially the javascript, but it works on my test sites and I know people are looking for such a widget.

The widget needs jQuery, luckily this comes with recent versions of Wordpress. If you get an javascript error about jQuery, you could check the readme.txt .

You can download Run for cover 2.0 here.

Update: the javascript seems to fail under some versions of Internet Explorer. First thing when I’m back home after the holidays.

No project complete without a todo list

  1. better error checking
  2. show a message when last.fm is unavailable or when data was not correctly loaded
  3. use nice jQuery effects when displaying the cd covers
  4. auto update function after x minutes

Hope you like it — I’d like to read what you think in the comments.

New version and project plans

Just committed version 1.4.1 of Last.Fm Records to Wordpress, to fix a couple of issues. The biggest one is the one that Rose pointed out, and I hadn’t seen yet. Thanks, Rose! She saw that if you selected ‘get more images if necessary’, the plugin didn’t display the correct images.

I also removed the commented out code that was displayed on the settings page.

The feedback has been extremely encouraging to work on this plugin. For Last.Fm Records the todo list is the first thing I’m planning to do.

The stand alone version is next. As it is already PHP5, I suspect there’ll be less disappointed people. Don’t think I’ll start on this one before I’m off on a three week holiday.

And a special note for anyone stuck on PHP4: LFM used to be called Run For Cover. I’m using that name to work on a widget that uses jQuery to parse the recent tracks list from Last.Fm. It’s still very beta, but you can follow development on that one by downloading the development version. If you make sure jQuery is loaded (see readme.txt for instructions) and activate the widget, it will display your recent tracks.

Version 1.4 of Last.Fm Records released

This was a big one for me — implementing the new last.fm API. I have submitted my changes to wordpress.org, so you should see a message on the plugins page that the new version is available.

To make this happen before my holidays, I had to disable some things from the previous version:

  • uploading images (already uploaded images will remain in your cache directory)
  • selecting a style

Please let me know how this new version works for you in the comments! I’m especially interested in what you think of the new option ‘get more images if necessary’.

If any bugs show up I’ll try to fix them before this or another sundown.

Last.Fm API

Last.fm has made available their new API two weeks ago — and it’s a big step forward. I did apply for a preview to update my Wordpress plugin Last.Fm Records before the API hit the streets, but alas. So the last few evenings I’ve written version 1.4.

Version 1.3 had a really ugly parseHTML function to parse the HTML pages requested on last.fm. Now that the API returns relatively clean XML, the first thing I did was to update the XML parsing to the functionality available in PHP5, which means I no longer support PHP4. All the Amazon and Musicbrainz requests have been replaced by requests to the new API. Great news is that almost all relevant feeds in the new API include urls for cd covers: only user.getWeeklyAlbumChart (cds you listened to last week) is lacking those: why?

Thanks to the new API the plugin is much faster, finds more images than the previous version, and is now in beta. If you want to test drive it, let me know in the comments. I’ll release this new version after testing it thoroughly. I’ll be listening to songs with single quotes and ampersands in the title!

Oh, and yes, the request (by a human being) for a fallback option has been implemented: when the period you set in the options returns not enough cds, the plugin requests more info from longer ago.