Guest User

Untitled

a guest
Apr 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. package MyCoro::IRC::Client;
  2. use Moose;
  3. use AnyEvent::IRC::Client;
  4. use Coro ();
  5. use Coro::AnyEvent ();
  6.  
  7. has ae_irc => (
  8. isa => 'AnyEvent::IRC::Client',
  9. is => 'ro',
  10. default => sub { AnyEvent::IRC::Client->new },
  11. );
  12.  
  13. has events => (
  14. isa => 'ArrayRef',
  15. is => 'ro',
  16. default => sub { [] },
  17. );
  18.  
  19. has cur_coro => (
  20. isa => 'Maybe[Coro]',
  21. is => 'rw',
  22. );
  23.  
  24. sub reg_watcher {
  25. my $self = shift;
  26. my ( $watcher ) = @_;
  27. $self->ae_irc->reg_cb(
  28. $watcher => sub {
  29. push @{ $self->events }, {event => $watcher, args => [ @_ ]};
  30. $self->cur_coro->ready if $self->cur_coro;
  31. }
  32. );
  33. }
  34.  
  35. sub next {
  36. my $self = shift;
  37. unless( @{ $self->events } ){
  38. $self->cur_coro( $Coro::current );
  39. Coro::schedule while ! @{ $self->events };
  40. }
  41. $self->cur_coro( undef );
  42.  
  43. return shift @{ $self->events };
  44. }
  45.  
  46. __PACKAGE__->meta->make_immutable;
  47. no Moose;
  48.  
  49.  
  50. package MyCoro::Twitter::Stream;
  51. use Moose;
  52. use AnyEvent::Twitter::Stream;
  53. use Coro ();
  54. use Coro::AnyEvent ();
  55.  
  56. has args => (
  57. isa => 'HashRef',
  58. is => 'ro',
  59. required => 1,
  60. );
  61.  
  62. has stream => (
  63. isa => 'AnyEvent::Twitter::Stream',
  64. is => 'rw',
  65. );
  66.  
  67. has tweets => (
  68. isa => 'ArrayRef',
  69. is => 'ro',
  70. default => sub { [] },
  71. );
  72.  
  73. has cur_coro => (
  74. isa => 'Maybe[Coro]',
  75. is => 'rw',
  76. );
  77.  
  78. sub BUILDARGS {
  79. return shift->SUPER::BUILDARGS( args => { @_ } );
  80. }
  81.  
  82. sub BUILD {
  83. my $self = shift;
  84. my $stream = AnyEvent::Twitter::Stream->new(
  85. %{ $self->args },
  86. on_tweet => sub {
  87. push @{ $self->tweets }, shift;
  88. $self->cur_coro->ready if $self->cur_coro;
  89. },
  90. on_error => sub { die @_; },
  91. on_eof => sub { exit; },
  92. );
  93. $self->stream( $stream );
  94. }
  95.  
  96.  
  97. sub next {
  98. my $self = shift;
  99. unless( @{ $self->tweets } ){
  100. $self->cur_coro( $Coro::current );
  101. Coro::schedule while ! @{ $self->tweets };
  102. }
  103. $self->cur_coro( undef );
  104.  
  105. return shift @{ $self->tweets };
  106. }
  107.  
  108.  
  109. __PACKAGE__->meta->make_immutable;
  110. no Moose;
  111.  
  112.  
  113. package main;
  114. use strict;
  115. use warnings;
  116. use Coro;
  117. use Data::Dumper;
  118. use Encode;
  119. use utf8;
  120.  
  121. my $channel = $ARGV[0];
  122. my $c = MyCoro::IRC::Client->new;
  123. $c->reg_watcher('connect');
  124. $c->reg_watcher('registered');
  125. $c->reg_watcher('publicmsg');
  126.  
  127. $c->ae_irc->connect(
  128. "irc.nara.wide.ad.jp", 6663, { nick => 'hiratara_bot' }
  129. );
  130. $c->ae_irc->send_srv( "JOIN", $channel );
  131.  
  132. async{
  133. while(my $ev = $c->next){
  134. if( $ev->{event} eq 'publicmsg'){
  135. $c->ae_irc->send_chan(
  136. $channel, "NOTICE", $channel,
  137. $ev->{args}[2]{params}[1]
  138. );
  139. }else{
  140. warn $ev->{event};
  141. };
  142. }
  143. };
  144.  
  145. my $twitter = MyCoro::Twitter::Stream->new(
  146. username => $ARGV[1],
  147. password => $ARGV[2],
  148. method => 'filter',
  149. track => '#food',
  150. );
  151.  
  152. async{
  153. while(my $tweet = $twitter->next){
  154. $c->ae_irc->send_chan(
  155. $channel, "NOTICE", $channel,
  156. encode_utf8( "$tweet->{user}{screen_name}: $tweet->{text}" )
  157. );
  158. }
  159. };
  160.  
  161.  
  162. schedule;
Add Comment
Please, Sign In to add comment