-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtwitstat.pl
More file actions
executable file
·100 lines (80 loc) · 2.29 KB
/
twitstat.pl
File metadata and controls
executable file
·100 lines (80 loc) · 2.29 KB
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
#!/usr/bin/perl -w
###################################
#
# written by Martin Scharm
# see https://binfalse.de
#
###################################
use Encode;
use warnings;
use strict;
use Net::Twitter;
use Cwd 'abs_path';;
binmode STDOUT, ":utf8";
my $CRED_FILE = abs_path($0) . ".credentials";
my $TOKEN_STR = "token";
my $TOKEN_SEC = "secret";
my $max = 10;
$max = $ARGV[0] if ($ARGV[0] && isdigit ($ARGV[0]));
sub restore_cred
{
my $file = $CRED_FILE;
my $access_token = undef;
my $access_token_secret = undef;
open(CF,'<'.$file) or return ("", "");
while (my $line = <CF>)
{
next if($line =~ /^\s*#/);
next if($line !~ /^\s*\S+\s*=.*$/);
my ($key,$value) = split(/=/,$line,2);
$key =~ s/^\s+//g;
$key =~ s/\s+$//g;
$value =~ s/^\s+//g;
$value =~ s/\s+$//g;
$access_token = $value if ($key eq $TOKEN_STR);
$access_token_secret = $value if ($key eq $TOKEN_SEC);
}
close(CF);
return ($access_token, $access_token_secret);
}
sub save_cred
{
my $file = $CRED_FILE;
my $access_token = shift;
my $access_token_secret = shift;
if (!open(CF,'>'.$file))
{
print "could not save credentials to " . $CRED_FILE . "\n";
return 0;
}
print CF $TOKEN_STR . "=" . $access_token . "\n";
print CF $TOKEN_SEC . "=" . $access_token_secret . "\n";
close(CF);
return 1;
}
my $nt = Net::Twitter->new(traits => ['API::REST', 'OAuth'], consumer_key => "KVgWlkFUWezK5AvJeR6GQ", consumer_secret => "CjuLw8Bh9OHG4DO9lnZnQK6w3UvoqNR1DB7oBwEgb44",);
my ($access_token, $access_token_secret) = restore_cred();
if ($access_token && $access_token_secret)
{
$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);
}
unless ( $nt->authorized )
{
print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN: ";
chomp (my $pin = <STDIN>);
my($access_token, $access_token_secret, $user_id, $screen_name) = $nt->request_access_token(verifier => $pin);
if (save_cred($access_token, $access_token_secret))
{
print "successfull enabled this app! credentials are stored in: " . $CRED_FILE . "\n"
}
else
{
die "failed\n";
}
}
my $tweets = $nt->friends_timeline({count=>$max});
foreach my $hash_ref (@$tweets)
{
print $hash_ref->{'text'} . "\n\tby " . $hash_ref->{'user'}->{'screen_name'} . " at " . $hash_ref->{'created_at'} . "\n\n";
}