Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Created August 11, 2009 11:52
Show Gist options
  • Save yusukebe/165769 to your computer and use it in GitHub Desktop.
Save yusukebe/165769 to your computer and use it in GitHub Desktop.
my $done = AnyEvent->condvar;
my $guard = AnyEvent::Twitter::Stream->new(
username => $user,
password => $password,
method => "track",
track => 'http',
on_tweet => sub {
my $tweet = shift;
my @links;
my $finder = URI::Find->new(
sub {
my $uri = shift;
push( @links, $uri->as_string );
}
);
$finder->find( \$tweet->{text} );
for my $link ( @links ){
my $permalink = filter( $link );
warn $permalink . "\n" if $permalink;
}
},
on_error => sub {
my $error = shift;
warn "ERROR: $error";
$done->send;
},
on_eof => sub {
$done->send;
},
);
$done->recv;
sub filter {
my $url = shift;
my $ua = LWP::UserAgent->new();
$ua->timeout( 5 );
my $res = $ua->head( $url );
return if $res->is_error();
my $permalink = $res->base;
if( $permalink =~ m!youtube.com!){
return $permalink;
}else{
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment