Jan 22 2010

My Simple Twitter Program

One thing that’s kept me from diving into the social networking systems like Twitter and Facebook is their ephemeral nature.  Twitter saves posts for maybe two weeks, and that time frame is getting shorter as the service gets busier.  I don’t know how long Facebook saves things, but I don’t think there’s any easy way to search back through them anyway.

With older communications technologies like email and Usenet discussion groups, the ability to save messages was usually built into the program, if not the default.  I have every email and Usenet post that I’ve written going back eight years now, and I’d have them back to when I first got online in 1994, if not for the Great RAID Crash of 2002.  I also have every non-spam email that I’ve received in that time.

It’s not that I think my words are so important that they must be saved forever, but there have been numerous times that having that paper trail has helped me.  When I can’t remember exactly what I had planned for stage 3 of a project, or can’t find back the article I recommended to someone, a quick search through my archives turns it up.

So it bugs me that if I recommend a link or post a great joke to Twitter, it’s just going to disappear in a week or two and I won’t be able to find it back later.  I could bookmark everything I post or keep some kind of log, but that’s an extra hassle.  There may be Twitter clients that log posts automatically, but the clients I use don’t.

So I wrote a little program that takes any message I write, posts it to Twitter, and logs it, all in one step.  It also shortens any URL, using a URL shortening program I installed on my own domain.  One nice thing about that is I can see how many people fetch the real URL.  Those numbers are misleading, because some Twitter clients immediately fetch the URL whether the person ever clicks on it or not.  That’s the only explanation for why each one gets fetched 8-10 times in the first few seconds.  But after that, further fetches are probably real clicks.  That’s helped me to see what time of day most of my followers are online and clicking links, and when I’m probably talking to the ether.  And if any of my posts ever get retweeted and explode across the Twitsphere (or whatever it’s called), I’ll know.

Now that I have an ongoing log of things I post to Twitter, I have a place to go with those articles I think I’d like to read again later, or things I think are worth passing along, but I don’t have enough to say about them myself to be worth a blog entry.  If I want to read one again later, it’ll be easy to find back now, and I can stop having 30 browser tabs open with things I think I might want to write about when I have more time.

The script is pretty short, so I figured I’d share it.  Decent error correction is left as an exercise for the student.  It requires the module Net::Twitter, but otherwise is ordinary perl.  It’s free for any use without restriction.

#!/usr/bin/perl
use warnings;
use strict;
use Net::Twitter;
my $logdir = '/home/abaugher/.tweets';   # your tweet log directory
my $message = '';

while(<STDIN>){
    chomp;
    $message .= ' '. $_;
}

$message =~ s|(https?://\S+)|shortenurl($1)|eg;
if( length($message) > 140 ){
    die "Message still too long: $message\n";
}

my $nt = Net::Twitter->new(
    traits   => [qw/API::REST/],
    clientname => "Aaron's Tweets",  # This seems to be ignored currently
    username => 'your _twitter_username',
    password => 'your_twitter_password',
);
$nt->update($message);

open my $out, '>', "$logdir/".time;
print $out $message;
close $out;

sub shortenurl {
    my $url = shift;
    my $username = 'your_username';
    my $password = 'your_password';
    my $surl = 'http://www.butteredham.com/u/yourls-api.php';  # URL of your choice URL shortener
    use LWP::UserAgent;
    my $ua = new LWP::UserAgent;
    my $result = $ua->post($surl, {
        'username' => $username,
        'password' => $password,
        'action'   => 'shorturl',
        'format'   => 'simple',
        'url'      => $url,
    });

    if($result->is_success()){
        return $result->content();
    } else {
        die $result->status_line();
    }
}

If you enjoyed this article, why not rate it and share it with your friends on Twitter, Facebook, or StumbleUpon?

GD Star Rating
loading...

RSS feed for comments on this post. TrackBack URI

Leave a Comment

*

WordPress Themes