Skip to content

Commit

Permalink
Added the possibility to retweet
Browse files Browse the repository at this point in the history
New config option "tweet_mode" wich takes values:
 - new_tweet
 - retweet
  • Loading branch information
miquelruiz committed Oct 30, 2013
1 parent 6deddf4 commit 3facfd5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
2 changes: 2 additions & 0 deletions lib/App/cart.pm
Expand Up @@ -185,6 +185,8 @@ delete_keywords: 0
keywords:
- twitter
tweet_mode: new_tweet
CONF
close CONFFILE;
$log->debug("Dumped conf file to '$self->{home}/$self->{conffile}'");
Expand Down
77 changes: 48 additions & 29 deletions lib/App/cart/Injector.pm
Expand Up @@ -11,6 +11,11 @@ use AnyEvent::DateTime::Cron;

use App::cart::Buffer;

my %tweet_mode = (
new_tweet => 'tweet',
retweet => 'retweet',
);

sub new {
my ($class, $conf) = @_;

Expand All @@ -20,12 +25,13 @@ sub new {
my @todotimes = @{ $conf->{publishtimes} };

my $self = bless {
buffer => App::cart::Buffer->new($conf),
alltimes => \@alltimes,
todotimes => \@todotimes,
maxrate => $conf->{maxrate},
keywords => $conf->{keywords},
delete_kw => $conf->{delete_keywords},
buffer => App::cart::Buffer->new($conf),
alltimes => \@alltimes,
todotimes => \@todotimes,
maxrate => $conf->{maxrate},
keywords => $conf->{keywords},
delete_kw => $conf->{delete_keywords},
tweet_handler => $tweet_mode{$conf->{tweet_mode}},
}, $class;

# Get an authenticated Twitter client
Expand Down Expand Up @@ -105,42 +111,55 @@ sub reeschedule {
after => 0,
interval => $interval,
cb => sub {
$self->tweet;
my $handler = $self->{tweet_handler};

my $tweet = $self->{buffer}->bshift;
if (defined $tweet) {
$self->$handler($tweet)
} else {
# Stop publication
undef $self->{publisher};
$log->debug('Publication stopped: no more buffered tweets');
}

$self->{tweets_left} = $self->{tweets_left} - 1;
undef $self->{publisher} unless $self->{tweets_left};
}
);
};

sub tweet {
my ($self) = @_;
my ($self, $tweet) = @_;

my $tweet = $self->{buffer}->bshift;
if (defined $tweet and defined $tweet->{data}) {
my $text = $tweet->{data};
return unless $tweet->{data};
my $text = $tweet->{data};

# Delete keywords if needed
if ($self->{delete_kw}) {
foreach (@{ $self->{keywords} }) {
$text =~ s/$_//;
}
# Delete keywords if needed
if ($self->{delete_kw}) {
foreach (@{ $self->{keywords} }) {
$text =~ s/$_//;
}
}

my $tweeted = 0;
try {
$self->{nt}->update($text);
$tweeted = 1;
} catch {
$log->error("Couldn't update: $_");
};
try {
$self->{nt}->update($text);
$log->info("Just tweeted: $text");
} catch {
$log->error("Couldn't update: $_");
};

$log->info("Just tweeted: $text" ) if $tweeted;
}

} else {
# Stop publication
undef $self->{publisher};
$log->debug('Publication stopped: no more buffered tweets');
}
sub retweet {
my ($self, $tweet) = @_;
return unless $tweet->{id};

try {
$self->{nt}->retweet($tweet->{id});
$log->info("Just retweeted: " . $tweet->{data});
} catch {
$log->error("Couldn't retweet: $_");
};
}

1;

0 comments on commit 3facfd5

Please sign in to comment.