/
Twitter.pm
302 lines (207 loc) · 6.55 KB
/
Twitter.pm
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package App::Maisha::Plugin::Twitter;
use strict;
use warnings;
our $VERSION = '0.15';
#----------------------------------------------------------------------------
# Library Modules
use base qw(App::Maisha::Plugin::Base);
use base qw(Class::Accessor::Fast);
use File::Path;
use Net::Twitter;
use Storable;
#----------------------------------------------------------------------------
# Accessors
__PACKAGE__->mk_accessors($_) for qw(api users);
#----------------------------------------------------------------------------
# Public API
#Request token URL
#https://api.twitter.com/oauth/request_token
#Access token URL
#https://api.twitter.com/oauth/access_token
#Authorize URL
#https://api.twitter.com/oauth/authorize
# http://dev.twitter.com/apps/347040
sub new {
my $class = shift;
my $self = {
consumer_key => 'ifCuNOQXA5KTnKVXVcZg',
consumer_secret => 'OXyHE1PSgfy66gbCu3QshXgP9RNA1fOVLdqv4afPDug',
};
bless $self, $class;
return $self;
}
sub login {
my ($self,$config) = @_;
unless($config->{username}) { warn "No username supplied\n"; return }
my $api = Net::Twitter->new(
# traits => [qw/API::REST OAuth WrapError/],
traits => [qw/API::REST OAuth/],
consumer_key => $self->{consumer_key},
consumer_secret => $self->{consumer_secret},
ssl => 1
);
unless($api) {
warn "Unable to establish connection to Twitter API\n";
return 0;
}
# for testing purposes we don't want to login
if(!$config->{test}) {
my $datafile = $config->{home} . '/.maisha/twitter.dat';
my $access_tokens = eval { retrieve($datafile) } || {};
if ( $access_tokens && $access_tokens->{$config->{username}}) {
$api->access_token($access_tokens->{$config->{username}}->[0]);
$api->access_token_secret($access_tokens->{$config->{username}}->[1]);
}
else {
my $auth_url = $api->get_authorization_url;
print " Authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
my $pin = <STDIN>; # wait for input
chomp $pin;
# request_access_token stores the tokens in $nt AND returns them
my @access_tokens = $api->request_access_token(verifier => $pin);
$access_tokens->{$config->{username}} = \@access_tokens;
mkpath( $config->{home} . '/.maisha' );
# save the access tokens
store $access_tokens, $datafile;
}
}
$self->api($api);
if(!$config->{test}) {
print "...building user cache for Twitter\n";
$self->_build_users();
}
return 1;
}
sub _build_users {
my $self = shift;
my %users;
my $f = $self->api->friends();
if($f && @$f) { for(@$f) { next unless($_); $users{$_->{screen_name}} = 1 } }
$f = $self->api->followers();
if($f && @$f) { for(@$f) { next unless($_); $users{$_->{screen_name}} = 1 } }
$self->users(\%users);
}
sub api_update {
my $self = shift;
$self->api->update(@_);
}
sub api_user {
my $self = shift;
$self->api->show_user(@_);
}
sub api_user_timeline {
my $self = shift;
$self->api->user_timeline(@_);
}
sub api_friends {
my $self = shift;
$self->api->friends();
}
sub api_friends_timeline {
my $self = shift;
$self->api->friends_timeline();
}
sub api_public_timeline {
my $self = shift;
$self->api->public_timeline();
}
sub api_followers {
my $self = shift;
$self->api->followers();
}
sub api_replies {
my $self = shift;
$self->api->replies();
}
sub api_send_message {
my $self = shift;
$self->api->new_direct_message(@_);
}
sub api_direct_messages_to {
my $self = shift;
$self->api->direct_messages();
}
sub api_direct_messages_from {
my $self = shift;
$self->api->sent_direct_messages();
}
sub api_follow {
my $self = shift;
$self->api->create_friend(@_);
}
sub api_unfollow {
my $self = shift;
$self->api->destroy_friend(@_);
}
1;
__END__
=head1 NAME
App::Maisha::Plugin::Twitter - Maisha interface to Twitter
=head1 SYNOPSIS
maisha
maisha> use Twitter
use ok
=head1 DESCRIPTION
App::Maisha::Plugin::Twitter is the gateway for Maisha to access the Twitter
API.
=head1 METHODS
=head2 Constructor
=over 4
=item * new
=back
=head2 Process Methods
=over 4
=item * login
Login to the service. See Authentication below.
=back
=head2 API Methods
The API methods are used to interface to with the Twitter API.
=over 4
=item * api_follow
=item * api_unfollow
=item * api_user
=item * api_user_timeline
=item * api_friends
=item * api_friends_timeline
=item * api_public_timeline
=item * api_followers
=item * api_update
=item * api_replies
=item * api_send_message
=item * api_direct_messages_to
=item * api_direct_messages_from
=back
=head1 AUTHENTICATION
On 31st August 2010, Twitter disabled Basic Authentication to access their API.
Instead they have introduce the OAuth method of authrntication, which now
requires application developers to request the user to authenticate themselves
and provide a PIN (Personal Identification Number) to allow the application to
retrieve access tokens.
With this new method of authentication, the application will provide a URL,
which the user needs to cut-n-paste into a browser to logging in to the
service, using your regular username/password, then 'Allow' Maisha to access
your account. This will then allow Maisha to post to your account. You will
then be given a PIN, which should then be entered at the prompt on the Maisha
command line.
Once you have completed authentication, the application will then store your
access tokens permanently under your profile on your computer. Then when you
next use the application it will retrieve these access tokens automatically and
you will no longer need to register the application.
=head1 SEE ALSO
For further information regarding the commands and configuration, please see
the 'maisha' script included with this distribution.
L<App::Maisha>
L<Net::Twitter>
=head1 WEBSITES
=over 4
=item * Main Site: L<http://maisha.grango.org>
=item * Git Repo: L<http://github.com/barbie/maisha/tree/master>
=item * RT Queue: L<RT: http://rt.cpan.org/Public/Dist/Display.html?Name=App-Maisha>
=back
=head1 AUTHOR
Copyright (c) 2009-2010 Barbie <barbie@cpan.org> for Grango.org.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
=cut