user1301428
user1301428

Reputation: 1783

Implementing the Twitter streaming API in Perl, nothing gets printed

I am trying to use the Twitter streaming API to demo tweets in the console but nothing is being printed on screen at the moment. I am using the AnyEvent::Twitter::Stream package to do so, I have taken the OAuth example contained on that page and edited it slightly:

#!/usr/local/bin/perl -w

use AnyEvent::Twitter::Stream;

# to use OAuth authentication
my $listener = AnyEvent::Twitter::Stream->new(
  consumer_key    => "my_consumer_key",
  consumer_secret => "my_consumer_secret",
  token           => "my_token",
  token_secret    => "my_token_secret",
  method          => "sample",
  api_url         => "https://stream.twitter.com/1.1/statuses/sample.json",
  track           => "apple",
  on_tweet => sub {
            my $tweet = shift;
            print "$tweet->{user}{screen_name}: $tweet->{text}\n";
        },
);

I was expecting this to print to stdout all tweets mentioning "apple", but when I execute this code, the script exits immediately and I am back to the shell. I was kind of expecting this to keep running and keep printing messages to stdout. Have I misunderstood how this works?

Upvotes: 1

Views: 215

Answers (1)

jcaron
jcaron

Reputation: 17710

You may want to check out the provided example:

http://cpansearch.perl.org/src/MIYAGAWA/AnyEvent-Twitter-Stream-0.27/eg/track.pl

It involves creating an AE::cv object, and, after setting up the Twitter stream listener, calling recv on that object so that it actually loops waiting for data.

Upvotes: 1

Related Questions