Skip to content

Commit

Permalink
Various list management utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Aug 17, 2014
1 parent 5e66a2b commit 48b42ec
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 34 deletions.
60 changes: 60 additions & 0 deletions bin/create_lists
@@ -0,0 +1,60 @@
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use lib 'lib';

use TwittElection::Twitter;
use TwittElection::Schema;

my $sch = TwittElection::Schema->connect(
"dbi:mysql:$ENV{TE_DB}", $ENV{TE_USER}, $ENV{TE_PASS},
) or die;

my $con_rs = $sch->resultset('Constituency');

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

$t->authorise;

foreach my $con ($con_rs->all) {
next unless $con->candidates->count;
say $con->name;

eval {
if ($con->list_id) {
say 'Delete ', $con->list_name;
$t->delete_list({
list_id => $con->list_id,
});
}
};
say 'Create ', $con->name;
my $list = $t->create_list({
owner_screen_name => 'twittelection',
name => $con->name,
});
say "Created $list->{slug} ($list->{id})";
sleep 2;

$con->update({
list_name => $list->{slug},
list_id => $list->{id},
});

foreach my $cand ($con->candidates) {
next unless $cand->twitter;
say ' -> ', $cand->name;
$t->add_list_member({
list_id => $list->{id},
screen_name => $cand->twitter,
});
sleep 2;
}
}
21 changes: 21 additions & 0 deletions bin/delete_list
@@ -0,0 +1,21 @@
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use lib 'lib';

use TwittElection::Twitter;

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

$t->authorise;

$t->delete_list({
list_id => +shift,
});
22 changes: 22 additions & 0 deletions bin/delete_lists
@@ -0,0 +1,22 @@
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use lib 'lib';

use TwittElection::Twitter;

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

$t->authorise;

foreach my $list (@{$t->list_ownerships->{lists}}) {
say $list->{name}, ' (', $list->{id}, ')';
$t->delete_list({ list_id => $list->{id} });
}
39 changes: 5 additions & 34 deletions bin/list_lists
Expand Up @@ -3,51 +3,22 @@
use strict;
use warnings;
use 5.010;
use lib 'lib';

use Net::Twitter;
use TwittElection::Twitter;

my $t = Net::Twitter->new(
my $t = TwittElection::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
}
$t->authorise;

foreach my $list (@{$t->list_ownerships->{lists}}) {
say $list->{name};
say $list->{name}, ' (', $list->{id}, ')';
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 48b42ec

Please sign in to comment.