| 1 | package Bot::BasicBot::Pluggable::Module::TwitterWatch; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Bot::BasicBot::Pluggable::Module'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 1 | ▶ | use Net::Twitter::Lite; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | Bot::BasicBot::Pluggable::Module::TwitterWatch - report new tweets matching given search patterns | ||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | Watches Twitter for tweets matching given search patterns, and reports them. | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | Load the module as with any other Bot::BasicBot::Pluggable module, then tell it | ||||||||||||||||||||||
| 22 | what to monitor - use these commands within a channel: | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | !twitterwatch search term here | ||||||||||||||||||||||
| 25 | !twitterunwatch search term here | ||||||||||||||||||||||
| 26 | !twittersearches | ||||||||||||||||||||||
| 27 | !twitterignore username | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | Each channel has its own just of searches stored. | ||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 0 | ▶ | sub said { | ||||||||||||||||||||
|
Subroutine
said: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||
| 34 | 0 | my ($self, $mess, $pri) = @_; | |||||||||||||||||||||
| 35 | 0 | ▶ | return unless $pri == 2; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 0 | my $message; | |||||||||||||||||||||
| 38 | 0 | ▶ | if (my($command, $params) = $mess->{body} =~ /^!(twitter\w+) (.+)/i) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 39 | # We need to know what channel we're talking about, so if this was a | ||||||||||||||||||||||
| 40 | # direct message, complain: | ||||||||||||||||||||||
| 41 | 0 | ▶ | if ($mess->{channel} eq 'msg') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 42 | 0 | return "Use the command in which the search results should appear"; | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | 0 | $params = lc $params; | |||||||||||||||||||||
| 46 | 0 | $params =~ s/\s+$//; | |||||||||||||||||||||
| 47 | 0 | ▶ | my $searches = $self->get('twitter_searches') || {}; | ||||||||||||||||||||
|
Condition0%0/2
$self->get('twitter_searches') || {}
MC/DC0%0/1
$self->get('twitter_searches') || {}
$self->get('twitter_searches')
Truth table0%0/2
A
A= $self->get('twitter_searches')
| |||||||||||||||||||||||
| 48 | 0 | ▶ | $searches->{ lc $mess->{channel} } ||= {}; | ||||||||||||||||||||
|
Condition0%0/2
$$searches{lc $mess->{"channel"}} ||= {}
MC/DC0%0/1
$$searches{lc $mess->{"channel"}} ||= {}
$$searches{lc $mess->{"channel"}}
Truth table0%0/2
A
A= $$searches{lc $mess->{"channel"}}
| |||||||||||||||||||||||
| 49 | 0 | my $chansearches = $searches->{ lc $mess->{channel} }; | |||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | 0 | ▶ | if (lc $command eq 'twitterwatch') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 52 | 0 | $chansearches->{$params} = 0; | |||||||||||||||||||||
| 53 | 0 | $message = "OK, now watching for '$params'"; | |||||||||||||||||||||
| 54 | } elsif (lc $command eq 'twitterunwatch') { | ||||||||||||||||||||||
| 55 | 0 | ▶ | if (exists $chansearches->{$params}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | 0 | delete $chansearches->{$params}; | |||||||||||||||||||||
| 57 | 0 | $message = "OK, no longer watching for '$params'"; | |||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||
| 59 | 0 | $message = "I wasn't watching for '$params'."; | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | } elsif (lc $command eq 'twittersearches') { | ||||||||||||||||||||||
| 62 | $message = "Currently watching for: " | ||||||||||||||||||||||
| 63 | 0 | . join ',', map { qq["$_"] } keys %$chansearches; | |||||||||||||||||||||
| 64 | } elsif (lc $command eq 'twitterignore') { | ||||||||||||||||||||||
| 65 | 0 | ▶ | my $ignore = $self->get('twitter_ignore') || {}; | ||||||||||||||||||||
|
Condition0%0/2
$self->get('twitter_ignore') || {}
MC/DC0%0/1
$self->get('twitter_ignore') || {}
$self->get('twitter_ignore')
Truth table0%0/2
A
A= $self->get('twitter_ignore')
| |||||||||||||||||||||||
| 66 | 0 | $ignore->{$params}++; | |||||||||||||||||||||
| 67 | 0 | $message = "OK, ignoring tweets from '$params'"; | |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 0 | $self->set('twitter_searches', $searches); | |||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | 0 | return $message; | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | # Tick is called automatically every 5 seconds | ||||||||||||||||||||||
| 78 | 0 | ▶ | sub tick { | ||||||||||||||||||||
|
Subroutine
tick: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||
| 80 | 0 | ▶ | my $seconds_between_checks = $self->get('twitter_search_wait') || 60 * 3; | ||||||||||||||||||||
|
Condition0%0/2
$self->get('twitter_search_wait') || 180
MC/DC0%0/1
$self->get('twitter_search_wait') || 180
$self->get('twitter_search_wait')
Truth table0%0/2
A
A= $self->get('twitter_search_wait')
| |||||||||||||||||||||||
| 81 | 0 | ▶ | return if time - $self->get('twitter_last_searched') | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 82 | < $seconds_between_checks; | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | # OK, time to do the searches: | ||||||||||||||||||||||
| 85 | 0 | my $twitter = Net::Twitter::Lite->new; | |||||||||||||||||||||
| 86 | 0 | ▶ | my $searches = $self->get('twitter_searches') || {}; | ||||||||||||||||||||
|
Condition0%0/2
$self->get('twitter_searches') || {}
MC/DC0%0/1
$self->get('twitter_searches') || {}
$self->get('twitter_searches')
Truth table0%0/2
A
A= $self->get('twitter_searches')
| |||||||||||||||||||||||
| 87 | 0 | ▶ | my $ignore = $self->get('twitter_ignore') || {}; | ||||||||||||||||||||
|
Condition0%0/2
$self->get('twitter_ignore') || {}
MC/DC0%0/1
$self->get('twitter_ignore') || {}
$self->get('twitter_ignore')
Truth table0%0/2
A
A= $self->get('twitter_ignore')
| |||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 0 | for my $channel (keys %$searches) { | |||||||||||||||||||||
| 90 | 0 | my %results; | |||||||||||||||||||||
| 91 | 0 | for my $searchterm (keys %{ $searches->{$channel} }) { | |||||||||||||||||||||
| 92 | 0 | ▶ | my $last_id = $searches->{$channel}{$searchterm} || 0; | ||||||||||||||||||||
|
Condition0%0/2
$searches->{$channel}{$searchterm} || 0
MC/DC0%0/1
$searches->{$channel}{$searchterm} || 0
$searches->{$channel}{$searchterm}
Truth table0%0/2
A
A= $searches->{$channel}{$searchterm}
| |||||||||||||||||||||||
| 93 | 0 | warn "Searching for '$searchterm' after $last_id on behalf of $channel"; | |||||||||||||||||||||
| 94 | 0 | ▶ | my $results = $twitter->search({ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 95 | q => $searchterm, | ||||||||||||||||||||||
| 96 | since_id => $last_id, | ||||||||||||||||||||||
| 97 | }) or return; | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | # Only process the results if we had a previous max ID; if not, this | ||||||||||||||||||||||
| 100 | # must be a newly-added search term, so don't spam the channel with | ||||||||||||||||||||||
| 101 | # all the initial matches, just find new ones from now on | ||||||||||||||||||||||
| 102 | 0 | ▶ | if ($last_id) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 103 | 0 | my %tweets_from_user; | |||||||||||||||||||||
| 104 | 0 | for my $result ( | |||||||||||||||||||||
| 105 | 0 | grep { $_->{id} > $last_id } @{ $results->{results} } | |||||||||||||||||||||
| 106 | ) { | ||||||||||||||||||||||
| 107 | 0 | ▶ | if ($ignore->{lc $result->{from_user}}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 108 | warn "Ignoring tweet from $result->{from_user} :" | ||||||||||||||||||||||
| 109 | 0 | . $result->{text}; | |||||||||||||||||||||
| 110 | 0 | return; | |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | # Retweets can be a bit spammy at times, so skip them: | ||||||||||||||||||||||
| 114 | 0 | ▶ | next if $result->{text} =~ /^RT/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | 0 | ▶ | next if ++$tweets_from_user{$result->{from_user}} > 3; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | # See whether this is a newly-created spam account: | ||||||||||||||||||||||
| 119 | my $user_details = $twitter->lookup_users( | ||||||||||||||||||||||
| 120 | { screen_name => $result->{from_user} } | ||||||||||||||||||||||
| 121 | 0 | ); | |||||||||||||||||||||
| 122 | 0 | ▶ | $user_details = $user_details->[0] | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 123 | or next; | ||||||||||||||||||||||
| 124 | 0 | ▶ | if ($user_details->{statuses_count} < 40) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 125 | 0 | warn "Ignoring new spam account $result->{from_user}"; | |||||||||||||||||||||
| 126 | 0 | next; | |||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | # Results are stored in a hash keyed on ID, so tweets that | ||||||||||||||||||||||
| 130 | # match more than one search only appear once | ||||||||||||||||||||||
| 131 | 0 | $results{ $result->{id} } = $result; | |||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | # Remember the ID of the highest match for this search, so we know | ||||||||||||||||||||||
| 137 | # where to start from next time | ||||||||||||||||||||||
| 138 | 0 | $searches->{$channel}{$searchterm} = $results->{max_id}; | |||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | # Right, now go through the results for the searches for this channel | ||||||||||||||||||||||
| 143 | # and assemble the messages we're going to send. | ||||||||||||||||||||||
| 144 | 0 | my @results; | |||||||||||||||||||||
| 145 | 0 | for my $tweetid (reverse sort keys %results) { | |||||||||||||||||||||
| 146 | 0 | my $result = $results{$tweetid}; | |||||||||||||||||||||
| 147 | push @results, sprintf 'Twitter: @%s: "%s"', | ||||||||||||||||||||||
| 148 | $result->{from_user}, | ||||||||||||||||||||||
| 149 | 0 | HTML::Entities::decode_entities($result->{text}); | |||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||
| 151 | |||||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | # TODO: probably check if we found too many results to sensibly relay | ||||||||||||||||||||||
| 155 | 0 | for my $result (@results) { | |||||||||||||||||||||
| 156 | 0 | $self->say(channel => $channel, body => $result); | |||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | 0 | $self->set('twitter_last_searched', time); | |||||||||||||||||||||
| 161 | 0 | $self->set('twitter_searches', $searches); | |||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | =head1 AUTHOR | ||||||||||||||||||||||
| 168 | |||||||||||||||||||||||
| 169 | David Precious, C<< <davidp at preshweb.co.uk> >> | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | =head1 BUGS | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | Please report any bugs or feature requests to C<bug-bot-basicbot-pluggable-module-twitterwatch at rt.cpan.org>, or through | ||||||||||||||||||||||
| 174 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bot-BasicBot-Pluggable-Module-TwitterWatch>. I will be notified, and then you'll | ||||||||||||||||||||||
| 175 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||
| 176 | |||||||||||||||||||||||
| 177 | |||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | |||||||||||||||||||||||
| 180 | =head1 SUPPORT | ||||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | perldoc Bot::BasicBot::Pluggable::Module::TwitterWatch | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | |||||||||||||||||||||||
| 187 | You can also look for information at: | ||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | =over 4 | ||||||||||||||||||||||
| 190 | |||||||||||||||||||||||
| 191 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||
| 192 | |||||||||||||||||||||||
| 193 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bot-BasicBot-Pluggable-Module-TwitterWatch> | ||||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||
| 196 | |||||||||||||||||||||||
| 197 | L<http://annocpan.org/dist/Bot-BasicBot-Pluggable-Module-TwitterWatch> | ||||||||||||||||||||||
| 198 | |||||||||||||||||||||||
| 199 | =item * CPAN Ratings | ||||||||||||||||||||||
| 200 | |||||||||||||||||||||||
| 201 | L<http://cpanratings.perl.org/d/Bot-BasicBot-Pluggable-Module-TwitterWatch> | ||||||||||||||||||||||
| 202 | |||||||||||||||||||||||
| 203 | =item * Search CPAN | ||||||||||||||||||||||
| 204 | |||||||||||||||||||||||
| 205 | L<http://search.cpan.org/dist/Bot-BasicBot-Pluggable-Module-TwitterWatch/> | ||||||||||||||||||||||
| 206 | |||||||||||||||||||||||
| 207 | =back | ||||||||||||||||||||||
| 208 | |||||||||||||||||||||||
| 209 | |||||||||||||||||||||||
| 210 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||
| 211 | |||||||||||||||||||||||
| 212 | |||||||||||||||||||||||
| 213 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||
| 214 | |||||||||||||||||||||||
| 215 | Copyright 2011 David Precious. | ||||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||
| 218 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||
| 219 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||
| 222 | |||||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||
| 225 | |||||||||||||||||||||||
| 226 | 1 | 1; # End of Bot::BasicBot::Pluggable::Module::TwitterWatch | |||||||||||||||||||||