1#!perl -w
2
3
4use strict;
5use lib qw(lib);
6use JSON::Any;
7
8my $CONFIG = ".twitter_config";
9
10
11binmode STDOUT, ":utf8";
12
13
14# Get the tokens from the command line, a config file or wherever
15my %tokens  = get_tokens();
16my $app     = Twitter->new(%tokens);
17
18# Check to see we have a consumer key and secret
19unless ($app->consumer_key && $app->consumer_secret) {
20    die "You must go get a consumer key and secret from App\n";
21}
22
23# If the app is authorized (i.e has an access token and secret)
24# Then look at a restricted resourse
25get_dms($app) if $app->authorized;
26
27# Otherwise the user needs to go get an access token and secret
28print "Enter your username:\n";
29chomp(my $username = <STDIN>);
30print "Enter your password:\n";
31chomp(my $password = <STDIN>);
32
33my ($access_token, $access_token_secret) = $app->xauth_request_access_token( x_auth_username => $username, x_auth_password => $password, x_auth_mode => 'client_auth' );
34
35print "You have now authorized this app.\n";
36print "Your access token and secret are:\n\n";
37print "access_token=$access_token\n";
38print "access_token_secret=$access_token_secret\n";
39print "\n";
40if (-f $CONFIG) {
41    save_tokens($app);
42    print "You should note these down but they have also been saved in $CONFIG\n\n";
43} else {
44    print "You should note these down or put them in $CONFIG with your consumer key and secret\n\n";
45}
46
47get_dms($app);
48
49
50sub get_dms {
51	my $dms = JSON::Any->jsonToObj($app->get_dms);
52	foreach my $dm (@$dms) {
53		chomp(my $sender = $dm->{sender}->{screen_name});
54		chomp(my $text   = $dm->{text});
55		print "--\n$sender said \"$text\"\n";
56	}
57	exit(0);
58}
59
60
61sub get_tokens {
62    my %tokens = Twitter->load_tokens($CONFIG);
63    while (@ARGV && $ARGV[0] =~ m!^(\w+)\=(\w+)$!) {
64        $tokens{$1} = $2;
65        shift @ARGV;
66    }
67    return %tokens;
68}
69
70sub save_tokens {
71    my $app     = shift;
72    my %tokens = $app->tokens;
73    Twitter->save_tokens($CONFIG, %tokens);
74}
75
76
77package Twitter;
78
79use strict;
80use base qw(Net::OAuth::Simple);
81
82
83sub new {
84    my $class  = shift;
85    my %tokens = @_;
86    return $class->SUPER::new( tokens => \%tokens,
87                               protocol_version => '1.0a',
88                               urls   => {
89                                    authorization_url => 'http://api.twitter.com/oauth/authorize',
90                                    request_token_url => 'http://api.twitter.com/oauth/request_token',
91                                    access_token_url  => 'http://api.twitter.com/oauth/access_token',
92                               });
93}
94
95sub get_dms {
96	my $self = shift;
97	my $form = shift || "json";
98	my $url  = "http://api.twitter.com/1/direct_messages.${form}";
99	return $self->_make_restricted_request($url, 'GET');
100}
101
102sub _make_restricted_request {
103    my $self     = shift;
104    my $response = $self->make_restricted_request(@_);
105    return $response->content;
106}
107
108
1091;
110