« I'm back to Movable Type! | Main | Google launched MyMaps »

Twitter API client written in Perl

Recently, Twitter has seemed to be in fashion among sensitive people all around the world. I'd like to catch up with the movement to avoid to be regarded as the wack.

For a starter, I've written command line client for Twitter. Probably, it's so-called reinvention of wheels, but not matter, because it's just a practice.

Its usage is:

$ perl twitter.pl <option> [status]

If it executed like below:

$ perl twitter.pl update 'blogging about a Twitter client which I wrote'

The client accessses Twitter and set current status "blogging about a Twitter client which I wrote".

For more options, execute the script without arguments.

$ perl twitter.pl
Usage:
      $ perl twitter.pl <option> [argument]

Options:
    update <status>
        Set your current status.

    friends
        Show most recent status of those who you marked as friends.

    friends_timeline
        Show the timeline of those who you marked as friends.

    public_timeline
        Show the public timeline.

    followers
        Show the timeline of those who follow your status.

Stupidly, the script now doesn't treat multibyte strings correctly. Well, anyway, it's alright, just a practice ;)

Here's the source code of twitter.pl:

#!/usr/bin/perl

use strict;
use warnings;

use YAML;
use Switch;
use Pod::Usage;
use Net::Twitter;
use File::Spec;
use File::HomeDir;
use Term::Prompt;

@ARGV or pod2usage(1);

my $config    =  prepare_config();
my $twitter   =  Net::Twitter->new(
    username  => $config->{username},
    password  => $config->{password},
);
my ($command) =  grep { $ARGV[0] eq $_ }
    qw(update friends friends_timeline public_timeline followers) or pod2usage(1);

$command eq 'update' && !$ARGV[1] && pod2usage(1);

if (my $res = $command eq 'update' ? $twitter->can($command)->($twitter, $ARGV[1])
                                   : $twitter->can($command)->($twitter)) {
    switch ($command) {
        case qr/timeline$/ {
            for (@{$res}) {
                printf "%s: %s\n", $_->{user}{name}, $_->{text};
            }
        }
        case 'update' {
            printf "Set status: %s\n", $ARGV[1];
        }
        else {
            for (@{$res}) {
                printf "%s: %s\n", $_->{name}, $_->{status}{text};
            }
        }
    }
}
else {
    die "Failed to request: $command"
}

sub prepare_config {
    my $path   = File::Spec->catfile(File::HomeDir->my_home, ".twitter");
    my $config = eval { YAML::LoadFile($path) } || {};

    my $save;
    while (!$config->{username} || !$config->{password}) {
        $config->{username} = prompt('x', 'username: ', '', '');
        $config->{password} = prompt('p', 'password: ', '', '');
        $save++;
    }

    YAML::DumpFile($path, $config) if $save;
    chmod 0600, $path;

    return $config;
}

__END__

=head1 NAME

twitter.pl

=head1 VERSION

This is version 0.01

=head1 SYNOPSIS

  $ perl twitter.pl <option> [argument]

=head1 OPTIONS

=over 4

=item update <status>

Set your current status.

=item friends

Show most recent status of those who you marked as friends.

=item friends_timeline

Show the timeline of those who you marked as friends.

=item public_timeline

Show the public timeline.

=item followers

Show the timeline of those who follow your status.

=back

=head1 AUTHOR

Copyright (c) 2007, Kentaro Kuribayashi

About

This page contains a single entry from the blog posted on April 5, 2007 11:25 PM.

The previous post in this blog was I'm back to Movable Type!.

The next post in this blog is Google launched MyMaps.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34