Guest User

Untitled

a guest
Apr 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use Data::Dumper;
  6. use Devel::Peek;
  7. use feature qw( say );
  8. use Encode;
  9.  
  10.  
  11. use AnyEvent::Twitter::Stream;
  12. use Continuity;
  13. use Coro::Event;
  14.  
  15. my $user = 'nihen';
  16. my $password = '';
  17. my $done = AnyEvent->condvar;
  18.  
  19. my @tweets;
  20. my $new_tweet_id;
  21.  
  22. my $streamer = AnyEvent::Twitter::Stream->new(
  23. username => $user,
  24. password => $password,
  25. method => 'filter',
  26. track => 'http',
  27. on_tweet => sub {
  28. my $tweet = shift;
  29. if( $tweet->{text} =~ /[\p{InHiragana}\p{InKatakana}\p{InHalfwidthAndFullwidthForms}\p{InCjkUnifiedIdeographs}\p{InCjkUnifiedIdeographsExtensionA}\p{InCjkUnifiedIdeographsExtensionB}\p{InEnclosedCjkLettersAndMonths}]/ ) {
  30.  
  31. shift @tweets if( $#tweets > 20 );
  32. push( @tweets, encode( 'utf8', "$tweet->{user}{screen_name}: $tweet->{text}\n" ) );
  33. $new_tweet_id = $tweet->{id};
  34. }
  35. },
  36. on_error => sub {
  37. my $error = shift;
  38. warn "ERROR: $error";
  39. $done->send;
  40. },
  41. on_eof => sub {
  42. $done->send;
  43. },
  44. );
  45.  
  46. my $server = Continuity->new(
  47. port => 16001,
  48. path_session => 1,
  49. cookie_session => 'sid',
  50. staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html)$/ },
  51. );
  52. $server->loop;
  53. $done->recv;
  54.  
  55. sub main {
  56. my ($req) = @_;
  57. my $path = $req->request->url->path;
  58. print STDERR "Path: '$path'\n";
  59. pushstream($req) if $path =~ /pushstream/;
  60. }
  61.  
  62. sub pushstream {
  63. my ($req) = @_;
  64.  
  65. my $w = Coro::Event->var(var => \$new_tweet_id, poll => 'w');
  66. while (1) {
  67. my $log = join "<br />", @tweets;
  68. $req->print($log);
  69. $req->next;
  70.  
  71. $w->next;
  72. }
  73. }
Add Comment
Please, Sign In to add comment