Skip to content

Commit

Permalink
Test twitter program to list lists
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Aug 17, 2014
1 parent 846c6d0 commit 4285350
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions bin/list_lists
@@ -0,0 +1,53 @@
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Net::Twitter;

my $t = Net::Twitter->new(
traits => [ 'API::RESTv1_1', 'OAuth' ],
ssl => 1,
consumer_key => $ENV{TE_TW_API_KEY},
consumer_secret => $ENV{TE_TW_API_SEC},
);

my($access_token, $access_token_secret) = restore_tokens();
if ($access_token && $access_token_secret) {
$t->access_token($access_token);
$t->access_token_secret($access_token_secret);
}

unless ( $t->authorized ) {
# The client is not yet authorized: Do it now
print "Authorize this app at ", $t->get_authorization_url, " and enter the PIN#\n";

my $pin = <STDIN>; # wait for input
chomp $pin;

my($access_token, $access_token_secret, $user_id, $screen_name) = $t->request_access_token(verifier => $pin);
save_tokens($access_token, $access_token_secret); # if necessary
}

foreach my $list (@{$t->list_ownerships->{lists}}) {
say $list->{name};
foreach my $mem (@{$t->list_members({ list_id => $list->{id} })->{users}}) {
say "* \@$mem->{screen_name}";
}
}

sub save_tokens {
my ($access_token, $access_token_secret) = @_;

open my $tw_fh, '>', '.te_tokens' or die $!;
print $tw_fh "$access_token $access_token_secret";
}

sub restore_tokens {
open my $tw_fh, '<', '.te_tokens' or die $!;

my $tokens = <$tw_fh>;

return split / /, $tokens;
}

0 comments on commit 4285350

Please sign in to comment.