Guest User

Untitled

a guest
Apr 9th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use AnyEvent::IRC::Client;
  7. use AnyEvent::Twitter::Stream;
  8. use Net::Twitter::Lite;
  9. use Getopt::Long;
  10. use Encode;
  11.  
  12. my $usage = <<EOF;
  13. anyevent-irc-bot.pl
  14. options:
  15. --username user name
  16. --password password
  17. --channel join channel
  18. EOF
  19.  
  20. GetOptions(
  21. "username=s" => \my $user,
  22. "password=s" => \my $password,
  23. "channel=s" => \my $channel,
  24. );
  25.  
  26. die $usage unless $user;
  27. die $usage unless $password;
  28. die $usage unless $channel;
  29.  
  30. my $owner_id = '11902602';
  31.  
  32. my $nt = Net::Twitter::Lite->new(
  33. username => $user,
  34. password => $password
  35. );
  36. my $cv = AnyEvent->condvar;
  37. my $pc = AnyEvent::IRC::Client->new;
  38. $pc->reg_cb(
  39. connect => sub {
  40. my ( $pc, $err ) = @_;
  41. if ( defined $err ) {
  42. print "Couldn't connect to server: $err\n";
  43. }
  44. },
  45. registered => sub {
  46. my ($self) = @_;
  47. print "registered!\n";
  48. $pc->enable_ping(60);
  49. },
  50. disconnect => sub {
  51. print "disconnected: $_[1]!\n";
  52. }
  53. );
  54. $pc->send_srv( "JOIN", $channel );
  55. $pc->send_chan( $channel, "NOTICE", $channel, "hi i import twitter" );
  56. $pc->connect( "irc.freenode.net", 6667,
  57. { nick => 'tw', user => 'tw', real => 'tw' } );
  58.  
  59. my $following_ids = $nt->followers_ids;
  60. push @{ $following_ids }, $owner_id;
  61. my $streamer = AnyEvent::Twitter::Stream->new(
  62. username => $user,
  63. password => $password,
  64. method => 'filter',
  65. follow => join(",", @{ $following_ids }),
  66. on_tweet => sub {
  67. my $tweet = shift;
  68. $pc->send_chan( $channel, "PRIVMSG", $channel,
  69. encode( 'utf8', "$tweet->{user}{screen_name}: $tweet->{text}" ) );
  70. },
  71. on_error => sub {
  72. my $error = shift;
  73. warn "ERROR: $error";
  74. $cv->send;
  75. },
  76. on_eof => sub {
  77. $cv->send;
  78. },
  79. );
  80.  
  81. $cv->recv;
Add Comment
Please, Sign In to add comment