如何使用 AnyEvent::Twitter::Stream 在 perl 中打印 JSON 对象
我正在使用 AnyEvent::Twitter::Stream 模块来获取推文。最终,我尝试将推文打印到文件中,但我无法(我认为)将推文作为 JSON 对象获取。我的代码如下:
#!/Applications/XAMPP/xamppfiles/bin/perl
use AnyEvent::Twitter::Stream;
my $done = AnyEvent->condvar;
BEGIN {
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw{
&init
};
}
sub print_tweet {
my $tweet = shift;
print $tweet;
}
# receive updates from @following_ids
my $listener = AnyEvent::Twitter::Stream->new(
username => XXXXXX
password => XXXXXX
method => 'sample', # "firehose" for everything, "sample" for sample timeline
decode_json => 1,
on_tweet => sub {
my $tweet = shift;
print_tweet($tweet);
},
on_keepalive => sub {
warn "ping\n";
},
on_delete => sub {
my ($tweet_id, $user_id) = @_; # callback executed when twitter send a delete notification
},
timeout => 45,
);
$done->recv;
然而,当我在 print_tweet 子例程中打印出推文时,我得到的只是:
HASH(0x8f0ad0)HASH(0x8f0640)HASH(0x875990)HASH(0x8f0ab0)HASH(0x8e0d80)HASH(0x8f06e0)HASH(0x8f08f0)HASH(0x93ef30)HASH(0x876190)HASH(0x93ee60)HASH(0x8f0610)HASH(0x8f0b00)HASH(0x8e13e0)HASH(0x93ee20)HASH(0x8f0a20)HASH(0x8e1970)HASH(0x8f0900)
我什至尝试打印出推文,假设它是一个散列,如下所示:
sub print_tweet {
my ($jsonref, $tweet) = @_;
my $tweet = shift;
print %tweet;
}
但这没有产生任何结果。看起来 AnyEvent::Twitter::Stream 根据以下示例代码将 $tweet 作为对象返回:
on_tweet => sub {
my $tweet = shift;
warn "$tweet->{user}{screen_name}: $tweet->{text}\n";
},
我知道我可以打印出单个对象,但是我可以获得原始 JSON 对象吗?我一定错过了一些东西,或者我的“菜鸟”性比我想象的要大...
更新
I was able to ALMOST get it by changing print_tweet to the following:
sub print_tweet {
my $tweet = shift;
my $json_output = to_json($tweet);
print $json_output;
}
它打印出大部分 JSON 对象,但抱怨宽字符,我认为这是输出的问题是utf8格式吗?我不确定如何解决这个问题......
I'm using the AnyEvent::Twitter::Stream module to grab tweets. Ultimately I'm trying to print the tweets to a file but I'm unable (I think) to get the tweet as a JSON object. My code is as follows:
#!/Applications/XAMPP/xamppfiles/bin/perl
use AnyEvent::Twitter::Stream;
my $done = AnyEvent->condvar;
BEGIN {
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw{
&init
};
}
sub print_tweet {
my $tweet = shift;
print $tweet;
}
# receive updates from @following_ids
my $listener = AnyEvent::Twitter::Stream->new(
username => XXXXXX
password => XXXXXX
method => 'sample', # "firehose" for everything, "sample" for sample timeline
decode_json => 1,
on_tweet => sub {
my $tweet = shift;
print_tweet($tweet);
},
on_keepalive => sub {
warn "ping\n";
},
on_delete => sub {
my ($tweet_id, $user_id) = @_; # callback executed when twitter send a delete notification
},
timeout => 45,
);
$done->recv;
Yet when I print out the tweet in the print_tweet subroutine all I get is:
HASH(0x8f0ad0)HASH(0x8f0640)HASH(0x875990)HASH(0x8f0ab0)HASH(0x8e0d80)HASH(0x8f06e0)HASH(0x8f08f0)HASH(0x93ef30)HASH(0x876190)HASH(0x93ee60)HASH(0x8f0610)HASH(0x8f0b00)HASH(0x8e13e0)HASH(0x93ee20)HASH(0x8f0a20)HASH(0x8e1970)HASH(0x8f0900)
I've even tried to print out the tweet assuming it is a hash as follows:
sub print_tweet {
my ($jsonref, $tweet) = @_;
my $tweet = shift;
print %tweet;
}
Yet that produced nothing. It appears that AnyEvent::Twitter::Stream is returning $tweet as an object based on their sample code of:
on_tweet => sub {
my $tweet = shift;
warn "$tweet->{user}{screen_name}: $tweet->{text}\n";
},
And I know I can print out individual objects, but can I get teh raw JSON object? I must be missing something or my 'noob'ness is greater than I thought...
UPDATE
I was able to ALMOST get it by changing print_tweet to the following:
sub print_tweet {
my $tweet = shift;
my $json_output = to_json($tweet);
print $json_output;
}
It prints out MOST of the JSON object but complains about wide characters, which I believe is an issue with the output being utf8 format? I'm unsure how to solve this issue though....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来它正在返回一个
hashref
。如果你不确定,你可以尝试做这样的事情。这应该让你了解正在传递的内容,然后你可以抓住你想要的东西 - 可能是这样的:
Looks like it's returning a
hashref
. If you're not sure, you could try doing something like this.That should give you an idea of what's being passed, then you can grab what you want - probably something like this:
在
print_tweet
中,您声明了$tweet
两次。首先,将@_
数组的第二个元素分配给它,然后重新声明它并为其分配@_
的第一个元素,因为shift
默认在@_
上运行。当然,如果您打开了
use warnings
,您就会看到这就是为什么您应该始终
use strict;在代码顶部使用警告;
。您看到的输出字符串是哈希引用,是打印
print_tweet
第一个参数中的内容(您最初分配给$json_ref
的内容)的结果。如果您想打印$tweet
的值,请删除使用shift
破坏它的行。In
print_tweet
, you're declaring$tweet
twice. First, you assign it the second element of the@_
array, then you redeclare it and assign it the first element of@_
, becauseshift
operated on@_
by default.Of course, if you had
use warnings
turned on, you would have seenThat's why you should always
use strict; use warnings;
at the top of your code.The strings of output that you're seeing are hash references, the result of printing what's in the first argument to
print_tweet
(what you initially assign to$json_ref
). If you want to print out the value of$tweet
, get rid of the line where you clobber it withshift
.想通了。需要使用JSON模块并进行编码。编码时必须使用
{utf8 => 1}
选项来解释您从 Twitter 获取的 utf8 字符。最终代码在这里:感谢你们提供的帮助,DataDumper至少让我验证了格式,它只是没有产生最终结果。
Figured it out. Need to use the JSON module and encode. When encoding you MUST use the
{utf8 => 1}
option to account for the utf8 characters you get form Twitter. Final code is here:Thanks to the help you guys gave, the DataDumper at least let me verify the format, it just didn't produce the final result.