This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfollowermeldeamt.pl
executable file
·175 lines (150 loc) · 5.7 KB
/
unfollowermeldeamt.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/perl
# mails log of people who unfollowed the authenticated user
# Copyright 2012-2013 Tobias Wolter
# MIT licence:
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
use Config::Simple;
use Net::Twitter 4.00001;
use Mail::Sendmail;
use Encode;
use Try::Tiny;
use strict;
# config file
my $rcfile = "$ENV{HOME}/.unfollow-mailerrc";
# config container
my $config;
if (-r $rcfile) {
$config = new Config::Simple;
$config->read($rcfile);
die "Need a source mail address!" unless $config->param('mail.source') =~ m/@/;
die "Need a target mail address!" unless $config->param('mail.target') =~ m/@/;
} else {
if (-e $rcfile) {
die "$0: configuration file «$rcfile» not readable\n";
} else {
$config = new Config::Simple('syntax' => 'ini');
foreach my $variable (qw{oauth.consumer_key oauth.consumer_secret oauth.access_token oauth.access_token_secret mail.target mail.source twitter.follower_ids twitter.last_check}) {
$config->param($variable, '');
}
$config->write($rcfile);
print "$0: Blank configuration file «$rcfile» created.\n";
print " Please edit the file to supply the necessary information.\n";
print " You will need to create your own Twitter application;\n";
print " this can be done at https://dev.twitter.com/\n";
exit 0;
}
}
# fluff
my $mailheader = "Hello, this is your friendly Twitter monitoring script thingie!\n\nThe following users unfollowed you";
if ($config->param('twitter.last_check')) {
$mailheader .= " since I was last run on " . $config->param('twitter.last_check');
}
$mailheader .= ":\n\n";
my $mailfooter = "Burn in rage as you curse their ancestors!\n\nHave a nice day!\n";
# FIXME: config sanity checks
my $twitter = Net::Twitter->new(
consumer_key => $config->param('oauth.consumer_key'),
consumer_secret => $config->param('oauth.consumer_secret'),
access_token => $config->param('oauth.access_token'),
access_token_secret => $config->param('oauth.access_token_secret'),
traits => [qw/OAuth API::RESTv1_1/],
ssl => 1,
);
# FIXME: exception handling
my %seen;
# loops for followers; taken mostly straight from Net::Twitter perldoc
for (my $cursor = -1, my $r; $cursor; $cursor = $r->{next_cursor}) {
$r = $twitter->followers_ids({ cursor => $cursor });
foreach (@{$r->{ids}}) {
$seen{$_} = 1;
}
}
# squashedstring
my $seen_string = join(',', sort keys %seen);
# check for initial run
unless ($config->param('twitter.follower_ids')) {
if (-t STDOUT) {
print "Initial import; not checking for unfollowers.\n";
}
$config->param('twitter.follower_ids', $seen_string);
$config->save;
exit 0;
}
# extract people who aren't following anymore
my @unfollowing_ids;
foreach (@{$config->param('twitter.follower_ids')}) {
if ($seen{$_} != 1) {
push(@unfollowing_ids, "$_");
}
}
# write followers to config
$config->param('twitter.follower_ids', $seen_string);
$config->save;
# checki if action necessary
my $number_unfollowers = scalar @unfollowing_ids;
# no unfollowers
if ($number_unfollowers == 0) {
if (-t STDOUT) {
print "No action required, no unfollowers.\n";
}
exit 0;
}
my %unfollowers; my @data;
for (my $i = 0; $i <= int($number_unfollowers/100); $i++) {
my $start = $i*100;
my $end = ($i+1)*100-1;
if ($i == int($number_unfollowers/100)) {
$end = $number_unfollowers;
}
my $package = join(',', @unfollowing_ids[$start .. $end]);
try {
my $reference = $twitter->lookup_users({user_id => $package});
push(@data,@{$reference});
};
}
foreach (@data) {
$unfollowers{$_->{screen_name}} = $_->{name};
}
my $subject = "Twitter: ";
$subject .= ($number_unfollowers == 1) ? "one person " : "$number_unfollowers people ";
$subject .= "unfollowed you!";
my $identified_unfollowers = scalar(keys(%unfollowers));
my $message = '';
if ($identified_unfollowers > 0) {
map { $message .= " * $unfollowers{$_} ($_)\n" } sort(keys(%unfollowers));
my $unidentified_unfollowers = $number_unfollowers - $identified_unfollowers;
if ($unidentified_unfollowers > 0) {
$message .= "\n";
$message .= "Additionally, there were $unidentified_unfollowers accounts which could not be identified.\n";
$message .= "This usually means they were deleted.\n";
}
$message .= "\n";
} else {
$message .= "Sadly, this list is empty because none of them could be identified,";
$message .= " which means they were probably deleted.\n";
}
my %mail = (
'To' => $config->param('mail.target'),
'From' => $config->param('mail.source'),
'Message' => Encode::encode('utf8',"${mailheader}${message}${mailfooter}"),
'Subject' => $subject,
'Content-Type' => 'text/plain; charset="utf-8"',
);
sendmail(%mail) or die $Mail::Sendmail::error;
# log last check time
$config->param('twitter.last_check', scalar localtime);
$config->save;