Last active
March 18, 2016 16:57
-
-
Save DakuTree/b5d18a6963cffb517566 to your computer and use it in GitHub Desktop.
Email when Twitter followed users change
This file contains hidden or 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
#!/usr/bin/perl -w | |
# Recently I've noticed twitter randomly removed some of the people I follow for no reason. I created this as an easy automated way to track those changes. | |
# The idea is you run it once a day (via cron, task scheduler or other), and it compares against the list it saved the previous day. If there is changes, it emails you with the changes. | |
# NOTE: You may run into rate limitting issues if you are following say, 2k+ users. You shouldn't be following so many people though, use lists! | |
#### MODULES #### | |
use Net::Twitter; | |
use File::Slurp; | |
use Array::Diff; | |
use Email::Sender::Simple qw(sendmail); | |
use Email::Sender::Transport::SMTP::TLS; | |
use Email::Simple; | |
use Email::Simple::Creator; | |
use Config::Simple; | |
use strict; | |
use warnings; | |
no warnings 'experimental::autoderef'; | |
#### CONFIG #### | |
my $cfg = new Config::Simple('twitter_followed_config.ini'); #optionally load external config file (see: http://search.cpan.org/~sherzodr/Config-Simple-4.59/Simple.pm#SIMPLIFIED_INI-FILE) | |
my $config = ($cfg ? $cfg->vars() : {}); | |
# To get these keys you need to create a twitter app @ https://apps.twitter.com/ | |
my $consumer_key = $config->{'default.consumer_key'} || ""; | |
my $consumer_secret = $config->{'default.consumer_secret'} || ""; | |
my $access_token = $config->{'default.access_token'} || ""; | |
my $access_token_secret = $config->{'default.access_token_secret'} || ""; | |
# Email Settings. This <assumes> you are using gmail. This may/may not work with other services. | |
my $email_host = $config->{'default.email_host'} || ""; | |
my $email_port = $config->{'default.email_port'} || ""; | |
my $email_user = $config->{'default.email_user'} || ""; | |
my $email_pass = $config->{'default.email_pass'} || ""; | |
my $email_to = $config->{'default.email_to'} || ""; | |
#### SCRIPT #### | |
my $nt = Net::Twitter->new( | |
traits => [qw/API::RESTv1_1/], | |
consumer_key => $consumer_key, | |
consumer_secret => $consumer_secret, | |
access_token => $access_token, | |
access_token_secret => $access_token_secret | |
); | |
my @followed_users_old = read_file('twitter_followed.txt', chomp => 1); | |
my @followed_users_new; | |
for (my $cursor = -1, my $r; $cursor; $cursor = $r->{next_cursor}) { | |
$r = $nt->friends({ cursor => $cursor, count => 200, skip_status => 1 }); | |
for my $user (keys $r->{users}) { | |
push(@followed_users_new, $r->{users}[$user]->{screen_name}); | |
} | |
sleep(3); #Let's try and avoid any rate limiting. | |
} | |
@followed_users_new = sort { lc($a) cmp lc($b) } @followed_users_new; #Sort alphabetically. | |
my $diff = Array::Diff->diff( \@followed_users_old, \@followed_users_new ); | |
my $mail_message = ""; | |
if($diff->count) { | |
$mail_message .= "Added:\n"; | |
if($diff->added) { | |
$mail_message .= join("", map { "$_\n" } @{$diff->added}); | |
} | |
$mail_message .= "\nDeleted:\n"; | |
if($diff->deleted) { | |
$mail_message .= join("", map { "$_\n" } @{$diff->deleted}); | |
} | |
my $transport = Email::Sender::Transport::SMTP::TLS->new({ | |
host => $email_host, | |
port => $email_port, | |
username => $email_user, | |
password => $email_pass, | |
}); | |
my $email = Email::Simple->create( | |
header => [ | |
To => $email_to, | |
From => $email_user, | |
Subject => "Twitter followers changed! (".$diff->count." changes)", | |
], | |
body => $mail_message, | |
); | |
sendmail($email, { transport => $transport }); | |
} | |
write_file('twitter_followed.txt', map { "$_\n" } @followed_users_new); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment