irssi: Twitter

| | Comments (0) | TrackBacks (0)
  • install all dependencies (POE, POE::Loop::Glib, POE::Session::Irssi, Net::Twitter, HTML::Entities, HTTP::Date)
  • change credentials
  • /script load /path/to/twitter.pl
  • /window new hidden
  • change to the new window and type /window name twitter
use strict;
use warnings;
use Irssi;
use Glib;
use POE qw(Loop::Glib Session::Irssi);
use Net::Twitter;
use HTML::Entities qw( decode_entities );
use HTTP::Date qw( time2str );

my $VERSION = '0.1';
my %IRSSI   = (
    authors => 'Johannes Plunien',
    contact => 'http://www.pqpq.de/contact/',
    name    => 'Twitter',
    license => 'Perl',
);  

my %twitter_config = (
    username        => 'plutooth',
    password        => 'secret',
    update_interval => 90,              # seconds
);

POE::Session::Irssi->create(
    irssi_commands => {
        tmsg => sub {
            $_[HEAP]->{twitter}->update(join " ", (@{ $_[ARG1] })[0]);
            $_[KERNEL]->delay( update => 5 );
        },   
    },
    inline_states => {
        _start => sub {
            my $heap = $_[HEAP];
            $heap->{window} = Irssi::window_find_name('twitter');
            Irssi::print("Create a window named 'twitter'") if !$heap->{window};
            my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
            $heap->{interval} = delete $twitter_config{update_interval};
            $heap->{twitter}  = Net::Twitter->new(%twitter_config)
              or Irssi::print("Twitter ERROR: $!");
            $kernel->delay( update => 2 );
        },   
        update => sub {
            my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
            my $params = {};
            $params->{since} = $heap->{last_update} if defined $heap->{last_update};
            my $timeline    = $heap->{twitter}->friends_timeline($params);
            my $http_status = $heap->{twitter}->http_code;
            $heap->{window} = Irssi::window_find_name('twitter');
            if ( $http_status == 200 && $heap->{window} ) {
                $heap->{last_update} = time2str(time);
                $kernel->yield( display => $timeline ) if scalar @$timeline > 0;
            }
            $kernel->delay( update => $heap->{interval} );
        },   
        display => sub {
            my ( $kernel, $heap, $timeline ) = @_[ KERNEL, HEAP, ARG0 ];
            foreach my $row ( reverse @$timeline ) {
                my $screen_name = $row->{user}{screen_name};
                my $text = $row->{text};
                $text =~ s/\n/\n   /;
                my $cleantext = decode_entities($text);
                my $message = "<$screen_name> $cleantext";
                $heap->{window}->print($message, MSGLEVEL_PUBLIC);
            }
        },
    },
);

0 TrackBacks

Listed below are links to blogs that reference this entry: irssi: Twitter.

TrackBack URL for this entry: http://www.pqpq.de/mt-tb.cgi/29

Leave a comment

About this Entry

This page contains a single entry by Johannes Plunien published on February 1, 2009 5:44 PM.

irssi: Close all queries was the previous entry in this blog.

Catalyst + DBIC + DAO is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.