Posting To Movable Type 3.3 Using Nokia Lifeblog

We’ve been installing Movable Type as a blogging solution at work for various sites recently.

We’d been using Typepad, the hosted version of Movable Type for a while, but wanted some extra flexibility and functionality.

One problem we came across was the lack of support for Nokia Lifeblog on Movable Type, compared to Typepad.

Thankfully Martin Higham has worked on this in the past, and even used my old notes on the Nokia Lifeblog Posting Protocol.

However, the current version of Movable Type is 3.3, and Martin’s work only extends to 3.2.

I took Martin’s code, which modifies AtomServer.pm and modified it so it works on Movable Type 3.3. You can download my Nokia Lifeblog compatible AtomServer.pm for Movable Type 3.3 here.

Make sure you follow Martin’s instructions for Movable Type 3.2 as the method is exactly the same and the same caveats apply (namely, this could well break other Atom tools that use your blog as it has to disable some WSSE authentication).

I hope you find it useful!

UPDATE: 22nd November 2006

With version 2.0 of Lifeblog and web upload functionality from Nokia phones, the authentication method has changed slightly. A new version of AtomServer.pm that works with Lifeblog 2.0 is now available.

UPDATE: 26th June 2007

Neils Berkers has been in touch to say he’s adapted the script so it now works with Movable Type 3.35, Lifeblogging Fixed.

iTunes Auto Updating Now Playing in VB2005

Previously I’ve covered how to link into the iTunes COM library from VB 2005 to show now playing information. This article assumes you have already read that, or have it to hand.

Taking this application further, wouldn’t it be useful for our application to automatically update itself when a track changes takes place on iTunes?

Well I hope you are saying yes, as it’s what I’m going to cover now.

iTunes can inform our application of changes of track if we choose to listen for it’s OnPlayerPlayEvent event.

If you are not sure what an event is, it’s basically a way for one program to tell another that something has happened in that program and the other program may wish to react to it. In this case, a different track has started playing in iTunes.

So how can we get our application to listen out for this? Well firstly we need to say that our programme is interested in these events. We do this by using VB 2005’s WithEvents keyword when we create our interface into iTunes.

So previously we did this using the following code.

Dim app As New iTunesApp()

Now we have to add in the WithEvents keyword as follows.

Private WithEvents app As New iTunesApp

Now our program can listen out for events from iTunes. You may have noticed I’ve changed Dim to Private. This is because for this example I’m going to use a Windows Application and not a Console application as before.

Next we have to create our event handler so we can react to the events iTunes is sending us.

The easiest way to do this is use the method generator at the top of the code view in Visual Basic 2005 Express Edition. My iTunes object is called app so we select this, then OnPlayerPlayEvent to create our event handler stub.

Creating an iTunes event in VB2005

This passes in the variable iTrack as an Object. We know this is really an IITTrack object, so we need to cast it as such.

Dim track As IITTrack
track = CType(iTrack, IITTrack)

This creates a new varible called track and casts the existing iTrack variable as an IITTrack using CType.
Doing this, we have easy access to the track data passed to us by iTunes in the event.
We need to do something with this information seeing as we’ve gone to all the trouble of asking iTunes for it. The easiest thing to do is to just display a small window with the name of the current track.

MsgBox(track.name)

Obviously this is a simplified version of a real program, but it should give you an idea of how to get events from a remote program into your VB2005 application.

iTunes Now Playing in VB 2005

As I’ve been using VB 2005 a lot more a work the past few weeks, I thought I’d rewrite my iTunes Now Playing console app in VB.

It does the same as the previous C# example, and again I’ve chosen to use the free version of the language and Microsoft’s Visual Basic 2005 Express Edition.

The first thing to do, is to make sure you have downloaded the iTunes SDK from Apple.

In Visual Basic, create a new Console Application. We need to add a reference to the iTunes COM library, so we do this by right clicking over our application in the Solution Explorer, and selecting “Add Reference…”. Select the COM tab, and scroll down to find the iTunes library. Mine was called “iTunes 1.7 Type Library”.

adding the iTunes COM object to VB 2005

Now for some code.

Firstly, like all good programmers we turn on VB’s strict mode to make sure we’re not using too many bad programming practices.

Option Strict On

Next we need to import the libraries we want to use. In this case, System> and iTunesLib.

Imports System
Imports iTunesLib

As we’ve created a Console App in VB, we can just drop the following code into the Main sub.

Firstly we need to create an iTunesApp object so we can get talk to iTunes. After that we need to get the current track.

Dim app As New iTunesApp()
Dim track As IITTrack = app.CurrentTrack

We should now have the details of the current track in the track object so we just need to print these to the console.

Console.WriteLine("Current Track: {0}", track.Name)
Console.WriteLine("Current Artist: {0}", track.Artist)

Finally, just so we can see the result we need to wait for the user to press return before we exit.

Console.ReadLine()

Fire up iTunes if it’s not already on and start a track playing. Now start your VB program, and it should tell you the current track title and artist as shown below.

iTunes now playing script

It’s as simple as that. Obviously I’m not checking for exceptions and errors as this is just an basic example.

Your final code should look something like this.

Option Strict On
Imports System
Imports iTunesLib
Module Module1
Sub Main()
Dim app As New iTunesApp()
Dim track As IITTrack = app.CurrentTrack
Console.WriteLine("Current Track: {0}", track.Name)
Console.WriteLine("Current Artist: {0}", track.Artist)
Console.ReadLine()
End Sub
End Module

iTunes Now Playing In Perl

After working on the C# iTunes Now Playing program, I thought I’d give the Perl Win32::OLE module a try.

This module is Activestate Perl‘s COM interface for Microsoft Windows.

After using Perl for nearly 8 years, I’ve never given it a try before, and it turns out to be surprisingly easy to use.

The following script works exactly the same way as the C# version.

Firstly we need to create a new iTunes.Application object.

my $iTunesApp = new Win32::OLE("iTunes.Application");

Next we get the current track and print the name and artist.

my $track = $iTunesApp->CurrentTrack;
print "Current Track: " . $track->Name . "nCurrent Artist: " . $track->Artist . "n"

And that’s it. Obviously we should be checking to make sure the objects were create correctly, but this is just a simple example, not live code.

So to recap, here’s the final working example code.

#!/usr/bin/perl

use strict;
use Win32::OLE;

my $iTunesApp = new Win32::OLE("iTunes.Application");
my $track = $iTunesApp->CurrentTrack;

print "Current Track: " . $track->Name . "nCurrent Artist: " . $track->Artist . "n"

iTunes Now Playing In C#

I’ve been playing about with the COM interface to Apple’s iTunes running on a windows machine with .NET installed.

My choice of language has been C# as it’s a bit more Perl like than VB. I’ve used Microsoft’s “free” Visual C# Express in this case.

The iTunes SDK is available from Apple and has all the documentation needed.

I’m going to run through how to get Now Playing information out of iTunes. The quickest way to do this is as a console application.

Start up Visual C# and create a new console application. I called mine NowPlaying.

You’ll need to add a reference to the iTunes come library. Do this on the righthand side of the C# project window by right clicking and selecting “Add Reference…”, selecting the COM tab, and finding the iTunes library.

The first thing you’ll need to in your code is to import the iTunes namespace to make the code look a little cleaner. We’ll also need the System interface for our console input and output.

using System;
using iTunesLib;

In your Main method, the first thing we need to do is to create an instance of the iTunesAppClass. I’ve rather originally called mine, app.

// Create a new iTunesApp object to use
iTunesApp app = new iTunesAppClass();

Next we need to get the current track. This is a simple attribute call to the app object.

// Get the current track from iTunes.
ITTrack track = app.CurrentTrack;

Finally we need to show the current track. To do this we call two attributes on our track variable, name and artist. There are plenty of other attributes we could call, but these are the two most useful for this example. See the SDK for more choice.

Once we have displayed our name and artist, we need to wait for the user to hit return. This is useful if our program wasn’t launched from a console window as it would end before we’ve had a chance to read any output.

// Display some info on the current track.
Console.WriteLine("Current Track: {0}rnCurrent Artist: {1}" , track.Name, track.Artist);
// Pause until we hit return.
Console.ReadLine();

That’s it, nice and easy.

Here’s the full .cs source code.

using System;
using iTunesLib;
namespace NowPlaying
{
class Program
{
static void Main(string[] args)
{
// Create a new iTunesApp object to use
iTunesApp app = new iTunesAppClass();
// Get the current track from iTunes and return its artist and name.
IITTrack track = app.CurrentTrack;
Console.WriteLine("Current Track: {0}rnCurrent Artist: {1}" , track.Name, track.Artist);
// Pause until we hit return.
Console.ReadLine();
}
}
}

Acme::Terror::UK Update

I’ve updated the Acme::Terror::UK module to version 0.02 on CPAN and added a new method called level

The new level method returns the current UK terror alert status in an easily comparible format.

my $t = Acme::Terror::UK->new();
if ($t->level() == Acme::Terror::UK::SEVERE) {
print "The current terror level is SEVEREn";
}

See the documentation with the module for more information.

On the back of this, Andy Kennedy released Acme::Terror::AU. As the Australian government refuses to release this information, his module always returns Acme::Terror::AU::UNKNOWN. πŸ™‚

Introducing Acme::Terror::UK

There is a lot of fuss in the press at present about the current terrorist threat to the UK.

The government has put the current threat level on a few of it’s websites. These are…

The US government has had provided it’s threat levels to the public for a while, and there are various ways to access this, incuding a Perl module called Acme::Terror.

Now the UK has this information online, I decided to provide the UK with it’s own version of this Perl module, which I’ve rather originally decided to call Acme::Terror::UK.

It’s really simple to use the module. For example, this small bit of code will fetch and display the current UK threat level.

use Acme::Terror::UK;
my $t = Acme::Terror::UK->new(); # create new Acme::Terror::UK object
my $level = $t->fetch; # fetch the current terror level
print "Current terror alert level is: $leveln";

The code goes off to the home office site behind the scenes and screen scrapes the page to get the current UK threat level as the UK government doesn’t currently provide and automated feed of this information. This makes the module vulnerable to any design changes on the page, but it works for now and that’s the main thing.

There are 5 levels, these are…

  • CRITICAL – an attack is expected imminently
  • SEVERE – an attack is likely
  • SUBSTANTIAL – an attack is a strong possibility
  • MODERATE – an attack is possible but not likely
  • LOW – an attack is unlikely

At the time of writing, Acme::Terror::UK informs me the current threat level is SEVERE.

Interesting Nokia Lifeblog Bug

Now here’s an interesting bug in Nokia Lifeblog application on my Nokia 3230 phone.

If you take a photo using the camera and then post it to the web using Lifeblog, it won’t then let you send the image via MMS. Instead, it complains “unable to send copyright protected item”, even though I own the copyright of my own picture.

This is running version 1.51.2 of the Lifeblog software.

Nokia’s Flickr Uploader Uses The Atom Protocol

Charlie has a photo of his back garden on his blog that was updated using the Flickr uploader on his Nokia N93 phone.

FYI, the ‘Flickr’ uploader is using the same protocol as Lifeblog. So, if you know how that is done, you can theoretically post to other Lifeblog compatible sites.

That’s really interesting, and I was pondering which protocol the Flickr uploader would use. Lifeblog uses the Atom protocol, and I have written code to take lifeblog entries and upload them to a blog in the past.

I just need to get hold of a nice new Nokia N93 phone to test my code still works with the new Flickr uploader.

Retargetting Selected Links With JavaScript

At work I have a new site that makes use of iframes to embed external content onto a page. The content is a blog, written by non technical members of staff who needed to be able to include links to external websites. They are happy with using <a href=" tags, but I didn’t want to worry them with target parameters in the HTML. We have to target _blank as we want the readers to keep the old site open and not open the content in the site framework.

The solution to this is to use a bit of JavaScript to manipulate the DOM and insert blank targeting to all blog entries.

As we control the HTML template of the blog, we made sure all the blog entries were wrapped by an enclosing div tag with a class called blogentry.

Here’s some example HTML. We have one link in the blogentry div we need to re target. The other link is not in a blogentry so has to be ignored.

<div class="blogentry">
<a href="http://www.robertprice.co.uk/">This link needs to open in a new window</a>.
</div>
<div>
<a href="http://www.robertprice.co.uk/">This link won't open in a new window</a>.
</div>

The JavaScript first has to get all div tags on the page. It does this by calling getElementsByTagName on the document. Once we have all the div tags, we have to iterate over them to make sure we only get the ones with the classname of blog entry.

var entries = document.getElementsByTagName('div');
for (var i in entries) {
if (entries[i].className == "blogentry") {
// insert target code here.
}
}

Now have our blogentry’s we need to find all the links inside and make sure they all target blank. We do this by calling getElementsByTagName on each node, then setting the target attribute to blank.

var targets = entries[i].getElementsByTagName('a');
for (var j in targets) {
targets[j].target="blank";
}

It’s as simple as that. The full code follows…

<script language="JavaScript" type="text/javascript">
<!--
var entries = document.getElementsByTagName('div');
for (var i in entries) {
if (entries[i].className == "blogentry") {
var targets = entries[i].getElementsByTagName('a');
for (var j in targets) {
targets[j].target="blank";
}
}
}
// -->
</script>