forked from ryhmrt/bassy-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterBot.pm
155 lines (131 loc) · 2.93 KB
/
TwitterBot.pm
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
package TwitterBot;
use strict;
use warnings;
use Moose;
use AnyEvent::Twitter::Stream;
use TwitterUtil;
use PullTimer;
use Log::Log4perl;
has 'username' => (isa => 'Str', is => 'ro', required => 1);
has 'password' => (isa => 'Str', is => 'ro', required => 1);
has 'ssl' => (isa => 'Bool', is => 'ro', default => 1);
has 'auto_update_friends_interval' => (isa => 'Int', is => 'ro', default => 60*60*24);
has 'util' => (
isa => 'TwitterUtil',
is => 'ro',
lazy => 1,
builder => '_build_twitter_util',
);
has 'timer' => (
isa => 'PullTimer',
is => 'ro',
builder => '_build_timer',
);
has 'actions' => (
isa => 'ArrayRef',
is => 'ro',
builder => '_build_actions',
);
has 'logger' => (
isa => 'Log::Log4perl::Logger',
is => 'ro',
lazy => 1,
builder => '_build_logger',
);
sub BUILD {
my $self = shift;
my $done = AnyEvent->condvar;
$self->{_CV} = $done;
my $stream = AnyEvent::Twitter::Stream->new(
username => $self->username,
password => $self->password,
method => 'filter',
follow => join(',', $self->following_ids),
on_tweet => sub {
my $tweet = shift;
$self->reaction($tweet);
},
on_error => sub {
my $error = shift;
warn "ERROR: $error";
$done->send;
},
on_eof => sub {
$done->send;
},
);
my $timer = AnyEvent->timer(
after => 10,
interval => 10,
cb => sub {
for my $sub ($self->timer->pull) {
$sub->($self);
}
},
);
my $friends_updater = AnyEvent->timer(
after => 0,
interval => $self->auto_update_friends_interval,
cb => sub {
$self->update_friends;
},
);
$self->{_EVENTS} = [];
push @{$self->{_EVENTS}}, $timer;
push @{$self->{_EVENTS}}, $stream;
push @{$self->{_EVENTS}}, $friends_updater;
}
sub _build_twitter_util {
my $self = shift;
return TwitterUtil->new(
username => $self->username,
password => $self->password,
ssl => $self->ssl,
);
}
sub _build_logger {
my $self = shift;
return Log::Log4perl->get_logger(ref $self);
}
sub _build_timer {
return PullTimer->new();
}
sub _build_actions {
die "override _build_actions method!";
}
sub start {
my $self = shift;
$self->{_CV}->recv;
}
sub following_ids {
my $self = shift;
return $self->util->user->{id}, @{$self->util->friends_ids};
}
sub not_following_followers_ids {
my $self = shift;
return grep {
my $id = $_;
! grep {$_ eq $id} @{$self->util->friends_ids}, @{$self->util->blocking_ids};
} @{$self->util->followers_ids};
}
sub update_friends {
my $self = shift;
$self->util->refresh_friends_ids;
$self->util->refresh_followers_ids;
$self->util->refresh_blocking_ids;
for my $friend_id ($self->not_following_followers_ids) {
my $user = $self->util->create_friend($friend_id);
$self->logger->info("new friend <$user->{screen_name}/$user->{id}> added.");
}
$self->util->refresh_friends_ids;
}
sub reaction {
my $self = shift;
my $tweet = shift;
for my $action (@{$self->actions}) {
last if $action->($self, $tweet);
}
}
no Moose;
__PACKAGE__->meta->make_immutable();
1;