Kastang Ramblings of a Geek

11Aug/100

Push Twitter Replies to your iPhone using Prowl

Yesterday I bought Prowl for the iPhone. Prowl is similar to Growl on OSX systems. Prowl pushes notifications to your iPhone or iTouch. One of the first uses I thought to use Prowl for was pushing my @replies and mentions from Twitter to my iPhone. I wanted to run the script from my Ubuntu server rather then keeping my desktop on 24/7. I did a quick search online and didn't find any command line options for achieving such a task. I decided to code a quick and dirty PHP script to accomplish what I wanted to do.

This is a quick hack. There are probably more efficient ways of accomplishing this task. I used ProwlPHP to link to the Prowl API.

I assume you know PHP and know how to navigate in Terminal. This is a command line app, it will run on any computer/server running PHP. I have extensively commented the script. Hopefully it is easy to follow along.

Quick Instructions:

  1. Download the newest version of ProwlPHP and copy it a directory.
  2. Create a file lastreply.txt and stick in in the same directory as the below code will be copied into. This file needs to have read and write permissions.
  3. Copy the below code into another file. Alter the ProwlPHP includes directory on line 2, and add your Prowl API key, Twitter Username, and Twitter Password in the constructor.
//CHANGE THIS PATH TO WHERE ProwlPHP IS LOCATED ON
//YOUR SERVER
include('../API/ProwlPHP.php');
$t = new Twitter();
 
/**
 * @class Twitter
 * This class integrates Twitter and Prowl notifications with the
 * iPhone. When this script runs it checks to see if any new
 * Twitter mentions have occured on your account since last check.
 * If any exist, the Tweet is sent to Prowl and will be notified
 * via push on your iPhone.
 *
 * This class uses ProwlPHP located at:
 * 			http://github.com/Fenric/ProwlPHP
 *
 * @author Kastang (josh dot kastang at gmail dot com)
 */
class Twitter {
 
    var $xml;
    var $lastID;
    var $tUser;
    var $tPass;
    var $prowl;
 
    /**
     * Constructor for Twitter Class. Three lines need to be edited
     * below before running the file: prowl, tUser, and tPass.
     */
    function __construct() {
 
        //EDIT THE 3 LINES BELOW
        $this->prowl = new Prowl('YOUR PROWL API KEY');
        $this->tUser = "TWITTER USERNAME";
        $this->tPass = "TWITTER PASSWORD";
 
        //XML info loaded from Twitter API.
        $this->xml = simplexml_load_string($this->getReplies());
 
        //Opens the lastreply file which contants the
        //id of the last mentioned tweet.
        $this->lastID = file_get_contents("lastreply.txt");
 
        //If the file is empty (probably the first time
        //you are using the script). It will pull the
        //newest mention id from your twitter feed and
        //store it in the file.
        if ($this->lastID == null) {
            $this->lastID = $this->xml->status[0]->id;
            $full = "@" . $this->xml->status[0]->user->screen_name .
                    ": " . $this->xml->status[0]->text;
            $this->prowl($full);
            $this->updateNewest($this->xml->status[0]->id);
        }
 
        //Checks for new Twitter Mentions.
        $this->checkForUpdates();
    }
 
    /**
     * Checks for updates.
     */
    function checkForUpdates() {
        //first run boolean.
        $first = true;
 
        //For each mention in the XML array, check to see if
        //the current ID is greater then the last recorded ID.
        //If it is, push the current Tweet to Prowl, if it isn't,
        //check to see if it is the first run, if it is, break out
        //of the for loop, if it isn't the first run, update the
        //lastreply.txt file and break from the forloop.
        for ($i = 0; $i < 10; $i++) {             $curr = $this->xml->status[$i]->id;
            if ($curr > $this->lastID) {
                $first = false;
                $full = "@" . $this->xml->status[$i]->user->screen_name .
                        ": " . $this->xml->status[$i]->text;
                $this->prowl($full);
            } else {
                if ($first) {
                    break;
                } else {
                    $this->updateNewest($this->xml->status[($i - 1)]->id);
                    break;
                }
            }
        }
    }
 
    /**
     * Writes the newest @reply id to a file.
     */
    function updateNewest($id) {
        $file = fopen("lastreply.txt", "w");
        fwrite($file, $id);
        fclose($file);
    }
 
    /**
     * Push the Tweet to Prowl. This code is modified from
     * example.php in the ProwlPHP API Wrapper.
     */
    function prowl($tweet) {
 
        $this->prowl->push(array(
            'application' => 'Twitter',
            'event' => 'Reply',
            'description' => $tweet,
            'priority' => 0,
                ), true);
    }
 
    /**
     * Gets replies from Twitter. In order to grab replies, you
     * must be authenticated.
     */
    function getReplies() {
        $twitterHost = "http://twitter.com/statuses/mentions.xml";
        $curl;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERPWD, "$this->tUser:$this->tPass");
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($curl, CURLOPT_URL, $twitterHost);
        $result = curl_exec($curl);
        curl_close($curl);
        header('Content-Type: application/xml; charset=ISO-8859-1');
        return $result;
    }
}

I chose to add an entry in my crontab to run this script every 10 minutes. The time can be adjusted to suit your needs. Personally I do not see a need to ping Twitter more then once every ten minutes.

*/10 * * * * curl /path/to/replies.php

If all goes well, you should see something like this when someone mentions you in a tweet:

Prowl pushing a notification to the iPhone.

Prowl pushing a notification to the iPhone.

It is possible to setup a redirection within Prowl to automatically launch your Twitter client of choice when you get a Twitter based notification. You can also open Prowl and view all notifications:

Prowl Main Screen

Prowl Main Screen

The script should be fairly easy to modify. I will probably add Direct Messaging next to my Prowl Push Notifications.

20Nov/080

Would you pay to use Twitter?

According to SitePoint:

Author and investor Guy Kawasaki posted a poll today asking, “How much would you pay to use Twitter?” Surprisingly, about 50% said they would pay some amount, the majority thinking the service worth $5 per month.

........

In fact, in an April poll on TechCrunch, about 25% of respondents said they would pay to have ads removed on Twitter if they service started putting ads in the feed (and 50% supported the idea of ads — which might be a better way to generate revenue than to charge end users directly).

The full article can be found here.

I would be willing to pay to remove ads from Twitter if they existed. I am not a huge fan of having to pay for the service though. I have always wondered how Twitter makes its money without ads or payment options.

Tagged as: No Comments
20Nov/080

Links for 11/20/08

27Oct/080

Combining Twitter and World of Warcraft

I recently finished a project of incorporating Twitter into my World of Warcraft guilds website. The ultimate goal of this feature was to have an additional way of communicating with those outside of the game who do not have regular access to the guild forums.

For those who don't know: Twitter is a service for friends, family, and co–workers to communicate and stay connected through the exchange of quick, frequent answers to one simple question: What are you doing? -Twitter.

Twitters limit of 140 characters gives the perfect amount of space to get the important details across while not giving too much room to stray off topic. One of the highlights of this project is that a Twitter account is NOT required to utilize this feature. The messages can be viewed by RSS feed, Sidebar on the guilds website, and of course on Twitter.

All "tweets" posted on the guild forums are under the account @WKto - The format of all posts are in the format of [Username] - Message. This seems to be working out great so far. Usernames are pulled the current Session the user is logged into on the forums. I chose to do it that way because usually forum names tend to match up with in game names.

The feature has been released less then 36 hours ago, but already seems to be catching on. I believe in the coming weeks it will become a great addition to the website!

10Jun/080

Twhirl and Growl

Growl and Twhirl

I am a recent convert to Twhirl. It is my new favorite Twitter client. I formally used Twitterific. Twitterific is a great client still in my opinion, but the ads were a bit distracting and I didn't want to pay the $15 dollars for the full version (Okay, so I may be a little cheap, but I can't see a reason to pay for something when there are free alternatives.) The only feature I see Twhirl missing is Growl. I asked @twhirl (on twitter) if supporting Growl was planned to be supported. As of now, its not and doesn't look like it is going to be anytime soon.

I believe the lack of support is a limitation of Adobe Air rather then Twhirl. Air tries to be multi compatible with different operating systems while Growl is a OSX only application. Hopefully some day Twhirl will offer support for Growl. Its not a ruiner for me personally, It has its own notification system that doesn't interfere with Growl. It would just be nice to have one notification system running since all other applications I use have support for Growl (where necessary).