-
Notifications
You must be signed in to change notification settings - Fork 1
/
tweets_search_bot.pl
212 lines (161 loc) · 5.01 KB
/
tweets_search_bot.pl
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
## Very simple Twitter-IRC Bot which posts search results to IRC channel
## Goes offline when it doesnt find anything new after a while.
## It saves the last posted tweed id, so it resume the search.
##
## Sorry for that unproper code and dirty style.. it was more like a prove of concept .. which runs :D
##
### Written by Matthias Strubel (c) 2013 [email protected]
## Licenced with GPL-3
use Net::Twitter;
use Scalar::Util 'blessed';
use Storable ;
use HTML::Entities ();
package Bot;
use base qw(Bot::BasicBot);
use warnings;
use strict;
use Data::Dumper;
my $last_search_id = {};
my $nt = Net::Twitter->new( traits => [qw/API::RESTv1_1/],
#Get these data from dev.twitter.com
consumer_key => 'x',
consumer_secret => 'x',
access_token => 'x',
access_token_secret => 'x',
ssl => 1,
);
my $MAX_TWEEDS_PER_SEARCH = 5 ;
my $TICK_COUNT = 30 ; # tick every 30 seconds
my $MAX_EMPTY_CNT = 10 ; # How much empty searches before disconnecting again
my $CACHE_FILE = "tweed_bot.cache";
my $EXCLUDE_RETWEETS = 1 ;
my $CHANNEL= "#piratebox" ;
my $CHANNEL_ARR = [ "$CHANNEL" ];
#don't connect to irc
my $DRY_RUN_ONLY = 0 ;
#----- IRC settings
my $IRC_SERVER = "irc.freenode.net" ;
my $IRC_SERVER_PORT = "6667" ;
my $IRC_NICK = "PirateBox-Bot";
my $IRC_ALT_NICK = "PirateBox-Bot2";
my $Twitter_Search_String = "Piratebox";
my $bot ;
my $bot_run = 0;
my $bot_wait_count = 0;
my $cnt_sent_tweeds = 0 ;
my $curr_empty = $MAX_EMPTY_CNT ;
sub save_max_id ( $$ ) {
my ( $string , $id ) = @_ ;
if ( not $last_search_id->{$string} ) {
$last_search_id->{$string} = $id;
} else {
if ( $last_search_id->{$string} < $id ) {
$last_search_id->{$string} = $id;
}
}
}
sub perform_search ($$) {
my $string = shift;
my $update_cache = shift;
my $r = "";
my $res_found = 0;
my $api_string = $string ;
if ( $EXCLUDE_RETWEETS ) {
print "Ignoring retweeds";
$api_string .= "+exclude:retweets";
}
print "Starting twitter search...\n";
if ( not $last_search_id->{$string} ) {
print "Initial search with >$string< \n";
# $r = $nt->search( { q=>"$string" } );
$r = $nt->search( { q=>"$api_string" , count=>"$MAX_TWEEDS_PER_SEARCH" } );
} else {
#sinceID
print "Follow up search with >$string< using ".$last_search_id->{$string} ."\n";
$r = $nt->search( { q=>"$api_string" ,
, count=>"$MAX_TWEEDS_PER_SEARCH" ,
since_id=>$last_search_id->{$string} } );
}
if ( $r ) {
# print Dumper $r ;
foreach my $result ( @{$r->{statuses}} ) {
$result->{'cleaned_text'} = HTML::Entities::decode_entities ( $result->{'text'});
$result->{'cleaned_text'} =~ s/\R/ /g;
my $msg = $result->{'user'}->{'name'} ." : ". $result->{'cleaned_text'} ." -
https://twitter.com/".$result->{'user'}->{'screen_name'}."/status/".$result->{'id'} ."\n" ;
print $msg ;
if ( $bot_run ) {
bot_send_tweet ( $msg );
sleep (1);
}
if ( $update_cache ) {
save_max_id( $string , $result->{'id'} );
} else {
print "Cache skipped..\n";
}
$res_found++;
}
} else {
#Error?
print Dumper $nt->get_error;
}
return $res_found;
}
sub quit_message {
my $msg = "Bye.. Served you $cnt_sent_tweeds tweeds from twitter search >$Twitter_Search_String<" ;
return $msg;
}
sub bot_start () {
print "Starting bot against $IRC_SERVER:$IRC_SERVER_PORT \n";
$bot = Bot->new(
server => $IRC_SERVER ,
port => $IRC_SERVER_PORT ,
channels => $CHANNEL_ARR ,
nick => $IRC_NICK ,
alt_nick => $IRC_ALT_NICK ,
);
$bot_run = 1;
$bot->run();
}
sub bot_send_tweet {
my $message = shift;
$bot->say ( "channel" => $CHANNEL , body => "$message" );
$cnt_sent_tweeds++;
}
sub tick {
my $self = shift;
my $cnt = perform_search ( $Twitter_Search_String , 1);
if ( $cnt == 0 ) {
if ( $curr_empty-- <= 0 ) {
print "Shutdown bot because there where no new tweeds anymore\n";
my $msg = $bot->quit_message() ;
$bot->shutdown( );
}
} else {
$curr_empty = $MAX_EMPTY_CNT ;
}
return $TICK_COUNT ;
}
#-----------------
#if file exists
if ( -e $CACHE_FILE ) {
print "Cache file $CACHE_FILE exists, loading..";
open ( my $fh , "< $CACHE_FILE " ) or die "can't open cache";
eval { $last_search_id = Storable::fd_retrieve ( $fh ); } ;
close ( $fh );
print "done \n";
} else {
print "No cache file found..\n";
}
#Check if if have anything new
if ( perform_search("Piratebox" , 0 ) > 0 ) {
if ( $DRY_RUN_ONLY ) {
exit ;
}
eval { bot_start; } ;
}
print "Writing cache file $CACHE_FILE .. ";
open ( my $output_fh, "> $CACHE_FILE ") or die "can't open cache for output" ;
Storable::store_fd ( $last_search_id , $output_fh ) || die "can't store cache \n";
close ( $output_fh );
print "done \n";