在 Perl 中实现 Twitter 流 API,没有打印任何内容 [英] Implementing the Twitter streaming API in Perl, nothing gets printed
问题描述
我正在尝试使用 Twitter 流 API 在控制台中演示推文,但目前屏幕上没有打印任何内容.我正在使用 AnyEvent::Twitter::Stream 包来这样做,我已经采用了该页面上包含的 OAuth 示例并对其进行了稍微
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";
},
);
我原以为这会将所有提及apple"的推文打印到标准输出,但是当我执行此代码时,脚本立即退出并返回到 shell.我有点期待它继续运行并继续将消息打印到标准输出.我误解了这是如何工作的吗?
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?
推荐答案
您可能想查看提供的示例:
You may want to check out the provided example:
http://cpansearch.perl.org/src/MIYAGAWA/AnyEvent-Twitter-Stream-0.27/eg/track.pl
它涉及创建一个 AE::cv
对象,并在设置 Twitter 流侦听器后,对该对象调用 recv
以便它实际上循环等待数据.
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.
这篇关于在 Perl 中实现 Twitter 流 API,没有打印任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!