-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweet
executable file
·84 lines (72 loc) · 2.4 KB
/
tweet
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
#! /usr/bin/perl
# This is horrid and ancient but i haven't rewritten it yet. it might still work.
# eric.
use Net::Twitter::Lite::WithAPIv1_1;
use Date::Parse;
use Text::Wrap;
$Text::Wrap::columns = 72;
if ( $#ARGV != -1) {
print "Usage: tweet\nprovide message on stdin\n";
exit;
}
$dm_to = $ARGV[0];
shift @ARGV;
my $message = "";
# print "Enter message: \n";
while(<>) {
chomp;
$twitter_message .= "$_ ";
}
@days = ("Sun", "Mon", "Tue", "Wed" , "Thu", "Fri", "Sat");
eval {
#(my $message) = @_;
my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
legacy_lists_api => 0,
source => 'API',
apiurl => 'https://api.twitter.com/1.1' ,
consumer_key => "MY_TWITTER_CONSUMER_KEY",
consumer_secret => "MY_TWITTER_CONSUMER_SECRET",
ssl => 1
);
my($access_token, $access_token_secret) = restore_tokens();
if ($access_token && $access_token_secret) {
$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);
}
unless ( $nt->authorized ) {
# The client is not yet authorized: Do it now
print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN#\n";
# this needs to get done via TTY interface eventually :(
my $pin = <STDIN>; # wait for input
chomp $pin;
my($access_token, $access_token_secret, $user_id, $screen_name) =
$nt->request_access_token(verifier => $pin);
save_twitter_oauth_tokens($access_token, $access_token_secret); # if necessary
}
$twitter_message =~ s/nnnn//;
$twitter_message =~ s/NNNN//;
$twitter_message =~ s/\n/ /g; # turn newlines into spaces because you have to
# type them on the tty since it does not wrap by itself
$nt->update($twitter_message);
};
if ($@) {
print "Error: $@\n";
} else {
print "Posted.\n";
}
sub save_twitter_oauth_tokens {
my ($access_token, $access_token_secret) = @_;
if (open(FUM, "> /opt/ttycommands/twitter_tokens")) {
print FUM "$access_token\n";
print FUM "$access_token_secret\n";
}
}
sub restore_tokens {
if (open(FUM, "< /opt/ttycommands/twitter_tokens")) {
$access_token = <FUM>;
$access_token_secret = <FUM>;
chomp $access_token;
chomp $access_token_secret;
}
return($access_token, $access_token_secret);
}