-
-
Save mattn/563440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Net::Twitter; | |
use Path::Class; | |
use Array::Diff; | |
use Config::Pit; | |
use Getopt::Long; | |
use LWP::Simple; | |
use File::Temp; | |
use File::Spec; | |
use Data::Dumper; | |
my $AppName = "follow-management"; | |
my $FileName; | |
my $oauth_pit; | |
my $consumer_pit; | |
GetOptions('db=s' => \$FileName, 'oauth=s' => \$oauth_pit, 'consumer=s' => \$consumer_pit); | |
my $oauth_config = pit_get($oauth_pit, require => { | |
"access_token" => "YOUR_ACCESS_TOKEN", | |
"access_token_secret" => "YOUR_TOKEN_SECRET", | |
}); # TODO: It have better to get access_token API. | |
my $consumer_config = pit_get($consumer_pit, require => { | |
"consumer_key" => "YOUR_CONSUMER_KEY", | |
"consumer_secret" => "YOUR_CONSUMER_SECRET", | |
}); | |
my $twitter = Net::Twitter->new( | |
traits => [qw/API::REST OAuth WrapError/], | |
consumer_key => $consumer_config->{consumer_key}, | |
consumer_secret => $consumer_config->{consumer_secret}, | |
ssl => 1, | |
); | |
$twitter->access_token($oauth_config->{access_token}); | |
$twitter->access_token_secret($oauth_config->{access_token_secret}); | |
my $file = Path::Class::File->new($FileName); | |
unless ( -e $file ) { | |
save_file ( fetch_twitter() ); | |
growl({ t => $AppName, m => 'imported' }); | |
} else { | |
my $old = read_file(); | |
my $new = fetch_twitter(); | |
my $diff = Array::Diff->diff( $old, $new ); | |
notify( $diff->added, "%s (%s) follow you.\n" ); | |
notify( $diff->deleted, "%s (%s) remove you.\n" ); | |
save_file ( $new ); | |
} | |
sub fetch_twitter { | |
my $cursor = -1; | |
my @ids; | |
for ( my $cursor = -1, my $r; $cursor; $cursor = $r->{next_cursor}) { | |
$r = $twitter->followers_ids({ cursor => $cursor }); | |
unless ($r) { | |
growl({ t => $AppName, m => 'fetch error' }, 1); | |
exit; | |
} | |
push @ids, @{ $r->{ids} }; | |
} | |
\@ids; | |
} | |
sub read_file { | |
my $fh = $file->openr; | |
my @old_ids; | |
while (<$fh>) { | |
s/[\r\n]+//g; | |
push @old_ids, split(',', $_); | |
} | |
$fh->close; | |
\@old_ids; | |
} | |
sub save_file { | |
my $ids = shift; | |
my $fh = $file->openw; | |
print $fh join(',', @$ids); | |
$fh->close; | |
} | |
sub notify { | |
my ($arr, $format) = @_; | |
my $tmpdir = File::Temp::tempdir( CLEANUP => 1 ); | |
for my $id (@$arr) { | |
my $user = $twitter->show_user($id); | |
next unless $user; | |
my $desc = sprintf $format, $user->{screen_name}, $user->{name}; | |
my $url = $user->{profile_image_url}; | |
my $image = File::Spec->catfile( $tmpdir, (split(/\//, $url))[-1] ); | |
LWP::Simple::mirror( $url, $image ); | |
growl({ t => $AppName, m => $desc }, $image, 1 ); | |
} | |
} | |
sub growl { | |
my $options = shift; | |
my $image = shift; | |
my $sticky = shift; | |
my $cmd; | |
while ( my ( $k, $v ) = each ( %$options ) ) { | |
$cmd .= sprintf(" -%s '%s'", $k, $v); | |
} | |
$cmd .= ' -s' if $sticky; | |
$cmd .= sprintf(' --image %s', $image) if $image; | |
system sprintf("/usr/local/bin/growlnotify %s", $cmd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment