365 days of music

Projects 23 January 2009 | 1 Comment

At the start of 2009, I launched a mini-project called 365 days of music. I put this together in about a month and it’s not nearly complete, but there it is for now (maybe forever).

The idea was that I wanted to share songs I like on the go and have an easy way to keep track of what I recommended. Initially, I wanted the website to play the songs from an embedded player, but I didn’t find any good APIs to accomplish what I needed. Instead I settled for linking to Rhapsody, where you can play the songs for free if you’re in the US and other websites if you’re not.

Here is how it works:

  • I send an e-mail to a super-secret special address with the following format:

    Subject: Song Title
    Body:
    by Artist
    from Album
    rating 1-5
    Notes

    Everything but the name of the song is optional.

  • A PHP script that I wrote fetches the e-mail, parses it, formats it for the website by inserting the HTML and links for downloading the song, and sends the formatted post to WordPress via the Postie plugin.

    I have the script set up to fetch only one message at a time and I have a cron job set up that runs the script once each day. This lets me queue up songs and have one posted each day.

And that’s all there is to it. The best part is that all I had to learn was how to get and send e-mail through PHP.

Here is the code (with my secret account and formatting details removed):

// connection info
$host = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$user = "account@gmail.com";
$pass = "password";

// open the connection
$mbox = imap_open ($host, $user, $pass)
    or die("Can't connect: " . imap_last_error());

// get the headers
$headers = imap_headers($mbox);
$found_new_post = false;

if ($headers == false) {
    // no headers in mailbox, so don't send anything
    echo "No new messages to process.";
}
else {
    for ($i = 1; $i <= count($headers); $i++) {
        // get the header
        $header = imap_headerinfo($mbox, $i);
        // get the text of the body
        // -- note: hard-coded for messages without attachments
        $body = imap_fetchbody($mbox, $i, "1");
        // only process oldest unseen message
        if ($header->Unseen == "U") {
            // message parsing and set-up goes here -->
            $post_title = trim($header->subject);
            $post_info = explode("\n", $body);
            $song = $post_title;
            $artist = "";
            $album = "";
            $rating = "";
            $comments = "";

            for ($j = 0; $j < count($post_info); $j++) {
                $line = $post_info[$j];
                if ($line == ":end") break;
                list($keyword, $value) = split(" ", $line, 2);
                if ($keyword == "by") $artist = trim($value);
                else if ($keyword == "from") $album = trim($value);
                else if ($keyword == "rating") $rating = trim($value);
                else if ($keyword != "")
                    $comments = $comments." ".trim($line);
            }

            $post_body = "";
            // etc...
            // <-- end message set-up

            // mark message as read
            imap_setflag_full($mbox, $i, "\\Seen");
            $found_new_post = true;
            break;
        }
    }
    if (!$found_new_post) echo "No new messages to process.";

}

// message processing goes here -->

// send email, to be processed by postie
$to = "another-account@gmail.com";
mail($to, $subject, $post_body, null, "-faccount@gmail.com");

// <-- end message processing

imap_expunge($mbox);
imap_close($mbox);

There may be a better way to accomplish this, but my goal was to get something set up as quickly as possible. Customizing the WordPress theme took longer than writing the script, which makes me happy.

So there we go. A quick little project, up and running. I’ll probably add things to the website if I have time, but for now, I’m happy with it.

Oh yeah, there’s also a special iPhone version of the website. Check it out on your iPhone.

One Response on “365 days of music”

  1. I like the layout of your blog. It is really awesome and I’m thinking about a similiar design for my site!

Leave a Reply