Как использовать Net :: Twitter :: Stream для чтения потока из API? - PullRequest
0 голосов
/ 02 ноября 2011

Я пытаюсь использовать модуль Perl Net :: Twitter :: Stream из CPAN для чтения потока из sample.json. Я полагаю, что это модуль corect, хотя они, как они разработали, позволяют обрабатывать поток фильтра. Я изменил это как таковое, но я должен что-то упустить, так как не получаю никаких данных взамен. Я устанавливаю связь, но ничего не возвращается. Я думаю, это должно быть легко исправить, но я немного новичок в этой части 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};
}

Ответы [ 2 ]

1 голос
/ 17 апреля 2012
sub parse_from_twitter_stream {
  my $user = 'XXX';
  my $password = 'YYYY';

  my $stream = Net::Twitter::Stream->new ( user => $user, pass => $password,
                             callback => \&got_tweet,
                             connection_closed_cb => \&connection_closed,
                             track => SEARCH_TERM);

  sub connection_closed {
    sleep 1;
    warn "Connection to Twitter closed";
    parse_from_twitter_stream();#This isn't working for me -- can't get connection to reopen after disconnect
  }

  sub got_tweet {
    my ( $tweet, $json ) = @_;   # a hash containing the tweet
    #Do stuff here

    }
}
1 голос
/ 02 ноября 2011

Вам не нужно публиковать источник, мы можем в значительной степени понять это. Вы должны попробовать один из примеров, но я советую использовать AnyEvent :: Twitter :: Stream , который является хорошим примером того, что вам нужно всего лишь немного изменить, чтобы запустить

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...