Skip to content

Instantly share code, notes, and snippets.

@norm
Created July 23, 2009 12:43
Show Gist options
  • Save norm/152857 to your computer and use it in GitHub Desktop.
Save norm/152857 to your computer and use it in GitHub Desktop.
prowl_api_key = [redacted]
[cackhanded]
password = [redacted]
skip_dm = 1
[pubstandards]
password = [redacted]
skip_mention = 1
#!/usr/bin/env perl
use strict;
use warnings;
use Config::Std { def_sep => '=' };
use Getopt::Std;
use Net::Twitter::Lite;
use WebService::Prowl;
my %opt;
read_config 'twitter.conf' => my %config;
getopts( 'a:dms', \%opt );
my $prowl = WebService::Prowl->new( apikey => $config{''}{'prowl_api_key'} );
$prowl->verify()
or die $prowl->error();
my @accounts = get_accounts();
my $twitter;
foreach my $account ( sort @accounts ) {
status( "-> $account" );
$twitter = Net::Twitter::Lite->new(
username => $account,
password => $config{ $account }{'password'},
);
if ( !defined $config{ $account }{'skip_dm'} ) {
check_direct_messages( $account );
}
if ( !defined $config{ $account }{'skip_mention'} ) {
check_mentions( $account );
}
}
write_config %config;
exit;
sub get_accounts {
my @accounts;
if ( $opt{'a'} ) {
push @accounts, $opt{'a'};
}
else {
foreach my $section ( keys %config ) {
next if '' eq $section;
push @accounts, $section;
}
}
return @accounts;
}
sub check_direct_messages {
my $account = shift;
my $last_dm = $config{ $account }{'last_dm'} || 1;
my $dms = $twitter->direct_messages( { since_id => $last_dm } );
foreach my $dm ( @{ $dms } ) {
my $from = $dm->{'sender_screen_name'};
my $body = $dm->{'text'};
my $id = $dm->{'id'};
if ( $id > $last_dm ) {
$last_dm = $id;
}
my $message = sprintf "%s: %s\n", $from, $body;
status( $message );
$prowl->add(
application => 'Twitter DM',
event => $account,
description => $message
);
}
$config{ $account }{'last_dm'} = $last_dm;
}
sub check_mentions {
my $account = shift;
my $last_mention = $config{ $account }{'last_mention'} || 1;
my $mentions = $twitter->mentions( { since_id => $last_mention } );
foreach my $mention ( @{ $mentions } ) {
my $from = $mention->{'user'}{'screen_name'};
my $body = $mention->{'text'};
my $id = $mention->{'id'};
if ( $id > $last_mention ) {
$last_mention = $id;
}
my $message = sprintf "%s: %s\n", $from, $body;
status( $message );
$prowl->add(
application => 'Twitter Mention',
event => $account,
description => $message
);
}
$config{ $account }{'last_mention'} = $last_mention;
}
sub status {
my $text = shift;
print "${text}\n" if $opt{'s'};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment