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

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

问题描述

我试图使用的Net ::微博::流从CPAN Perl模块读取sample.json流。我相信这是corect模块虽然他们的方式,他们制作它允许一个处理过滤器流。我已经修改了它本身,而是我必须失去了一些东西,因为我不得到的回报的任何数据。我建立连接,但没有回来。我猜这应该是一个简单的办法,但我是一个触摸新的Perl的这部分.....

 包装网::微博::流;
使用严格的;
使用警告;
使用IO ::插座;
使用MIME :: Base64编码;
使用JSON;
使用IO ::插座:: SSL;
使用LibNewsStand QW(%CF);
使用UTF8;我们的$ VERSION ='0.27';
1;=头1名使用Twitter流API。=头1概要使用网::微博::流;网::微博:: - 于流gt;新建(用户=> $用户名,通过= GT; $的密码,
                          回调=> \\&安培; got_tweet,
                          轨道= GT; perl的,tinychat,Emacs的,
                          遵循=> '27712481,14252288,972651'); 子got_tweet {
 我($鸣叫,$ JSON)= @_; #包含鸣叫哈希
                                  #和原JSON
 打印:tweet- $> {用户} {} SCREEN_NAME \\ n;
 打印消息:$ tweet->的{text} \\ n;
 }=头1描述Twitter的API的流VERSON允许接近实时的访问
Twitter的公共状态的各种子集。该/1/status/filter.json API调用,可以用它来跟踪多达200个关键字
并按照200个用户。HTTP基本身份验证支持(无尚的OAuth),因此你需要
一个Twitter帐户连接。JSON格式仅支持。 Twitter可能去preciate XML。
http://dev.twitter.com/pages/streaming_api:在更多细节选项
  user和pass:需要,Twitter帐号用户名/密码
  回调:需要,要求每个接收鸣叫子程序
perl@redmond5.com
@martinredmond=头1 UPDATESHTTPS FIX:伊万斯坦德利< iwan@slebog.net>=切
亚新{
  我的$类=转变;
  我ARGS%= @_;
  死用法:净::微博:: - 于流gt;新建(用户=>'用户',通过= GT;'通',回调=> \\&安培; got_tweet_cb),除非
    的$ args {}用户和放大器;&安培;的$ args {}通放大器和;&安培;的$ args {}回调;
  我自$ =祝福{};
  $自> {}用户= $ {ARGS用户};
  $自> {}通=的$ args {}传递;
  $自> {} got_tweet = $ {ARGS回调};
  $自> {}连接关闭= $ {ARGS} connection_closed_cb如果
    的$ args {connection_closed_cb};  我的$内容=跟着=的$ args {}如下:如果的$ args {}遵循;
  $内容=轨道=的$ args {}轨道如果的$ args {}轨道;
  $内容=跟着=的$ args {}跟踪和放大器;跟踪=的$ args {}轨道\\ r \\ n如果的$ args {}跟踪功放&;&安培;的$ args {}如下;  我的$ AUTH = EN code_base64(的$ args {}用户:的$ args {}传);
  的Chomp $权威性;  我的$ CL = $长度的内容;
  我的$ REQ =<< EOF;
GET /1/statuses/sample.json HTTP / 1.1 \\ r
授权:基本身份验证$ \\ r
主持人:stream.twitter.com \\ r
用户代理:净叽叽喳喳流/ 0.1 \\ r
内容类型:应用程序/ x-WWW的形式urlen codeD \\ r
内容长度:$ CL \\ r
\\ r
EOF  我的袜子$ = IO ::插座:: INET->新建(PeerAddr =>'stream.twitter.com:https~~V');
  #$ sock->打印($ $ REQ内容);
  而(我的$ L = $ sock->函数getline){
    最后,如​​果$ L =〜/ ^ \\ s * $ /;
  }
  而(我的$ L = $ sock->函数getline){
    接下来,如果$ L =〜/ ^ \\ s * $ /; #跳过空行
    $ L =〜S / [^ A-FA-F0-9] //克; #停止六角从compaining约\\ r
    我的$ jsonlen =十六进制($ L);
    最后,如​​果$ jsonlen == 0;
    EVAL {
        我的$ json的;
        我的$ LEN = $ sock->阅读($ JSON,$ jsonlen);
        我的$ O = from_json($ JSON);
        $自> {} got_tweet($ 0,$ JSON);
    };
  }
  $自> {}连接关闭($袜子)如果$自> {}连接关闭;
}


解决方案

您不必公布源,我们可以pretty多少看着办吧。你应该尝试的例子之一,但我的建议是使用 AnyEvent ::微博::流的,它有一个很好的例子,你只需要修改了一下,让它运行

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};
}

解决方案

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

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

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