-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch-followers
executable file
·65 lines (53 loc) · 1.6 KB
/
fetch-followers
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
#!/usr/bin/env perl
use strict q(vars);
use warnings;
use Data::Dump qw(dump);
use Net::Twitter;
use Text::CSV;
our $consumer_key = q(key);
our $consumer_secret = q(secret);
our $token;
our $token_secret;
sub restore_tokens {
open my $fh, '<', "oauth" or die qq(failed to open file; $!);
my ($access_token, $access_token_secret) = map {
chomp;
s/^[^:]+:// if defined and length;
$_
} <$fh>;
close $fh;
return ($access_token, $access_token_secret);
}
($token,$token_secret) = (restore_tokens);
die "Not Authorized" unless $token && $token_secret;
my $nt = Net::Twitter->new(
traits => [
qw/OAuth API::RESTv1_1/,
],
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
access_token => $token,
access_token_secret => $token_secret,
ssl => 1,
);
my $csv = Text::CSV->new({binary => 1}) or die "CSV init error: " . Text::CSV->error_diag();
open my $fh, ">&:encoding(UTF-8)", STDOUT or die "couldn't dup stdout: $!";
$csv->eol("\n");
my @cols = qw(created_at favourites_count followers_count friends_count location name screen_name statuses_count description);
$csv->print($fh, \@cols);
for (my $cursor = -1, my $i = 1, my $r; $cursor; $i++) {
my $r = $nt->followers_list({cursor => $cursor, count => 200});
if (ref($r->{users}) eq 'ARRAY') {
foreach (@{$r->{users}}) {
my @vals = @$_{@cols};
$csv->print($fh, \@vals);
}
}
$cursor = $r->{next_cursor};
undef $r;
if ($i % 15 == 0) {
sleep(15 * 60);
}
}
close($fh);
exit(0);