magπ

A crafty, nommy, occassionally geeky blog-thing.

Learning the WordPress CLI: LastFM

One of the neato things about CLI2 is that you can create custom commands, just by dropping in an appropriately coded “command-{something}-inc.php” in your “wordpress-cli/usr/bin”. Rod provides a few sample commands which demonstrate the format for command metadata, plus how output should be returned.

I’m using the creation of a (very simple) Last.FM command to become acquainted with his API.

The command name itself appears to be taken directly from the file name. For instance, the existence of an empty text file called “command-lastfm.inc.php” will create a valid command “lastfm”, which does nothing.

Command scripts open with (optional) meta-data.

<code>
    [source:php]
    /*
    command_name
    @@
    Usage: $0 [argument]
    Synonyms: none
    Switches: none
    Description of this command.
    @@
    */
    [/source]
</code>

I suspect that this meta-data is pulled when CLI2 dynamically generates its command list. (This is easy to test, but me, I’m lazy =P)

Follow this by some fairly standard PHP. The most obvious gotcha (which really isn’t a gotcha, since Rod points this out in his readme) is that any output needs to be printed with @e(‘’);@, rather than a standard echo function.

To create the @lastfm@ command, I used the LastRSS Class to parse my Last.FM feed. LastRSS returns the feed in an array $rs, which I then processed like so:

<code>
    [source:php]
    e('
</code>
<h2>Most Recent Tracks on Last.FM</h2>
');
e('
<ol>');
    foreach($rs['items'] as $item) {
    e('
<li>
        <a href="'.$item['link'].'">
            '.$item['title'].'
        </a></li>
        ');
    }
    e('</ol>
    ');
[/source]

Et voilà!

This does not print play dates/times, because I haven’t figured out how to pull that data out of the feed. LastRSS provides a @$item[‘pubDate’]@, which contains the day of the week, but that is all I can find. More digging required.