| 1 | package App::Twitch; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $App::Twitch::AUTHORITY = 'cpan:GETTY'; | |
| 4 | } | ||
| 5 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | $App::Twitch::VERSION = '0.904'; | |
| 7 | } | ||
| 8 | # ABSTRACT: Your personal Twitter b...... lalalala | ||
| 9 | |||
| 10 | # Speed up complete POE Kernel | ||
| 11 | sub POE::Kernel::USE_SIGCHLD () { 1 } | ||
| 12 | 1 | ▶ | use MooseX::POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | with qw( | ||
| 15 | MooseX::Getopt | ||
| 16 | MooseX::SimpleConfig | ||
| 17 | MooseX::LogDispatch | ||
| 18 | MooseX::Daemonize | ||
| 19 | ); | ||
| 20 | |||
| 21 | 1 | ▶ | use POE qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | Component::Client::HTTP | ||
| 23 | Component::Client::Keepalive | ||
| 24 | Component::FeedAggregator | ||
| 25 | Component::WWW::Shorten | ||
| 26 | 1 | ); | |
| 27 | |||
| 28 | use HTTP::Request; | ||
| 29 | use Text::Trim; | ||
| 30 | use URI; | ||
| 31 | use POSIX; | ||
| 32 | use IO::All; | ||
| 33 | use String::Truncate qw(elide); | ||
| 34 | use utf8; | ||
| 35 | use Text::Keywords; | ||
| 36 | use Text::Keywords::Container; | ||
| 37 | use Text::Keywords::List; | ||
| 38 | use Text::Tweet; | ||
| 39 | use HTML::ExtractContent; | ||
| 40 | use Carp qw( croak ); | ||
| 41 | use YAML qw( DumpFile ); | ||
| 42 | |||
| 43 | # could be ... ah forget it :-P | ||
| 44 | use Net::Twitter; | ||
| 45 | |||
| 46 | our $VERSION ||= '0.0development'; | ||
| 47 | |||
| 48 | before run => sub { | ||
| 49 | my $package = __PACKAGE__; | ||
| 50 | print <<"END_OF_INTRO"; | ||
| 51 | _ _ _ _ | ||
| 52 | | |_ _ _(_) |_ ___| |___ | ||
| 53 | | _\\ \\ /\\ / / | __/ __| '_ \\ | ||
| 54 | | |_ \\ V V /| | || (__| | | | $package $VERSION | ||
| 55 | \\__| \\_/\\_/ |_|\\__\\___|_| |_| Usage with --help | ||
| 56 | |||
| 57 | More information under http://search.cpan.org/perldoc/twitch | ||
| 58 | |||
| 59 | Commands: | ||
| 60 | |||
| 61 | start Start the twitch | ||
| 62 | stop Stop the running twitch | ||
| 63 | restart Restart the twitch, or start it up if not running | ||
| 64 | status Give current status of the twitch | ||
| 65 | |||
| 66 | END_OF_INTRO | ||
| 67 | }; | ||
| 68 | |||
| 69 | after start => sub { | ||
| 70 | my $self = shift; | ||
| 71 | return unless $self->is_daemon; | ||
| 72 | # Required, elsewhere your POE goes nuts | ||
| 73 | POE::Kernel->has_forked if !$self->foreground; | ||
| 74 | POE::Kernel->run; | ||
| 75 | }; | ||
| 76 | |||
| 77 | has '+pidbase' => ( | ||
| 78 | default => sub { shift->tmpdir }, | ||
| 79 | documentation => 'Directory for the pid file (default: tmpdir)', | ||
| 80 | ); | ||
| 81 | |||
| 82 | has '+pidfile' => ( | ||
| 83 | documentation => 'Filename for the pidfile (default: basedir/progname.pid)', | ||
| 84 | ); | ||
| 85 | |||
| 86 | has '+progname' => ( | ||
| 87 | default => sub { 'twitch' }, | ||
| 88 | documentation => 'Name for the application, like configfile name base and so on (default: twitch)', | ||
| 89 | ); | ||
| 90 | |||
| 91 | has '+foreground' => ( | ||
| 92 | documentation => 'Run on the console and don\'t detach into background (default: off)', | ||
| 93 | ); | ||
| 94 | |||
| 95 | has '+configfile' => ( | ||
| 96 | default => sub { 'twitch.yml' }, | ||
| 97 | documentation => 'Configuration file used for all those settings (default: twitch.yml)', | ||
| 98 | ); | ||
| 99 | |||
| 100 | has 'configdir' => ( | ||
| 101 | is => 'ro', | ||
| 102 | isa => 'Str', | ||
| 103 | default => sub { getcwd }, | ||
| 104 | documentation => 'Directory for the keyword files and the feed file if given (default: current directory)', | ||
| 105 | ); | ||
| 106 | |||
| 107 | has log_dispatch_conf => ( | ||
| 108 | is => 'ro', | ||
| 109 | isa => 'HashRef', | ||
| 110 | lazy => 1, | ||
| 111 | required => 1, | ||
| 112 | traits => [ 'NoGetopt' ], | ||
| 113 | default => sub { | ||
| 114 | my $self = shift; | ||
| 115 | return {} if $self->no_logging; | ||
| 116 | my $format = '[%d] [%p] %m'; | ||
| 117 | my $minlevel = $self->debug ? 'debug' : 'info'; | ||
| 118 | if ($self->foreground || !$self->logfile) { | ||
| 119 | return { | ||
| 120 | class => 'Log::Dispatch::Screen', | ||
| 121 | min_level => $minlevel, | ||
| 122 | stderr => 1, | ||
| 123 | format => $format, | ||
| 124 | newline => 1, | ||
| 125 | } | ||
| 126 | } else { | ||
| 127 | return { | ||
| 128 | class => 'Log::Dispatch::File', | ||
| 129 | min_level => $minlevel, | ||
| 130 | filename => $self->configdir.'/'.$self->logfile, | ||
| 131 | mode => '>>', | ||
| 132 | format => $format, | ||
| 133 | newline => 1, | ||
| 134 | } | ||
| 135 | } | ||
| 136 | }, | ||
| 137 | ); | ||
| 138 | |||
| 139 | has consumer_key => ( | ||
| 140 | isa => 'Str', | ||
| 141 | is => 'ro', | ||
| 142 | required => 1, | ||
| 143 | documentation => 'Consumer Key of your Twitter application', | ||
| 144 | ); | ||
| 145 | |||
| 146 | has consumer_secret => ( | ||
| 147 | isa => 'Str', | ||
| 148 | is => 'ro', | ||
| 149 | required => 1, | ||
| 150 | documentation => 'Consumer Secret of your Twitter application', | ||
| 151 | ); | ||
| 152 | |||
| 153 | has access_token => ( | ||
| 154 | isa => 'Str', | ||
| 155 | is => 'ro', | ||
| 156 | required => 1, | ||
| 157 | documentation => 'Access Token of the Twitter user for the application', | ||
| 158 | ); | ||
| 159 | |||
| 160 | has access_token_secret => ( | ||
| 161 | isa => 'Str', | ||
| 162 | is => 'ro', | ||
| 163 | required => 1, | ||
| 164 | documentation => 'Access Token Secret of the Twitter user for the application', | ||
| 165 | ); | ||
| 166 | |||
| 167 | has feeds => ( | ||
| 168 | traits => [ 'Array' ], | ||
| 169 | is => 'ro', | ||
| 170 | isa => 'ArrayRef[Str]', | ||
| 171 | default => sub { | ||
| 172 | my $self = shift; | ||
| 173 | my @lines; | ||
| 174 | if ($self->feeds_file) { | ||
| 175 | @lines = grep { | ||
| 176 | $_ = trim($_); | ||
| 177 | /^http:\/\// | ||
| 178 | } io($self->configdir.'/'.$self->feeds_file)->slurp; | ||
| 179 | } | ||
| 180 | return \@lines; | ||
| 181 | }, | ||
| 182 | #documentation => 'Feeds (must be given via config file as array [TODO])', | ||
| 183 | handles => { | ||
| 184 | feeds_shift => 'shift', | ||
| 185 | feeds_count => 'count', | ||
| 186 | }, | ||
| 187 | ); | ||
| 188 | |||
| 189 | has feeds_file => ( | ||
| 190 | isa => 'Str', | ||
| 191 | is => 'ro', | ||
| 192 | default => sub { 'feeds.txt' }, | ||
| 193 | documentation => 'File with the list of Feeds (one per line, default: feeds.txt)', | ||
| 194 | ); | ||
| 195 | |||
| 196 | has feed_delay => ( | ||
| 197 | isa => 'Int', | ||
| 198 | is => 'ro', | ||
| 199 | required => 1, | ||
| 200 | default => sub { 600 }, | ||
| 201 | documentation => 'How often every feed should be checked in seconds (default: 600)', | ||
| 202 | ); | ||
| 203 | |||
| 204 | has hashtags_at_end => ( | ||
| 205 | is => 'ro', | ||
| 206 | isa => 'Bool', | ||
| 207 | required => 1, | ||
| 208 | default => sub { 0 }, | ||
| 209 | documentation => 'Put all hashtag keywords after the URL (default: 0)', | ||
| 210 | ); | ||
| 211 | |||
| 212 | has dryrun => ( | ||
| 213 | is => 'ro', | ||
| 214 | isa => 'Bool', | ||
| 215 | required => 1, | ||
| 216 | default => sub { 0 }, | ||
| 217 | documentation => 'Do not actually generate tweets, but do all other steps (default: 0)', | ||
| 218 | ); | ||
| 219 | |||
| 220 | has dryrun_url => ( | ||
| 221 | is => 'ro', | ||
| 222 | isa => 'Str', | ||
| 223 | required => 1, | ||
| 224 | default => sub { 'http://xrl.us/DrYRuN' }, | ||
| 225 | documentation => 'ShortenURL used for the dryrun debugging informations (default: http://xrl.us/DrYRuN)', | ||
| 226 | ); | ||
| 227 | |||
| 228 | has tweet_everything => ( | ||
| 229 | is => 'ro', | ||
| 230 | isa => 'Bool', | ||
| 231 | required => 1, | ||
| 232 | default => sub { 0 }, | ||
| 233 | documentation => 'Do not require a trigger keyword in the RSS for a tweet (default: 0)', | ||
| 234 | ); | ||
| 235 | |||
| 236 | has triggercontainer => ( | ||
| 237 | traits => [ 'Array' ], | ||
| 238 | is => 'ro', | ||
| 239 | isa => 'ArrayRef', | ||
| 240 | default => sub {[]}, | ||
| 241 | documentation => 'Give list of triggering keyword files (comma seperated list of filenames)', | ||
| 242 | ); | ||
| 243 | |||
| 244 | has container => ( | ||
| 245 | traits => [ 'Array' ], | ||
| 246 | is => 'ro', | ||
| 247 | isa => 'ArrayRef', | ||
| 248 | default => sub {[]}, | ||
| 249 | documentation => 'Give list of keyword files (comma seperated list of filenames)', | ||
| 250 | ); | ||
| 251 | |||
| 252 | has blockercontainer => ( | ||
| 253 | traits => [ 'Array' ], | ||
| 254 | is => 'ro', | ||
| 255 | isa => 'ArrayRef', | ||
| 256 | default => sub {[]}, | ||
| 257 | documentation => 'Give list of keyword files, which block tweeting that entry (comma seperated list of filenames)', | ||
| 258 | ); | ||
| 259 | |||
| 260 | has shorten_type => ( | ||
| 261 | isa => 'Str', | ||
| 262 | is => 'ro', | ||
| 263 | required => 1, | ||
| 264 | default => sub { 'Metamark' }, | ||
| 265 | documentation => 'Which shorten service to be used, see WWW::Shorten (default: Metamark)', | ||
| 266 | ); | ||
| 267 | |||
| 268 | has shorten_params => ( | ||
| 269 | isa => 'ArrayRef', | ||
| 270 | is => 'ro', | ||
| 271 | required => 1, | ||
| 272 | default => sub {[]}, | ||
| 273 | documentation => 'Parameter used for the WWW::Shorten call, see WWW::Shorten (default: none)', | ||
| 274 | ); | ||
| 275 | |||
| 276 | has tmpdir => ( | ||
| 277 | isa => 'Str', | ||
| 278 | is => 'ro', | ||
| 279 | required => 1, | ||
| 280 | default => sub { getcwd }, | ||
| 281 | documentation => 'Temp directory for the application (default: current directory)', | ||
| 282 | ); | ||
| 283 | |||
| 284 | has debug => ( | ||
| 285 | isa => 'Bool', | ||
| 286 | is => 'ro', | ||
| 287 | default => sub { 0 }, | ||
| 288 | documentation => 'Write debugging into logfile (default: 0)', | ||
| 289 | ); | ||
| 290 | |||
| 291 | has no_logging => ( | ||
| 292 | isa => 'Bool', | ||
| 293 | is => 'ro', | ||
| 294 | default => sub { 0 }, | ||
| 295 | documentation => 'Do not log on screen or file (default: 0)', | ||
| 296 | ); | ||
| 297 | |||
| 298 | has logfile => ( | ||
| 299 | isa => 'Str', | ||
| 300 | is => 'ro', | ||
| 301 | default => sub { shift->progname.'.log' }, | ||
| 302 | documentation => 'Name of the logfile in the configuration directory (default: twitch.log)', | ||
| 303 | ); | ||
| 304 | |||
| 305 | has ignore_first => ( | ||
| 306 | isa => 'Str', | ||
| 307 | is => 'ro', | ||
| 308 | required => 1, | ||
| 309 | default => sub { 1 }, | ||
| 310 | documentation => 'When no cache file exist, ignore the first incoming feed news (default: 1)', | ||
| 311 | ); | ||
| 312 | |||
| 313 | has http_agent => ( | ||
| 314 | isa => 'Str', | ||
| 315 | is => 'ro', | ||
| 316 | default => sub { __PACKAGE__.'/'.$VERSION }, | ||
| 317 | documentation => 'HTTP-agent to be used for the HTTP request to fetch the content (default: '.__PACKAGE__.'/'.$VERSION.')', | ||
| 318 | ); | ||
| 319 | |||
| 320 | has [ '+no_double_fork', '+ignore_zombies', '+dont_close_all_files', '+stop_timeout' ] => ( | ||
| 321 | documentation => 'Please see MooseX::Daemonize documentation', | ||
| 322 | ); | ||
| 323 | |||
| 324 | #-------------------------------------------------------- | ||
| 325 | |||
| 326 | has '+basedir' => ( | ||
| 327 | default => sub { shift->tmpdir }, | ||
| 328 | traits => [ 'NoGetopt' ], | ||
| 329 | ); | ||
| 330 | |||
| 331 | has [ '+logger', '+use_logger_singleton', '+help_flag' ] => ( | ||
| 332 | traits => [ 'NoGetopt' ], | ||
| 333 | ); | ||
| 334 | |||
| 335 | sub _generate_containers { | ||
| 336 | my ( $self, $array, $params ) = @_; | ||
| 337 | my @containers; | ||
| 338 | $params = {} if !$params; | ||
| 339 | for (@{$array}) { | ||
| 340 | $self->logger->debug($self->logger_prefix.'Preparing Keywords::Container for blockercontainer'); | ||
| 341 | my @lists; | ||
| 342 | for (split(',',$_)) { | ||
| 343 | $self->logger->debug($self->logger_prefix.'Preparing Keywords::List '.$_); | ||
| 344 | my @lines = grep { $_ = trim($_); } io($self->configdir.'/'.$_)->slurp; | ||
| 345 | push @lists, Text::Keywords::List->new( | ||
| 346 | keywords => \@lines, | ||
| 347 | ); | ||
| 348 | } | ||
| 349 | push @containers, Text::Keywords::Container->new( | ||
| 350 | lists => \@lists, | ||
| 351 | params => $params, | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | return @containers; | ||
| 355 | } | ||
| 356 | |||
| 357 | has _containers => ( | ||
| 358 | traits => [ 'NoGetopt', 'Array' ], | ||
| 359 | is => 'ro', | ||
| 360 | isa => 'ArrayRef[Text::Keywords::Container]', | ||
| 361 | lazy => 1, | ||
| 362 | default => sub { | ||
| 363 | my ( $self ) = @_; | ||
| 364 | $self->logger->debug($self->logger_prefix.'Generating all Keywords::Container'); | ||
| 365 | my @containers; | ||
| 366 | push @containers, $self->_generate_containers($self->blockercontainer, { blocker => 1 }); | ||
| 367 | push @containers, $self->_generate_containers($self->triggercontainer, { trigger => 1 }); | ||
| 368 | push @containers, $self->_generate_containers($self->container); | ||
| 369 | return \@containers; | ||
| 370 | }, | ||
| 371 | ); | ||
| 372 | |||
| 373 | has _max_feeds_count => ( | ||
| 374 | traits => [ 'NoGetopt' ], | ||
| 375 | isa => 'Int', | ||
| 376 | is => 'ro', | ||
| 377 | default => sub { shift->feeds_count }, | ||
| 378 | ); | ||
| 379 | |||
| 380 | has _feedaggregator => ( | ||
| 381 | traits => [ 'NoGetopt' ], | ||
| 382 | isa => 'POE::Component::FeedAggregator', | ||
| 383 | is => 'ro', | ||
| 384 | lazy => 1, | ||
| 385 | default => sub { | ||
| 386 | my $self = shift; | ||
| 387 | $self->logger->debug($self->logger_prefix.'Starting POE::Component::FeedAggregator'); | ||
| 388 | POE::Component::FeedAggregator->new( | ||
| 389 | tmpdir => $self->tmpdir, | ||
| 390 | logger => $self->logger, | ||
| 391 | http_agent => $self->http_agent, | ||
| 392 | ); | ||
| 393 | }, | ||
| 394 | ); | ||
| 395 | |||
| 396 | has _twitter => ( | ||
| 397 | traits => [ 'NoGetopt' ], | ||
| 398 | isa => 'Net::Twitter', | ||
| 399 | is => 'ro', | ||
| 400 | lazy => 1, | ||
| 401 | default => sub { | ||
| 402 | my $self = shift; | ||
| 403 | $self->logger->debug($self->logger_prefix.'Starting Net::Twitter'); | ||
| 404 | Net::Twitter->new( | ||
| 405 | traits => [qw/ API::REST API::Search OAuth /], | ||
| 406 | consumer_key => $self->consumer_key, | ||
| 407 | consumer_secret => $self->consumer_secret, | ||
| 408 | access_token => $self->access_token, | ||
| 409 | access_token_secret => $self->access_token_secret, | ||
| 410 | ), | ||
| 411 | }, | ||
| 412 | ); | ||
| 413 | |||
| 414 | has _session => ( | ||
| 415 | is => 'rw', | ||
| 416 | isa => 'POE::Session', | ||
| 417 | traits => [ 'NoGetopt' ], | ||
| 418 | ); | ||
| 419 | |||
| 420 | has _keywords => ( | ||
| 421 | is => 'ro', | ||
| 422 | isa => 'Text::Keywords', | ||
| 423 | traits => [ 'NoGetopt' ], | ||
| 424 | lazy => 1, | ||
| 425 | default => sub { | ||
| 426 | Text::Keywords->new( | ||
| 427 | containers => shift->_containers, | ||
| 428 | ) | ||
| 429 | }, | ||
| 430 | ); | ||
| 431 | |||
| 432 | has _tweet => ( | ||
| 433 | is => 'ro', | ||
| 434 | isa => 'Text::Tweet', | ||
| 435 | traits => [ 'NoGetopt' ], | ||
| 436 | lazy => 1, | ||
| 437 | default => sub { | ||
| 438 | Text::Tweet->new() | ||
| 439 | }, | ||
| 440 | ); | ||
| 441 | |||
| 442 | has _http_alias => ( | ||
| 443 | is => 'rw', | ||
| 444 | isa => 'Str', | ||
| 445 | traits => [ 'NoGetopt' ], | ||
| 446 | default => sub { 'http' }, | ||
| 447 | ); | ||
| 448 | |||
| 449 | has _keepalive => ( | ||
| 450 | isa => 'POE::Component::Client::Keepalive', | ||
| 451 | is => 'ro', | ||
| 452 | traits => [ 'NoGetopt' ], | ||
| 453 | lazy => 1, | ||
| 454 | default => sub { | ||
| 455 | my ( $self ) = @_; | ||
| 456 | $self->logger->debug($self->logger_prefix.'Startup POE::Component::Client::Keepalive'); | ||
| 457 | POE::Component::Client::Keepalive->new( | ||
| 458 | keep_alive => 20, # seconds to keep connections alive | ||
| 459 | max_open => 100, # max concurrent connections - total | ||
| 460 | max_per_host => 100, # max concurrent connections - per host | ||
| 461 | timeout => 10, # max time (seconds) to establish a new connection | ||
| 462 | ) | ||
| 463 | }, | ||
| 464 | ); | ||
| 465 | |||
| 466 | has _shorten => ( | ||
| 467 | isa => 'POE::Component::WWW::Shorten', | ||
| 468 | is => 'ro', | ||
| 469 | traits => [ 'NoGetopt' ], | ||
| 470 | lazy => 1, | ||
| 471 | default => sub { | ||
| 472 | my ( $self ) = @_; | ||
| 473 | $self->logger->debug($self->logger_prefix.'Startup '.$self->shorten_type.' Shorten Service...'); | ||
| 474 | return POE::Component::WWW::Shorten->spawn( | ||
| 475 | alias => $self->_shorten_alias, | ||
| 476 | type => $self->shorten_type, | ||
| 477 | params => $self->shorten_params, | ||
| 478 | ); | ||
| 479 | }, | ||
| 480 | ); | ||
| 481 | |||
| 482 | has _shorten_alias => ( | ||
| 483 | is => 'rw', | ||
| 484 | isa => 'Str', | ||
| 485 | traits => [ 'NoGetopt' ], | ||
| 486 | default => sub { 'shorten' }, | ||
| 487 | ); | ||
| 488 | |||
| 489 | has _entry_count => ( | ||
| 490 | traits => [ 'Counter', 'NoGetopt' ], | ||
| 491 | is => 'ro', | ||
| 492 | isa => 'Num', | ||
| 493 | default => 0, | ||
| 494 | handles => { | ||
| 495 | _entry_count_inc => 'inc', | ||
| 496 | }, | ||
| 497 | ); | ||
| 498 | |||
| 499 | sub running_config { shift->_running_config(@_) } | ||
| 500 | |||
| 501 | has _running_config => ( | ||
| 502 | is => 'rw', | ||
| 503 | isa => 'HashRef', | ||
| 504 | traits => [ 'NoGetopt' ], | ||
| 505 | lazy => 1, | ||
| 506 | default => sub { | ||
| 507 | my $self = shift; | ||
| 508 | my %attributes; | ||
| 509 | for ($self->meta->get_all_attributes) { | ||
| 510 | if (!$_->does('MooseX::Getopt::Meta::Attribute::Trait::NoGetopt')) { | ||
| 511 | my $value = $_->get_value($self); | ||
| 512 | $value = $value->file if (blessed $value and $value->isa('MooseX::Daemonize::Pid::File')); | ||
| 513 | $value = $value->stringify if (blessed $value and ( $value->isa('Path::Class::Dir') or $value->isa('Path::Class::File') )); | ||
| 514 | $attributes{$_->name} = $value; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | $attributes{INC} = \@INC; | ||
| 518 | $attributes{PACKAGE} = __PACKAGE__; | ||
| 519 | $attributes{getcwd} = getcwd; | ||
| 520 | $attributes{getpid} = getpid; | ||
| 521 | $attributes{started_time} = time; | ||
| 522 | return \%attributes; | ||
| 523 | }, | ||
| 524 | ); | ||
| 525 | |||
| 526 | sub set_process_name { | ||
| 527 | my ( $self ) = @_; | ||
| 528 | $0 = __PACKAGE__.' '.$VERSION.' using '.( getcwd ).'/'.$self->configfile; | ||
| 529 | } | ||
| 530 | |||
| 531 | sub run { | ||
| 532 | my ( $self ) = @_; | ||
| 533 | POE::Kernel->run; | ||
| 534 | if (!blessed $self) { | ||
| 535 | $self = $self->new_with_options; | ||
| 536 | } | ||
| 537 | my ( $cmd ) = @{$self->extra_argv}; | ||
| 538 | $cmd = 'start' if !$cmd; | ||
| 539 | |||
| 540 | print "Using configfile: ".( getcwd ).'/'.$self->configfile."\n"; | ||
| 541 | print "Status: ".$self->status_message."\n" if ($cmd ne 'status' && $self->status); | ||
| 542 | print "\n"; | ||
| 543 | |||
| 544 | die __PACKAGE__." already running...\n" if ($cmd eq 'start' && $self->status); | ||
| 545 | die __PACKAGE__." not running...\n" if ($cmd eq 'stop' && !$self->status); | ||
| 546 | |||
| 547 | if ($cmd eq 'start') { | ||
| 548 | print "Starting up ".__PACKAGE__."...\n"; | ||
| 549 | $self->start; | ||
| 550 | }; | ||
| 551 | if ($cmd eq 'status') { | ||
| 552 | print "Status: ".__PACKAGE__." is ".( $self->status ? '' : 'not ')."running...\n"; | ||
| 553 | exit $self->status ? 0 : 1; | ||
| 554 | } | ||
| 555 | if ($cmd eq 'restart') { | ||
| 556 | if ($self->status) { | ||
| 557 | print __PACKAGE__." not running, starting up...\n"; | ||
| 558 | } else { | ||
| 559 | print __PACKAGE__." restarting...\n"; | ||
| 560 | } | ||
| 561 | $self->restart; | ||
| 562 | } | ||
| 563 | if ($cmd eq 'stop') { | ||
| 564 | print "Stopping ".__PACKAGE__."...\n"; | ||
| 565 | $self->stop; | ||
| 566 | }; | ||
| 567 | |||
| 568 | exit $self->exit_code; | ||
| 569 | } | ||
| 570 | |||
| 571 | sub START { | ||
| 572 | my ( $self, $session ) = @_[ OBJECT, SESSION ]; | ||
| 573 | $self->set_process_name; | ||
| 574 | $self->logger->info($self->logger_prefix.'Starting up App::Twitch '.$App::Twitch::VERSION.'... '); | ||
| 575 | $self->logger->debug($self->logger_prefix.'Assigning POE::Session'); | ||
| 576 | $self->_session($session); | ||
| 577 | $self->_containers; | ||
| 578 | $self->_twitter if !$self->dryrun; | ||
| 579 | $self->_tweet; | ||
| 580 | $self->_keywords; | ||
| 581 | $self->_feedaggregator; | ||
| 582 | $self->_shorten if !$self->dryrun; | ||
| 583 | $self->logger->info($self->logger_prefix.'Startup HTTP Service...'); | ||
| 584 | POE::Component::Client::HTTP->spawn( | ||
| 585 | Agent => $self->http_agent, | ||
| 586 | Alias => $self->_http_alias, | ||
| 587 | Timeout => 30, | ||
| 588 | ConnectionManager => $self->_keepalive, | ||
| 589 | FollowRedirects => 5, | ||
| 590 | ); | ||
| 591 | $self->_max_feeds_count; | ||
| 592 | my $running_config_dumpfile = $self->tmpdir.'/'.$self->configfile; | ||
| 593 | $running_config_dumpfile =~ s/\.yml/\.running_config\.yml/; | ||
| 594 | DumpFile($running_config_dumpfile,$self->running_config); | ||
| 595 | chmod 0600, $running_config_dumpfile; | ||
| 596 | $self->yield('add_feed'); | ||
| 597 | } | ||
| 598 | |||
| 599 | event add_feed => sub { | ||
| 600 | my ( $self, $kernel ) = @_[ OBJECT, KERNEL ]; | ||
| 601 | my $feed_url = $self->feeds_shift; | ||
| 602 | $self->logger->info($self->logger_prefix.'Adding feed: '.$feed_url); | ||
| 603 | eval { | ||
| 604 | my $feed = { | ||
| 605 | url => $feed_url, | ||
| 606 | delay => $self->feed_delay, | ||
| 607 | max_headlines => 100, | ||
| 608 | ignore_first => $self->ignore_first, | ||
| 609 | }; | ||
| 610 | $self->_feedaggregator->add_feed($feed); | ||
| 611 | }; | ||
| 612 | $self->logger->error($self->logger_prefix.'ERROR ['.$feed_url.']: '.$@) if $@; | ||
| 613 | my $delay = floor( $self->feed_delay / $self->_max_feeds_count ); | ||
| 614 | $kernel->delay('add_feed',$delay) if $self->feeds_count; | ||
| 615 | }; | ||
| 616 | |||
| 617 | event new_feed_entry => sub { | ||
| 618 | my ( $self, $feed, $entry ) = @_[ OBJECT, ARG0..$#_ ]; | ||
| 619 | $self->_entry_count_inc; | ||
| 620 | my $url = $entry->link; | ||
| 621 | $url =~ s/ //g; | ||
| 622 | my $event = { | ||
| 623 | entry => $entry, | ||
| 624 | url => $url, | ||
| 625 | run_id => $self->_entry_count, | ||
| 626 | }; | ||
| 627 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} New feed entry: '.$url); | ||
| 628 | POE::Kernel->post( | ||
| 629 | $self->_http_alias, | ||
| 630 | 'request', | ||
| 631 | 'new_content', | ||
| 632 | HTTP::Request->new(GET => $url), | ||
| 633 | $event, | ||
| 634 | ); | ||
| 635 | }; | ||
| 636 | |||
| 637 | use Encode; | ||
| 638 | require Encode::Detect; | ||
| 639 | |||
| 640 | event new_content => sub { | ||
| 641 | my ( $self, $request_packet, $response_packet ) = @_[ OBJECT, ARG0..$#_ ]; | ||
| 642 | my $event = $request_packet->[1]; | ||
| 643 | my $response = $response_packet->[0]; | ||
| 644 | eval { | ||
| 645 | if ($response->code == 200) { | ||
| 646 | my $extractor = HTML::ExtractContent->new; | ||
| 647 | my $content = $response->decoded_content; | ||
| 648 | my $title = $event->{entry}->title; | ||
| 649 | if (!utf8::is_utf8($content)) { | ||
| 650 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} No utf8, trying recode content'); | ||
| 651 | $content = decode("Detect", $content); | ||
| 652 | } | ||
| 653 | if (utf8::is_utf8($content)) { | ||
| 654 | $extractor->extract($content); | ||
| 655 | my $extracted_text = $extractor->as_text; | ||
| 656 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Extracted content with '.length($extracted_text).' chars'); | ||
| 657 | $event->{content} = $extracted_text; | ||
| 658 | my @keywords = $self->_keywords->from($title, $extracted_text); | ||
| 659 | if ($self->debug && @keywords) { | ||
| 660 | my @keywords_text; | ||
| 661 | push @keywords_text, $_->found for (@keywords); | ||
| 662 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Keywords found: '.join(", ",@keywords_text)); | ||
| 663 | } | ||
| 664 | if ( $keywords[0] && $keywords[0]->container->params->{blocker} ) { | ||
| 665 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Blocker found, ignoring entry'); | ||
| 666 | } elsif ( $self->tweet_everything || ( $keywords[0] && $keywords[0]->container->params->{trigger} ) ) { | ||
| 667 | $event->{keywords} = \@keywords; | ||
| 668 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Trigger keyword found in: '.$title) if (!$self->tweet_everything); | ||
| 669 | if ($self->dryrun) { | ||
| 670 | $self->yield('new_shortened',{ | ||
| 671 | short => $self->dryrun_url, | ||
| 672 | _twitch_event => $event, | ||
| 673 | }); | ||
| 674 | } else { | ||
| 675 | $self->_shorten->shorten({ | ||
| 676 | url => $event->{url}, | ||
| 677 | event => 'new_shortened', | ||
| 678 | _twitch_event => $event, | ||
| 679 | }); | ||
| 680 | } | ||
| 681 | } else { | ||
| 682 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Yeah... what i care... doing nothing with it'); | ||
| 683 | } | ||
| 684 | } else { | ||
| 685 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Is no UTF8'); | ||
| 686 | } | ||
| 687 | } else { | ||
| 688 | $self->logger->error($self->logger_prefix.'{'.$event->{run_id}.'} Wrong HTTP Code '.$response->code); | ||
| 689 | } | ||
| 690 | }; | ||
| 691 | $self->logger->error($self->logger_prefix.'{'.$event->{run_id}.'} ERROR [content handling]: '.$@) if $@; | ||
| 692 | }; | ||
| 693 | |||
| 694 | event new_shortened => sub { | ||
| 695 | my ( $self, $returned ) = @_[ OBJECT, ARG0..$#_ ]; | ||
| 696 | my $event = $returned->{_twitch_event}; | ||
| 697 | eval { | ||
| 698 | my $title = $event->{entry}->title; | ||
| 699 | my $content = $event->{content}; | ||
| 700 | my $url = $event->{url}; | ||
| 701 | my @keywords = @{$event->{keywords}}; | ||
| 702 | if ($returned->{short}) { | ||
| 703 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Received ShortURL'); | ||
| 704 | my $short = $returned->{short}; | ||
| 705 | my @keywords_text; | ||
| 706 | for (@keywords) { | ||
| 707 | push @keywords_text, $_->found; | ||
| 708 | } | ||
| 709 | $event->{tweet} = $self->_tweet->make(\@keywords_text,$title,\$short); | ||
| 710 | $self->twitter_update($event); | ||
| 711 | } else { | ||
| 712 | $self->logger->error($self->logger_prefix.'{'.$event->{run_id}.'} Failing generation of ShortURL'); | ||
| 713 | } | ||
| 714 | }; | ||
| 715 | $self->logger->error($self->logger_prefix.'{'.$event->{run_id}.'} ERROR [finalize and tweeting]: '.$@) if $@; | ||
| 716 | }; | ||
| 717 | |||
| 718 | sub twitter_update { | ||
| 719 | my ( $self, $event ) = @_; | ||
| 720 | my $tweet = $event->{tweet}; | ||
| 721 | $self->logger->info($self->logger_prefix.'{'.$event->{run_id}.'} Twitter update: '.$tweet); | ||
| 722 | if (!$self->dryrun) { | ||
| 723 | eval { | ||
| 724 | $self->_twitter->update({ status => $tweet }); | ||
| 725 | }; | ||
| 726 | if ($@) { | ||
| 727 | $self->logger->error('('.$event->{run_id}.') ERROR [twitter]: '.$@); | ||
| 728 | return 0; | ||
| 729 | } | ||
| 730 | } else { | ||
| 731 | $self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} dryrun set, not really twittering it!'); | ||
| 732 | } | ||
| 733 | return 1; | ||
| 734 | } | ||
| 735 | |||
| 736 | sub logger_prefix { | ||
| 737 | my $self = shift; | ||
| 738 | __PACKAGE__.' ('.$self->get_session_id.') '; | ||
| 739 | } | ||
| 740 | |||
| 741 | __PACKAGE__->meta->make_immutable; | ||
| 742 | |||
| 743 | 1; | ||
| 744 | |||
| 745 | |||
| 746 | |||
| 747 | =pod | ||
| 748 | |||
| 749 | =head1 NAME | ||
| 750 | |||
| 751 | App::Twitch - Your personal Twitter b...... lalalala | ||
| 752 | |||
| 753 | =head1 VERSION | ||
| 754 | |||
| 755 | version 0.904 | ||
| 756 | |||
| 757 | =head1 DESCRIPTION | ||
| 758 | |||
| 759 | More documentation coming soon.... | ||
| 760 | |||
| 761 | =head1 SEE ALSO | ||
| 762 | |||
| 763 | =over 4 | ||
| 764 | |||
| 765 | =item * | ||
| 766 | |||
| 767 | L<Net::Twitter> | ||
| 768 | |||
| 769 | =item * | ||
| 770 | |||
| 771 | L<POE::Component::FeedAggregator> | ||
| 772 | |||
| 773 | =item * | ||
| 774 | |||
| 775 | L<MooseX::POE> | ||
| 776 | |||
| 777 | =back | ||
| 778 | |||
| 779 | =head1 AUTHOR | ||
| 780 | |||
| 781 | Torsten Raudssus <torsten@raudssus.de> L<http://www.raudssus.de/> | ||
| 782 | |||
| 783 | =head1 COPYRIGHT AND LICENSE | ||
| 784 | |||
| 785 | This software is copyright (c) 2010 by L<Raudssus Social Software|http://www.raudssus.de/>. | ||
| 786 | |||
| 787 | This is free software; you can redistribute it and/or modify it under | ||
| 788 | the same terms as the Perl 5 programming language system itself. | ||
| 789 | |||
| 790 | =cut | ||
| 791 | |||
| 792 | |||
| 793 | __END__ | ||