From 27f90cde5f22817cb9c6014e9bcb74490587c37a Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Fri, 22 Aug 2014 21:01:58 +0100 Subject: [PATCH] Couple more list management utilities. Edited create_lists. --- bin/create_lists | 41 ++++++++++++++++++++++++----------------- bin/delete_excess_lists | 40 ++++++++++++++++++++++++++++++++++++++++ bin/set_list_ids | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 17 deletions(-) create mode 100755 bin/delete_excess_lists create mode 100755 bin/set_list_ids diff --git a/bin/create_lists b/bin/create_lists index f761b83dba..9833bfa751 100755 --- a/bin/create_lists +++ b/bin/create_lists @@ -37,27 +37,35 @@ $t->authorise; foreach my $con ($con_rs->all) { next unless $con->candidates->count; say $con->name; - + + my $list; eval { if ($con->list_id) { - say 'Delete ', $con->list_name; - $t->delete_list({ - list_id => $con->list_id, + $list = $t->show_list({ + owner_screen_name => 'twittelection', + list_id => $con->list_id, + }); + if ($list) { + foreach my $mem (@{$t->list_members({ list_id => $list->{id} })->{users}}) { + $t->remove_list_members({ + list_id => $list->{id}, + screen_name => $mem->{screen_name}, + }); + } + } + } else { + say 'Create ', $con->name; + $list = $t->create_list({ + owner_screen_name => 'twittelection', + name => $con->name, + }); + + $con->update({ + list_name => $list->{slug}, + list_id => $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; @@ -66,6 +74,5 @@ foreach my $con ($con_rs->all) { list_id => $list->{id}, screen_name => $cand->twitter, }); - sleep 2; } } diff --git a/bin/delete_excess_lists b/bin/delete_excess_lists new file mode 100755 index 0000000000..5321b5eac1 --- /dev/null +++ b/bin/delete_excess_lists @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.010; +use lib 'lib'; + +use TwittElection::Schema; +use TwittElection::Twitter; + +my $sch = TwittElection::Schema->connect( + "dbi:mysql:database=$ENV{TE_DB}", $ENV{TE_USER}, $ENV{TE_PASS}, + { mysql_enable_utf8 => 1 }, +) or die; + +my $const_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 $list (@{$t->list_ownerships->{lists}}) { + say $list->{name}, ' (', $list->{id}, ')'; + my $const = $const_rs->find({ + list_id => $list->{id}, + list_name => $list->{name}, + }); + + next if $const; + + say "Deleting..."; + $t->delete_list({ + list_id => $list->{id}, + }); +} diff --git a/bin/set_list_ids b/bin/set_list_ids new file mode 100755 index 0000000000..d70d100d2a --- /dev/null +++ b/bin/set_list_ids @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.010; +use lib 'lib'; + +use TwittElection::Schema; +use TwittElection::Twitter; + +my $sch = TwittElection::Schema->connect( + "dbi:mysql:database=$ENV{TE_DB}", $ENV{TE_USER}, $ENV{TE_PASS}, + { mysql_enable_utf8 => 1 }, +) or die; + +my $const_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 $list (@{$t->list_ownerships->{lists}}) { + say $list->{name}, ' (', $list->{id}, ')'; + my $const = $const_rs->find({ + list_name => $list->{name}, + }); + + $const->update({ list_id => $list->{id} }) if $const; +}