Bot-BasicBot-Pluggable-Module-TwitterFriendsStatus
view release on metacpan or search on metacpan
lib/Bot/BasicBot/Pluggable/Module/TwitterFriendsStatus.pm view on Meta::CPAN
#==============================================================================
#
# Bot::BasicBot::Pluggable::Module::TwitterFriendsStatus
#
# DESCRIPTION
#
# Adds ability for Bot::BasicBot::Pluggable IRC bots to check for comments
# posted to Twitter for a list of users and echo them into a channel.
#
# AUTHORS
# Gryphon Shafer <gryphon@cpan.org>
# David Precious <davidp@preshweb.co.uk>
#
# COPYRIGHT
# Copyright (C) 2008 by Gryphon Shafer
#
# This library is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#==============================================================================
package Bot::BasicBot::Pluggable::Module::TwitterFriendsStatus;
use strict;
use warnings;
use LWP::UserAgent;
use XML::Simple 'XMLin';
use Bot::BasicBot::Pluggable::Module;
use base 'Bot::BasicBot::Pluggable::Module';
our $VERSION = 0.2;
use constant {
CREDENTIALS => {
'netloc' => 'twitter.com:80',
'realm' => 'Twitter API',
},
URL => {
'verify_credentials' => 'http://twitter.com/account/verify_credentials.xml',
'friends_status' => 'http://twitter.com/statuses/friends_timeline.xml',
'friend_add' => [ 'http://twitter.com/friendships/create/', '.xml' ],
'friend_remove' => [ 'http://twitter.com/friendships/destroy/', '.xml' ],
},
TICK_INTERVAL => 60,
};
my $tick_counter = TICK_INTERVAL;
my $ua = LWP::UserAgent->new(
'agent' => 'Bot::BasicBot::Pluggable::Module::TwitterFriendsStatus/0.1 ',
'max_redirect' => 7,
'parse_head' => 1,
);
sub help {
return join( ', ',
'twitter add <twittername>',
'twitter remove <twittername>',
'!twitterauth <username> <password>',
);
}
sub init {
my ($self) = @_;
my ( $username, $password ) = ( $self->get('username'), $self->get('password') );
if ( $username and $password ) {
print "Twitter authentication previously successful. Using auth for $username\n";
$ua->credentials(
CREDENTIALS->{'netloc'},
CREDENTIALS->{'realm'},
$username,
$password,
);
}
_twitter_friends_status( $self, 0 );
}
sub said {
my ( $self, $message, $priority ) = @_;
return unless ( $priority == 2 );
if (my($user,$pass) = $message->{'body'} =~ /^\s*!\s*twitter\s*auth\s*(\S+)\s+(\S+)/ ) {
$ua->credentials( CREDENTIALS->{'netloc'}, CREDENTIALS->{'realm'}, $user, $pass );
my $response = $ua->get( URL->{'verify_credentials'} );
my $credentials = XMLin($response->content());
use Data::Dumper;
warn Dumper($credentials);
if ( $credentials->{name} eq $user) {
$self->reply(
$message,
"You have properly authenticated to Twitter as $user\n" .
"This authentication will be saved in the bot memory storage.\n" .
"$user has $credentials->{followers_count} followers and" .
"follows $credentials->{friends_count} users.\n"
);
$self->set( 'username' => $1 );
$self->set( 'password' => $2 );
}
else {
$self->reply(
$message,
"You have failed to properly authenticate with Twitter as $user",
);
$self->set( 'username' => '' );
$self->set( 'password' => '' );
}
}
elsif ( $message->{'body'} =~ /^\s*twitter\s*add\s*(\S+)/ ) {
my $response = $ua->post( URL->{'friend_add'}[0] . $1 . URL->{'friend_add'}[1] );
$self->reply(
$message,
"Added $1 to my Twitter following list.",
);
}
elsif ( $message->{'body'} =~ /^\s*twitter\s*remove\s*(\S+)/ ) {
my $response = $ua->post( URL->{'friend_remove'}[0] . $1 . URL->{'friend_remove'}[1] );
$self->reply(
$message,
"Removed $1 from my Twitter following list.",
);
}
return;
}
sub tick {
my ($self) = @_;
$tick_counter++;
return if ( $tick_counter < TICK_INTERVAL );
$tick_counter = 0;
_twitter_friends_status( $self, 1 );
foreach ( $self->store_keys() ) {
$self->unset($_) if ( $_ =~ /^\d+$/ and $self->get($_) + 60 * 60 * 50 < time );
}
}
sub _twitter_friends_status {
my ( $self, $do_tell ) = @_;
my $response = $ua->get( URL->{'friends_status'} );
my $friends_status = XMLin(
$response->content(),
'ForceArray' => 1,
'KeyAttr' => [],
);
foreach my $status (
grep { $_->{'user'}[0]{'name'}[0] ne 'woprircbot' }
@{ $friends_status->{'status'} }
( run in 0.835 second using v1.01-cache-2.11-cpan-70e19b8f4f1 )