Skip to content

Instantly share code, notes, and snippets.

@plu
Created November 23, 2010 16:41
Show Gist options
  • Save plu/712065 to your computer and use it in GitHub Desktop.
Save plu/712065 to your computer and use it in GitHub Desktop.
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 );
}
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment