/
create_lists
executable file
·163 lines (136 loc) · 3.96 KB
/
create_lists
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
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use lib 'lib';
use DateTime;
use Scalar::Util qw[blessed];
use Log::Log4perl qw[:easy];
use TwittElection::Twitter;
use TwittElection::Schema;
Log::Log4perl->easy_init($INFO);
my $logger = Log::Log4perl->get_logger;
my $sch = TwittElection::Schema->get_schema;
my $con_rs;
my $delay = 1;
if (@ARGV) {
$con_rs = $sch->resultset('Constituency')->search({
mapit_id => \@ARGV,
});
$delay = 0;
} else {
$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;
# Here's how this should work.
#
# IF constit has a list id and that list exists
# Delete all members
# ELSE
# Create new list
# Update list name and list id
# END
#
# Insert list members
# Twitter rate limits (https://dev.twitter.com/rest/public/rate-limits)
# show_list : 15 / 15 min
# list_members : 15 / 15 min
# remove_list_members :
# create_list :
# add_list_member :
# Some thoughts about a more intelligent approach.
# Add a 'list_updated_timestamp' column to constituency table.
# Only attempt to rebuild list if
# candidates_updated timestamp > list_updated_timestamp
# Get current list members and current candidate list.
# Only attempt to rebuild Twitter list if these lists differ.
# Update list_updated_timestamp when we update Twitter list.
#
# Also, need to think about Twitter rate limiting.
# Stop working as soon as we get a rate exceeded response.
# Only attempt to rebuild lists that haven't been rebuilt in the last 24 hours.
foreach my $con ($con_rs->all) {
$logger->info($con->name . ' (' . $con->mapit_id . ')');
unless ($con->candidates->count) {
$logger->info('No candidates found');
next;
}
if ($con->candidates_updated_time <= $con->list_rebuilt_time) {
$logger->info('No need to rebuild list');
next;
}
if ($delay and $con->list_rebuilt_time > DateTime->now->subtract(days=>1)) {
$logger->info('List rebuilt too recently');
next;
}
my $list;
eval {
if ($con->list_id) {
$list = $t->show_list({
owner_screen_name => 'twittelection',
slug => $con->list_name,
});
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 {
$logger->info('Create ' . $con->name);
$list = $t->create_list({
owner_screen_name => 'twittelection',
name => $con->list_name,
});
$logger->info($list->{slug});
$con->update({
list_name => $list->{slug},
list_id => $list->{id},
});
}
} else {
$logger->info('Create ' . $con->name);
$list = $t->create_list({
owner_screen_name => 'twittelection',
description => $con->name,
name => $con->list_name,
});
$logger->info($list->{slug});
# Remove the number that Twitter will sometimes add at the end
$list->{slug} =~ s/-\d+$//;
$con->update({
list_name => $list->{slug},
list_id => $list->{id},
});
}
foreach my $cand ($con->candidates) {
next unless $cand->twitter;
$logger->info("$list->{id} -> " . $cand->name . ' / ' . $cand->twitter);
$t->add_list_member({
list_id => $list->{id},
screen_name => $cand->twitter,
});
}
$con->update({
list_rebuilt_time => DateTime->now,
});
};
if ($@) {
my $err = $@;
$logger->logdie($err)
unless blessed $err and $err->isa('Net::Twitter::Error');
$logger->logwarn($err->code . ': ' . $err->error);
if ($err->code == 429 or $err->code == 403) {
$logger->logdie('Rate limit exceeded');
} else {
next;
}
}
}