Skip to content

Instantly share code, notes, and snippets.

@afresh1
Created February 11, 2015 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afresh1/858af53ba1ac975ae6cb to your computer and use it in GitHub Desktop.
Save afresh1/858af53ba1ac975ae6cb to your computer and use it in GitHub Desktop.
An ICB bot that watches for twitter URLs and fills out the text.
#!/usr/bin/perl
use strict;
use warnings;
# On OpenBSD, I have a port for p5-Bot-ICB and p5-Net-ICB
# pkg_add p5-Bot-ICB p5-LWP-Protocol-https p5-HTML-Tree p5-Text-Unidecode
use Bot::ICB;
use LWP::Simple;
use HTML::Tree;
use Text::Unidecode;
my $user = 'icbbot';
$|++;
sub get_tweet {
my ($uri) = @_;
my $content = get($uri) || die "Unable to get $uri";
my $tree = HTML::TreeBuilder->new();
$tree->parse($content);
$tree->eof;
my $element = $tree->look_down(
_tag => 'div',
class => qr/\bjs-original-tweet\b/,
);
my $get = sub {
my $class = shift;
scalar $element->look_down( class => qr/\b\Q$class\E\b/ );
};
#print $element->dump;
my %tweet = (
fullname => $get->('fullname'),
username => $get->('username'),
timestamp => $get->('tweet-timestamp'),
text => $get->('tweet-text'),
);
$tweet{$_} = unidecode( $tweet{$_}->as_trimmed_text ) for keys %tweet;
$tree->delete;
return "$tweet{fullname} ($tweet{username}): $tweet{text}";
}
#my $on_connect = sub {
# my ($bot) = @_;
# #$bot->sendopen("hai!");
#};
my $on_public = sub {
my ( $bot, $nick, @msg ) = @_;
print STDERR "<$nick> @msg\n";
my $msg = "@msg";
if ( $msg =~ m{\b(https?://twitter.com/\w+/status/\d+)\b} ) {
my $tweet = get_tweet($1);
$bot->sendopen($tweet);
print STDERR "-> $tweet\n";
}
};
#my $on_msg = sub {
# my ( $bot, $nick, @msg ) = @_;
# ( $bot->sendopen("later") && $bot->disconnect ) if $nick eq 'hoople';
# print STDERR "<*$nick*> @msg\n";
#};
my $on_status = sub {
my ( $bot, $info, @msg ) = @_;
print STDERR "[info] $info: @msg\n";
};
my $bot = Bot::ICB->newconn;
$bot->debug(1);
#$bot->add_handler( 'connect', $on_connect );
$bot->add_handler( 'name', $on_status );
$bot->add_handler( 'sign-off', $on_status );
$bot->add_handler( 'arrive', $on_status );
$bot->add_handler( 'depart', $on_status );
$bot->add_handler( 'topic', $on_status );
$bot->add_handler( 'boot', $on_status );
$bot->add_handler( 'public', $on_public );
#$bot->add_handler( 'msg', $on_msg );
$bot->add_handler( 'status', $on_status );
$bot->login( user => $user, group => '$!', host => 'localhost' );
$bot->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment