Skip to content

Instantly share code, notes, and snippets.

@leedo
Created May 27, 2011 22:01
Show Gist options
  • Save leedo/996272 to your computer and use it in GitHub Desktop.
Save leedo/996272 to your computer and use it in GitHub Desktop.
tweet rss feed updates
#!/usr/bin/env perl
use JSON;
use AnyEvent;
use AnyEvent::Twitter;
use AnyEvent::Feed;
die "need json file with oauth credentials\n" unless -e $ARGV[0];
my @queue;
my $credentials = do {
undef $/;
open my $fh, "<", $ARGV[0] or die "could not open oauth file\n";
my $json = <$fh>;
decode_json $json;
};
my $twitter = AnyEvent::Twitter->new($credentials);
my $feed = AnyEvent::Feed->new(
url => 'http://feeds.arstechnica.com/arstechnica/index/',
interval => 5 * 60,
on_fetch => sub {
my ($feed, $new, $feed, $error) = @_;
if ($error) {
warn "FEED ERROR: $error\n";
return;
}
for (@$new) {
my ($hash, $entry) = @$_;
push @queue, $entry->title . " " . $entry->link;
}
print "added ".scalar(@$new)." new articles to the queue\n" if @$new;
}
);
my $timer = AE::timer 30, 60, sub {
my $item = shift @queue;
return unless $item;
$twitter->post(
'statuses/update',
{ status => $item },
sub {
my ($hdr, $res, $reason) = @_;
if ($res) {
print "posted $item\n";
}
else {
warn "TWITTER ERROR: $reason\n";
}
}
);
};
AE::cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment