Skip to content

Instantly share code, notes, and snippets.

@robn
Created November 9, 2012 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robn/4042883 to your computer and use it in GitHub Desktop.
Save robn/4042883 to your computer and use it in GitHub Desktop.
Scrape Cricinfo live commentary stream, output to IRC
#!/usr/bin/env perl
use 5.016;
use warnings;
use strict;
use AnyEvent;
use AnyEvent::IRC::Client;
use AnyEvent::HTTP;
use HTML::TreeBuilder 5 -weak;
use Digest::SHA1 qw(sha1_hex);
if (@ARGV < 5) {
say "usage: cricinfobot <host> <port> <nick> <channel> <matchid>";
exit 1;
}
my ($HOST, $PORT, $NICK, $CHANNEL, $MATCHID) = @ARGV;
my $URL = "http://www.espncricinfo.com/netstorage/$MATCHID.html";
my $c = AnyEvent->condvar;
my $timer;
my $con = new AnyEvent::IRC::Client;
$con->reg_cb(
registered => sub {
$con->send_srv(JOIN => $CHANNEL);
},
disconnect => sub {
$c->broadcast;
},
);
$con->connect ($HOST, $PORT, { nick => $NICK });
my %seen;
my $check = AnyEvent->timer(interval => 15, cb => sub {
http_get($URL, sub {
my ($body, $hdr) = @_;
if ($hdr->{Status} !~ m/^2/) {
say STDERR "fetch failed: $hdr->{Status}";
return;
}
my $tree = HTML::TreeBuilder->new_from_content($body);
my %new_seen;
for my $elem (reverse $tree->look_down(class => "commsText")) {
my $text = $elem->as_text;
my $checksum = sha1_hex($text);
$new_seen{$checksum} = 1;
$con->send_srv(PRIVMSG => $CHANNEL, $text) if not $seen{$checksum};
}
%seen = %new_seen;
});
});
$c->wait;
$con->disconnect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment