App-Twitch
view release on metacpan or search on metacpan
lib/App/Twitch.pm view on Meta::CPAN
package App::Twitch;
BEGIN {
$App::Twitch::AUTHORITY = 'cpan:GETTY';
}
BEGIN {
$App::Twitch::VERSION = '0.904';
}
# ABSTRACT: Your personal Twitter b...... lalalala
# Speed up complete POE Kernel
sub POE::Kernel::USE_SIGCHLD () { 1 }
use MooseX::POE;
with qw(
MooseX::Getopt
MooseX::SimpleConfig
MooseX::LogDispatch
MooseX::Daemonize
);
use POE qw(
Component::Client::HTTP
Component::Client::Keepalive
Component::FeedAggregator
Component::WWW::Shorten
);
use HTTP::Request;
use Text::Trim;
use URI;
use POSIX;
use IO::All;
use String::Truncate qw(elide);
use utf8;
use Text::Keywords;
use Text::Keywords::Container;
use Text::Keywords::List;
use Text::Tweet;
use HTML::ExtractContent;
use Carp qw( croak );
use YAML qw( DumpFile );
# could be ... ah forget it :-P
use Net::Twitter;
our $VERSION ||= '0.0development';
before run => sub {
my $package = __PACKAGE__;
print <<"END_OF_INTRO";
_ _ _ _
| |_ _ _(_) |_ ___| |___
| _\\ \\ /\\ / / | __/ __| '_ \\
| |_ \\ V V /| | || (__| | | | $package $VERSION
\\__| \\_/\\_/ |_|\\__\\___|_| |_| Usage with --help
More information under http://search.cpan.org/perldoc/twitch
Commands:
start Start the twitch
stop Stop the running twitch
restart Restart the twitch, or start it up if not running
status Give current status of the twitch
END_OF_INTRO
};
after start => sub {
my $self = shift;
return unless $self->is_daemon;
# Required, elsewhere your POE goes nuts
POE::Kernel->has_forked if !$self->foreground;
POE::Kernel->run;
};
has '+pidbase' => (
default => sub { shift->tmpdir },
documentation => 'Directory for the pid file (default: tmpdir)',
);
has '+pidfile' => (
documentation => 'Filename for the pidfile (default: basedir/progname.pid)',
);
has '+progname' => (
default => sub { 'twitch' },
documentation => 'Name for the application, like configfile name base and so on (default: twitch)',
lib/App/Twitch.pm view on Meta::CPAN
my ( $self, $session ) = @_[ OBJECT, SESSION ];
$self->set_process_name;
$self->logger->info($self->logger_prefix.'Starting up App::Twitch '.$App::Twitch::VERSION.'... ');
$self->logger->debug($self->logger_prefix.'Assigning POE::Session');
$self->_session($session);
$self->_containers;
$self->_twitter if !$self->dryrun;
$self->_tweet;
$self->_keywords;
$self->_feedaggregator;
$self->_shorten if !$self->dryrun;
$self->logger->info($self->logger_prefix.'Startup HTTP Service...');
POE::Component::Client::HTTP->spawn(
Agent => $self->http_agent,
Alias => $self->_http_alias,
Timeout => 30,
ConnectionManager => $self->_keepalive,
FollowRedirects => 5,
);
$self->_max_feeds_count;
my $running_config_dumpfile = $self->tmpdir.'/'.$self->configfile;
$running_config_dumpfile =~ s/\.yml/\.running_config\.yml/;
DumpFile($running_config_dumpfile,$self->running_config);
chmod 0600, $running_config_dumpfile;
$self->yield('add_feed');
}
event add_feed => sub {
my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
my $feed_url = $self->feeds_shift;
$self->logger->info($self->logger_prefix.'Adding feed: '.$feed_url);
eval {
my $feed = {
url => $feed_url,
delay => $self->feed_delay,
max_headlines => 100,
ignore_first => $self->ignore_first,
};
$self->_feedaggregator->add_feed($feed);
};
$self->logger->error($self->logger_prefix.'ERROR ['.$feed_url.']: '.$@) if $@;
my $delay = floor( $self->feed_delay / $self->_max_feeds_count );
$kernel->delay('add_feed',$delay) if $self->feeds_count;
};
event new_feed_entry => sub {
my ( $self, $feed, $entry ) = @_[ OBJECT, ARG0..$#_ ];
$self->_entry_count_inc;
my $url = $entry->link;
$url =~ s/ //g;
my $event = {
entry => $entry,
url => $url,
run_id => $self->_entry_count,
};
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} New feed entry: '.$url);
POE::Kernel->post(
$self->_http_alias,
'request',
'new_content',
HTTP::Request->new(GET => $url),
$event,
);
};
use Encode;
require Encode::Detect;
event new_content => sub {
my ( $self, $request_packet, $response_packet ) = @_[ OBJECT, ARG0..$#_ ];
my $event = $request_packet->[1];
my $response = $response_packet->[0];
eval {
if ($response->code == 200) {
my $extractor = HTML::ExtractContent->new;
my $content = $response->decoded_content;
my $title = $event->{entry}->title;
if (!utf8::is_utf8($content)) {
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} No utf8, trying recode content');
$content = decode("Detect", $content);
}
if (utf8::is_utf8($content)) {
$extractor->extract($content);
my $extracted_text = $extractor->as_text;
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Extracted content with '.length($extracted_text).' chars');
$event->{content} = $extracted_text;
my @keywords = $self->_keywords->from($title, $extracted_text);
if ($self->debug && @keywords) {
my @keywords_text;
push @keywords_text, $_->found for (@keywords);
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Keywords found: '.join(", ",@keywords_text));
}
if ( $keywords[0] && $keywords[0]->container->params->{blocker} ) {
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Blocker found, ignoring entry');
} elsif ( $self->tweet_everything || ( $keywords[0] && $keywords[0]->container->params->{trigger} ) ) {
$event->{keywords} = \@keywords;
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Trigger keyword found in: '.$title) if (!$self->tweet_everything);
if ($self->dryrun) {
$self->yield('new_shortened',{
short => $self->dryrun_url,
_twitch_event => $event,
});
} else {
$self->_shorten->shorten({
url => $event->{url},
event => 'new_shortened',
_twitch_event => $event,
});
}
} else {
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Yeah... what i care... doing nothing with it');
}
} else {
$self->logger->debug($self->logger_prefix.'{'.$event->{run_id}.'} Is no UTF8');
}
} else {
$self->logger->error($self->logger_prefix.'{'.$event->{run_id}.'} Wrong HTTP Code '.$response->code);
}
};
$self->logger->error($self->logger_prefix.'{'.$event->{run_id}.'} ERROR [content handling]: '.$@) if $@;
};
( run in 0.784 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )