Guest User

Untitled

a guest
May 1st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. package Twib::CLI::StreamReader;
  2. use Mouse;
  3. with 'Twib::API::Role';
  4. use AnyEvent::Twitter::Stream;
  5. use DateTime::Format::Strptime;
  6. use Encode;
  7. use URI::Find;
  8. use utf8;
  9.  
  10. has 'username' => ( is => 'rw', isa => 'Str', default => '' );
  11. has 'password' => ( is => 'rw', isa => 'Str', default => '' );
  12. has 'track' => ( is => 'rw', isa => 'Str', default => 'http,bit.ly' );
  13. no Mouse;
  14.  
  15. sub run {
  16. my $self = shift;
  17. my $done = AnyEvent->condvar;
  18. my $guard = AnyEvent::Twitter::Stream->new(
  19. username => $self->username,
  20. password => $self->password,
  21. method => "filter",
  22. track => $self->track,
  23. on_tweet => sub {
  24. my $tweet = shift;
  25. if ( $tweet->{text} && $tweet->{text} =~ /[あ-んア-ン]/ ) {
  26. my @links;
  27. my $finder = URI::Find->new(
  28. sub {
  29. my $uri = shift;
  30. push ( @links, $uri->as_string );
  31. }
  32. );
  33. my $text = $tweet->{text};
  34. $finder->find( \$text );
  35. $self->create_post( $tweet, \@links );
  36. }
  37. },
  38. on_error => sub {
  39. my $error = shift;
  40. warn "ERROR: $error";
  41. $done->send;
  42. },
  43. on_eof => sub {
  44. $done->send;
  45. },
  46. );
  47. $done->recv;
  48. }
  49.  
  50. #...
Add Comment
Please, Sign In to add comment