如何使用 Net::Twitter::Stream 从 API 读取流? [英] How to use Net::Twitter::Stream to read stream from API?

查看:29
本文介绍了如何使用 Net::Twitter::Stream 从 API 读取流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Net::Twitter::Stream 来自 CPAN 的 Perl 模块,用于从 sample.json 读取流.我相信这是 corect 模块,尽管他们制作的方式允许人们处理过滤器流.我已经这样修改了它,但我一定遗漏了一些东西,因为我没有得到任何数据作为回报.我建立了连接,但什么也没有回来.我猜这应该是一个简单的修复,但我对 Perl 的这一部分有点陌生......

I'm trying to use the Net::Twitter::Stream Perl module from CPAN to read the stream from sample.json. I believe this is the corect module though they way they crafted it allows one to process the filter stream. I've modified it as such but I must be missing something as I don't get any data in return. I establish a connection but nothing comes back. I'm guessing this should be an easy fix but I'm a touch new to this part of Perl.....

package Net::Twitter::Stream;
use strict;
use warnings;
use IO::Socket;
use MIME::Base64;
use JSON;
use IO::Socket::SSL;
use LibNewsStand qw(%cf);
use utf8;



our $VERSION = '0.27';
1;

=head1 NAME

Using Twitter streaming api. 

=head1 SYNOPSIS

use Net::Twitter::Stream;

Net::Twitter::Stream->new ( user => $username, pass => $password,
                          callback => \&got_tweet,
                          track => 'perl,tinychat,emacs',
                          follow => '27712481,14252288,972651' );

 sub got_tweet {
 my ( $tweet, $json ) = @_;   # a hash containing the tweet
                                  # and the original json
 print "By: $tweet->{user}{screen_name}\n";
 print "Message: $tweet->{text}\n";
 }

=head1 DESCRIPTION

The Streaming verson of the Twitter API allows near-realtime access to
various subsets of Twitter public statuses.

The /1/status/filter.json api call can be use to track up to 200 keywords
and to follow 200 users.

HTTP Basic authentication is supported (no OAuth yet) so you will need
a twitter account to connect.

JSON format is only supported. Twitter may depreciate XML.


More details at: http://dev.twitter.com/pages/streaming_api

Options 
  user, pass: required, twitter account user/password
  callback: required, a subroutine called on each received tweet


perl@redmond5.com
@martinredmond

=head1 UPDATES

https fix: iwan standley <iwan@slebog.net>

=cut


sub new {
  my $class = shift;
  my %args = @_;
  die "Usage: Net::Twitter::Stream->new ( user => 'user', pass => 'pass', callback => \&got_tweet_cb )" unless
    $args{user} && $args{pass} && $args{callback};
  my $self = bless {};
  $self->{user} = $args{user};
  $self->{pass} = $args{pass};
  $self->{got_tweet} = $args{callback};
  $self->{connection_closed} = $args{connection_closed_cb} if
    $args{connection_closed_cb};

  my $content = "follow=$args{follow}" if $args{follow};
  $content = "track=$args{track}" if $args{track};
  $content = "follow=$args{follow}&track=$args{track}\r\n" if $args{track} && $args{follow};

  my $auth = encode_base64 ( "$args{user}:$args{pass}" );
  chomp $auth;

  my $cl = length $content;
  my $req = <<EOF;
GET /1/statuses/sample.json HTTP/1.1\r
Authorization: Basic $auth\r
Host: stream.twitter.com\r
User-Agent: net-twitter-stream/0.1\r
Content-Type: application/x-www-form-urlencoded\r
Content-Length: $cl\r
\r
EOF

  my $sock = IO::Socket::INET->new ( PeerAddr => 'stream.twitter.com:https' );
  #$sock->print ( "$req$content" );
  while ( my $l = $sock->getline ) {
    last if $l =~ /^\s*$/;
  }
  while ( my $l = $sock->getline ) {
    next if $l =~ /^\s*$/;           # skip empty lines
    $l =~ s/[^a-fA-F0-9]//g;         # stop hex from compaining about \r
    my $jsonlen = hex ( $l );
    last if $jsonlen == 0;
    eval {
        my $json;
        my $len = $sock->read ( $json, $jsonlen );
        my $o = from_json ( $json );
        $self->{got_tweet} ( $o, $json );
    };
  }
  $self->{connection_closed} ( $sock ) if $self->{connection_closed};
}

推荐答案

你不需要发布源码,我们几乎可以弄清楚.您应该尝试其中一个示例,但我的建议是使用 AnyEvent::Twitter::Stream 带有一个很好的例子,你只需要稍微修改一下就可以让它运行

You don't need to post the source, we can pretty much figure it out. You should try one of the examples, but my advice is to use AnyEvent::Twitter::Stream which comes with a good example that you only have to modify a bit to get it running

这篇关于如何使用 Net::Twitter::Stream 从 API 读取流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆