Blog Workshop Wiki About Me
Your Mac can talk FeedBurner stats via PHP!

You have a blog and you are proud of it. Your sense of self-worth depends on how many people are following it. Making a detour to FeedBurner every day (the feed stats only update once a day) to check on your vitals is simple and does not take long (with a bookmark) but there has to be a more automated way.

What if you could just think “I wonder what my stats are today?” and your Mac would tap into your mind and give you the answer. It’s probably a good thing it has not advanced that far yet.

But what if you could ask your Mac and it would answer?

Well … It can! The needed speech tools are built right into Mac OS X.

By default, once enabled, you can voice-control some common features of most applications. You can also open Terminal and type:

say You look good today

These are two separate systems (speech recognition and speech synthesis). To have your Mac listen for some keyword and then answer with some intelligent information you need some plumbing code. Luckily it’s not hard to do at all and there are only a few steps. I’ll walk you through the setup.

Requirements

  • Mac OS X Tiger but it should work just as well on Leopard
  • PHP5
  • A text editor
  • I am also assuming you have Growl installed

Step 1:  Save and configure PHP script

Save the PHP script below in a file somewhere on your hard drive. You will need to know the absolute path to it. Lets say you save it as FeedBurnerStats.php in your user home directory. My path would be /Users/cadorn/FeedBurnerStats.php

Once saved you need to edit it to change the <username>, <password> and <feeduri> configuration options. That is your FeedBurner username and password as well as the unique URI of your feed that FeedBurner gives you. Your login information will be encrypted before it is sent so don’t worry about that.

If you have Growl installed this script will also show your stats in Growl. If you don’t, just set $growlnotify to false.

I am not going to run you through the code. It should be simple enough to follow. If you don’t know PHP don’t worry. This will still work for you.

<?php
/*******************************/
// Your account username
$user = '<username>';
// Your account password
$pass = '<password>';
// The URI of your feed (http://feeds.feedburner.com/<feeduri>)
$feedUri = '<feeduri>';
// Path to growlnotify if you want growl notifications
$growlnotify = '/usr/local/bin/growlnotify';
/*******************************/
 
$url = 'https://api.feedburner.com/awareness/1.0/GetFeedData?uri='
       . $feedUri;
 
$options = array(
    'http'=>array(
        'method'=>'GET',
        'header'=>'Authorization: Basic '
                  . base64_encode($user . ':' . $pass) . "\n"));
 
exec('say Fetching');
 
$xml = file_get_contents($url, false, stream_context_create($options));
 
$tree = simplexml_load_string($xml);
 
$msg = $tree->feed->entry[0]['circulation']
       . ' Subscribers and ' . $tree->feed->entry[0]['hits'] . ' Hits';
 
if ($growlnotify) exec($growlnotify . ' -m "' . $msg . '"');
exec('say ' . $msg);
?>

Step 2: Choose your speech synthesis voice

Go to your System Preferences and click on Speech under the System group.

Select the Text to Speech tab and choose a voice in the System Voice dropdown.

I like Vicki. She seems to be the clearest.

Step 3: Enable speech recognition


Click on the Speech Recognition tab. Ensure Speakable Items is set to On. You should now see this little tool:

Now check your Settings. I am using my internal microphone and have it set to continuously listen without needing a keyword before a command is spoken. Turn the command acknowledgement off as it will interfere.

Before we continue, ensure that the system is actually listening. Toggle your Listen Key until the bars (blue and green) in the speech tool move as you speak.

Step 4: Install your “statts” voice command

Click on the Commands tab and ensure Application Specific Items is checked.

Click on Open Speakable Items Folder.

In the Finder select the Application Speakable Items folder and select an application to enable the command for. It has to be attached to an application for it to work. I have mine in firefox-bin as I use Firefox all day.

Leave this folder open in your Finder and proceed below.

Save the following script into a file called statts.scpt on your desktop. Double-click the file you just created. It should launch the Script Editor.

tell application "Terminal"
	do shell script "/usr/bin/php /Users/cadorn/FeedBurnerStats.php"
end tell

Edit the path to PHP if it is different and update the path to your PHP script file we saved above. To find the absolute path to your PHP binary you can type which php in your Terminal window. Remember that this needs to be the PHP5 binary.

Go to the File menu and select Save As …

Now drag the folder you have selected in the Finder (see above) onto the Where dropdown in the Save As dialog. Save the script. It will be automatically compiled.

My file would now be at /Users/cadorn/Library/Speech/Speakable Items/Application Speakable Items/firefox-bin/statts.scpt.

NOTE: The name of the file is important. It will be your voice command. The double tt is not a mistake. The command needs to be written as it sounds.

At your command

Open the application you have enabled the command for and speak it out aloud. When the system recognizes your command it will show briefly above the speech tool and execute your script.

The script will say fetching, then get your stats via the FeedBurner Awareness API and tell you the number of subscribers and hits for the past 24 hours.

The statts word is pretty forgiving so you should not have too much of a problem having the system recognize you. If it is not working you can go to the Speech Recognition tab and under Settings try and calibrate your system. If it is recognizing other commands then you probably have an error in your Speakable Item Script or the PHP script.

While playing with this I noticed that the system does not recognize changes to the Speakable Item Script right away. It seems to index it every 10 seconds or so.

Now Innovate

This example may not be the most useful to you. The point is you now know how to trigger a PHP script via a voice command as well as have a PHP script talk to you.

Change the code, make it more useful. Have some fun with it. Who knows what you can come up with.

5 Responses

  1. Patrick

    Wow, this is a neat little trick. Thank you very much for this tutorial.

    Greetings

  2. Greg

    Thanks for writing this up. Pretty awesome! :)

    Just a heads up that the feedburner api url doesn’t seem to work anymore. No circulation numbers there. But if you use the new google url, it’s good:

    https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=

  3. Christoph Dorn

    Hmm. The old URL still works for me and the new URL does not!

  4. Rodrigo Fante

    The new url should work for the account that have changed to the new feedproxy Google url, if you’re using feeds.feedburner yet, so you must use the old one.
    I wrote the new URL “should” work for the new accounts because it is really unstable I really do not reccomend to use it for now, Google is really messing up with the feedburner.

Leave a Reply



  1. PHP Coding School » Blog Archive & ...

    [...] Your Mac can talk FeedBurner stats! By Christoph Dorn This example may not be the most useful to you. The point is you now know how to trigger a PHP script via a voice command as well as have a PHP script talk to you. Change the code, make it more useful. Have some fun with it. … Christoph Dorn - All around PHP - http://www.christophdorn.com/Blog [...]

Wordpress Forged
Creative Commons License
Copyright © 2008 - 2009 Christoph Dorn