Archived entries for coding

New version of Last.fm records

I have released version 1.5.1 yesterday:

http://wordpress.org/extend/plugins/lastfm-records/

What has been done?

* complete rewrite, can be used under PHP4 again!
* most of the work is done in javascript, as the php code waited for a response from last.fm which slowed down your weblog
* support for wordpress 2.8/2.8.1, including the automatic updating
* you can set an auto refresh time (in minutes), if you do, the code will go back to last.fm and update the cd covers dynamically in the webpage
* you can specifiy your time offset from GMT
* new stylesheet built, as a starting point for your own splendid work

* the javascript in the zip at the Wordpress Plugin Directory can be used without Wordpress, see

http://jeroensmeets.net/projects/last.fm/

for info and a how to.

There’s still some work to do, and some things have changed:

* the option to use

<?php lastfmrecords_display(); ?>

has been dropped. Please use the javascript call instead.

* the option to use

[ lastfmrecords|overall|4]

in Wordpress pages & posts isn’t working correctly at the moment. Haven’t decided yet what to do about this, have no support requests about this. Anyone using it?

* the offset setting seems broken. Will look into that for version 1.5.4
* using other periods (last week, etc.) will be back in has been added to version 1.5.3

Een beetje van jezelf, en een beetje van jQuery

Ik was Yoursurvey aan het testen, een van onze online applicaties. Gebouwd om enquetes in elkaar te zetten en te versturen, en dat doet het prima. In dit geval was er iets mis op de laatste pagina van een enorme enquete — en ik moest het hele geval een aantal keer invullen om die laatste pagina te testen. En dan begin je te fantaseren over een stukje magische javascript.

jQuery schoot te hulp: dit scriptje vult ieder HTML formulier of enquete in. Sleep deze link naar je bladwijzerbalk en klik erop als je een formulier wilt testen. Als altijd is de versie 1.0beta.

Vul formulier met dummy data

Bij een klik op de bladwijzer wordt het formulier met dummy data gevuld en scrollt de pagina helemaal naar beneden. Er wordt alleen (nog) niet op de “volgende pagina” knop geklikt.

Setup Postfix to forward incoming email to PHP

>Setting this up took me a couple of minutes — finding the reason why it didn’t work a couple of hours. The question has hundreds of hits on Google, a simple answer hasn’t. Hope this helps someone somewhere you.

Create script to parse incoming mail

<?php
  $data = file_get_contents("php://stdin");
?>

Add a new email address to Postfix configuration

sudo vi /etc/aliases

and add the following line (everything on one line)

email+to+redirect: "| php -q /home/user/full/path/to/your/new/script.php"

Apply the changes to the aliases by running

newaliases

To make sure Postfix will pick up the newly added alias soon. If you’re in a hurry, do a

/etc/init.d/postfix reload

Aaaaaah!

One thing that took some time to figure out: my php script ran ok from the prompt, I had supplied it with test data and I saw all the expected data in my database. Yet, when I triggered the script by sending an email to the new address, Postfix bounced it right back at me, saying

The e-mail system was unable to deliver the message, but did not report a specific reason. Check the address and try again. If it still fails, contact your system administrator.

< xxx.yyy #5.0.0 X-Postfix; Command died with status 255: " php -q /home/user/full/path/to/your/new/script.php">

In the end, the reason for this error is simple. It is Postfix telling me PHP stopped executing the script as it found an error. In my case, the error occured when the script ran as user Nobody. When tested from the prompt, I didn't get the error, as the user had sufficient rights.

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.
UPDATE: see this page for the javascript file. It’s in the Wordpress package, but can be used alone.

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.

UPDATE (2009/05/23): it works in Chrome 2.0 on Windows.Op het “Signals vs. Noise” weblog van 37signals stond een uitdaging — de eerste die een (plaatje van een) doolhof oplost krijgt een korting van $50 dollar. De eerste (grappige) reactie was vrij vertaald “Daar is geloof ik een jQuery plugin voor.” en dat maakte iets in me los: dit wilde ik in een browser oplossen.

Mijn doolhofoplosser werkt op dit moment in 1 browser: Firefox. De javascript kopieert het plaatje naar een canvas element en tekent de oplossing op de canvas. Trots presenteer ik: mazeQuery.

UPDATE (23 mei 2009): het werkt nu ook in Chrome 2.0 op Windows.

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.



Copyright © 2004–2010. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses Modern Clix, a theme by Rodrigo Galindez.