Archive for the ‘Twitter’ Category

‘tweetFromFile’ PHP Class

Posted in 'Code Snippets, PHP, Twitter' by James on March 2nd, 2009

This recently created PHP class is part of today’s ongoing struggle of trying to create a Twitter ‘drip’ that will automatically update Twitter at pre-defined intervals with status updates specified in a text file.

// (C) Copyright JAMES PADOLSEY
class tweetFromFile {
 
    private $curlHandle;
    private $updateFile;
    private $archiveFile;
 
    private function getNewStatus() {
 
        $upcomingTweetsFile = $this->files['upcoming'];
        $archivedTweetsFile = $this->files['archive'];
 
        $upcomingTweets_R = fopen($upcomingTweetsFile, "r");
 
        // Get upcoming Tweets:
        $contents = fread($upcomingTweets_R, filesize($upcomingTweetsFile));
        $splitContents = preg_split('/\n/', $contents, 2);
 
        // ARCHIVE OLD POSTS:
        $archive = fopen($archivedTweetsFile, "a");
        fwrite($archive, $splitContents[0]."\n");
 
        // Remove top line from upcoming:
        $upcomingTweets_W = fopen($upcomingTweetsFile, "w");
        fwrite($upcomingTweets_W, $splitContents[1]);
 
        // Clean up
        fclose($upcomingTweets_W);
        fclose($upcomingTweets_R);
        fclose($archive);
 
        return $splitContents[0];
    }
 
    public $files = array('upcoming' => '', 'archive' => '');
 
    public function __construct($username, $password, $filename) {
 
        $this->curlHandle = curl_init();
        $this->files['upcoming'] = $filename;
        $this->files['archive'] = 'ARCHIVE_' . $filename;
 
        // Shortcut:
        $ch = $this->curlHandle;
 
        curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
 
    }
 
    public function __destruct() {
        curl_close($this->curlHandle);
    }
 
    public function updateStatus() {
 
        $status = $this->getNewStatus();
 
        curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, "status=$status");
 
        $result = curl_exec($this->curlHandle);	
	$resultArray = curl_getinfo($this->curlHandle);
 
        if ($resultArray['http_code'] == 200) return true;
 
        return false;
 
    }
 
}
 
// ==================
// ===== USAGE ======
// ==================
 
$tweet = new tweetFromFile('twitterUsername', 'password', 'textfile.txt');
$success = $tweet->updateStatus();
if ($success) {
    echo 'Twitter updated!';
} else {
    echo 'Hmm, an error...';
}

The class will retrieve each new update from the specified text file. Each update needs to be on a new line; an example text file:

Blah blah blah, this is the first update...
And another update!
Third and last update!!! ARGH!

The PHP class will remove each update from this file when it’s added to Twitter so all you need to do is keep adding to the bottom of the file…

This won’t do the dripping automatically. You’ll need to setup a ‘cron job’ to request the script at selected intervals (e.g. every two hours). I’m rubbish at Apache and anything to do with the command line so I haven’t quite accomplished this part yet… maybe someone else wants to give it a go? ;)

Extending jQuery’s selector capabilities

Posted in 'JavaScript, Twitter' by James on December 11th, 2008
Extending jQuery’s selector capabilities

I’m sure you all know that it’s possible to create plugins and extend various aspects of the jQuery JavaScript library but did you know you could also extend the capabilities of it’s selector engine?

Well, you can! For example, you might want to add a new ‘:inline’ selector which will return those elements that are displayed inline. Have a look:

$.extend($.expr[':'],{
    inline: function(a) {
        return $(a).css('display') === 'inline';
    }
});

Using the above code, when you want to select elements that are displayed inline you can simply include it within the selector:

$(':inline'); // Selects ALL inline elements
$('a:inline'); // Selects ALL inline anchors

That was a pretty simple example but I’m sure you can see the endless possibilites that this enables! And, creating a custom jQuery selector couldn’t really be simpler!

Solving Twitter’s URL posting issue

Posted in 'JavaScript, Twitter' by James on October 25th, 2008
Solving Twitter’s URL posting issue

I’m one of the few people on this planet still using the Twitter web interface. I just cannot be bothered with any of those Adobe Air clients, they seem too intrusive. Twitter isn’t so great that it deserves it’s own process in Windows (sorry Twitter!). Plus, if I was using a client I’d feel like I’d have to tweet about every tiny insignificant thing that happens, which, although it is the point in twitter, would be a massive waste of time!

There are a few things which annoy me about the web interface though. The worst thing by far is that it’s very slow and sometimes just doesn’t work. There are also some posting issues; if I have a really long link which I want to share, it won’t let me press the "update" button (because my tweet is now over 140 characters) - but I know that Twitter will convert the URL to a tinyurl which will surely result in a tweet shorter than 140 characters but Twitter still insists on disallowing this action!

Cannot update Twitter

I got really tired of this, twitter wouldn’t let me tweet even though shortened versions of posted URLs would fit into the 140 character limit quite nicely! Out of desperation I decided to create a cool little GreaseMonkey script which solves this problem.

You can install the script here - (Sorry, it’s Firefox only, duh!)

JustTweetIt.com launched

Posted in 'General, News, Twitter' by James on October 7th, 2008
JustTweetIt.com launched

Dani McDaniel of ‘Anidan Design‘ and Adelle Charles of ‘Fuel your Creativity‘ recently launched ‘Just Tweet it’ (JustTweetIt.com), a site which offers twitter users the opportunity to submit themselves to a global directory of fellow tweeters. The directory is split up into various subdirectories sorted by interests & professions. Obviously I’ve put myself in with the ‘web developers’!

From the site:

Just Tweet It was created to make it easier for people using the popular micro-blogging service Twitter to find other “Tweeters” with similar interests.

For news and updates on the new site make sure you follow @justtweetit on Twitter.

A superb idea from Adelle and Dani and congrats to them on the successful launch! :)