| 1 | package Twitter::TagGrep; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '1.0000'; | |||||||||
| 7 | |||||||||||
| 8 | 0 | ▶ | sub prefix { | ||||||||
|
Subroutine
prefix: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 9 | 0 | my $self = shift; | |||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | if (defined $_[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 12 | 0 | $self->{prefix} = $_[0]; | |||||||||
| 13 | 0 | undef $self->{tag_regex}; | |||||||||
| 14 | } | ||||||||||
| 15 | 0 | return $self->{prefix}; | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub add_prefix { | ||||||||
|
Subroutine
add_prefix: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 19 | 0 | my $self = shift; | |||||||||
| 20 | |||||||||||
| 21 | 0 | $self->{prefix} = join '', $self->{prefix}, @_; | |||||||||
| 22 | 0 | undef $self->{tag_regex}; | |||||||||
| 23 | 0 | return $self->{prefix}; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 0 | ▶ | sub tags { | ||||||||
|
Subroutine
tags: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 27 | 0 | my $self = shift; | |||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | ▶ | if (ref $_[0] eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | $self->{tags} = $_[0]; | |||||||||
| 32 | } else { | ||||||||||
| 33 | 0 | $self->{tags} = [ @_ ]; | |||||||||
| 34 | } | ||||||||||
| 35 | 0 | undef $self->{tag_regex}; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 0 | return @{$self->{tags}}; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub add_tag { | ||||||||
|
Subroutine
add_tag: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 42 | 0 | my $self = shift; | |||||||||
| 43 | |||||||||||
| 44 | 0 | for my $tag (@_) { | |||||||||
| 45 | 0 | ▶ | if (ref $tag eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | push @{$self->{tags}}, @$tag; | |||||||||
| 47 | } else { | ||||||||||
| 48 | 0 | push @{$self->{tags}}, $tag; | |||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | undef $self->{tag_regex}; | |||||||||
| 53 | 0 | return @{$self->{tags}}; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub grep_tags { | ||||||||
|
Subroutine
grep_tags: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 58 | 0 | my $self = shift; | |||||||||
| 59 | 0 | my $timeline = shift; | |||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | $self->_gen_tag_regex unless $self->{tag_regex}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | return reverse grep { | ||||||||||
| 64 | 0 | my @tags = $_->{text} =~ /$self->{tag_regex}/gi; | |||||||||
| 65 | 0 | ▶ | $_->{tags} = \@tags if @tags; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | } @$timeline; | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 71 | 0 | my ($class, %params) = @_; | |||||||||
| 72 | |||||||||||
| 73 | my $self = { | ||||||||||
| 74 | 0 | ▶ | prefix => defined $params{prefix} ? $params{prefix} : '#', | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | tags => [], | ||||||||||
| 76 | tag_regex => undef, | ||||||||||
| 77 | }; | ||||||||||
| 78 | 0 | bless $self, $class; | |||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | $self->tags($params{tags}) if $params{tags}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | |||||||||||
| 82 | 0 | return $self; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub _gen_tag_regex { | ||||||||
|
Subroutine
_gen_tag_regex: not called CC=1 SCAR=6.9
| |||||||||||
| 86 | 0 | my $self = shift; | |||||||||
| 87 | |||||||||||
| 88 | 0 | $self->{tag_regex} = '(?:\A|\s)[' . $self->prefix . '](' | |||||||||
| 89 | . (join '|', $self->tags) . ')\b'; | ||||||||||
| 90 | |||||||||||
| 91 | # print $self->{tag_regex}, "\n"; | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head1 NAME | ||||||||||
| 95 | |||||||||||
| 96 | Twitter::TagGrep - Find messages with selected tags in Twitter timelines | ||||||||||
| 97 | |||||||||||
| 98 | =head1 VERSION | ||||||||||
| 99 | |||||||||||
| 100 | Version 1.00 | ||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | =head1 SYNOPSIS | ||||||||||
| 104 | |||||||||||
| 105 | use Twitter::TagGrep; | ||||||||||
| 106 | use Net::Twitter; | ||||||||||
| 107 | |||||||||||
| 108 | my $twit = Net::Twitter->new( ... ); | ||||||||||
| 109 | |||||||||||
| 110 | my $tg = Twitter::TagGrep->new( prefix => '#!', | ||||||||||
| 111 | tags => [ 'foo', 'bar' ] ); | ||||||||||
| 112 | |||||||||||
| 113 | my $timeline = $twit->friends_timeline; | ||||||||||
| 114 | |||||||||||
| 115 | # Get tweets containing one or more of #foo, #bar, !foo, or !bar | ||||||||||
| 116 | my @matches = $tg->grep_tags($timeline); | ||||||||||
| 117 | |||||||||||
| 118 | for my $tweet (@matches) { | ||||||||||
| 119 | print $tweet->{text}, "\n", join(', ', @{$tweet->{tags}}), "\n"; | ||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | =head1 METHODS | ||||||||||
| 123 | |||||||||||
| 124 | =over | ||||||||||
| 125 | |||||||||||
| 126 | =item C<new> | ||||||||||
| 127 | |||||||||||
| 128 | Initializes and returns a new Twitter::TagGrep object. | ||||||||||
| 129 | |||||||||||
| 130 | Takes the following optional parameters: | ||||||||||
| 131 | |||||||||||
| 132 | =over | ||||||||||
| 133 | |||||||||||
| 134 | =item C<prefix> | ||||||||||
| 135 | |||||||||||
| 136 | A string defining the set of tag prefixes to recognize. Defaults to '#' | ||||||||||
| 137 | (hashtags) if not specified. | ||||||||||
| 138 | |||||||||||
| 139 | =item C<tags> | ||||||||||
| 140 | |||||||||||
| 141 | Either a single tag to search for or a reference to an array containing | ||||||||||
| 142 | any number of tags to search for. | ||||||||||
| 143 | |||||||||||
| 144 | =back | ||||||||||
| 145 | |||||||||||
| 146 | =item C<prefix> | ||||||||||
| 147 | |||||||||||
| 148 | If passed a parameter, replaces the set of recognized prefixes. | ||||||||||
| 149 | |||||||||||
| 150 | Returns the set of recognized prefixes as a single string value. | ||||||||||
| 151 | |||||||||||
| 152 | =item C<add_prefix> | ||||||||||
| 153 | |||||||||||
| 154 | Appends all parameters to the set of recognized prefixes and returns that | ||||||||||
| 155 | set. | ||||||||||
| 156 | |||||||||||
| 157 | =item C<tags> | ||||||||||
| 158 | |||||||||||
| 159 | If passed one or more parameters, sets the list of recognized tags. Any | ||||||||||
| 160 | array references will add the contents of the referenced array, while other | ||||||||||
| 161 | parameters will be used as-is. | ||||||||||
| 162 | |||||||||||
| 163 | Returns an array of recognized tags. | ||||||||||
| 164 | |||||||||||
| 165 | =item C<add_tag> | ||||||||||
| 166 | |||||||||||
| 167 | As C<tags>, but appends to the list of tags rather than replacing it. | ||||||||||
| 168 | |||||||||||
| 169 | =item C<grep_tags> | ||||||||||
| 170 | |||||||||||
| 171 | Takes a single scalar parameter referencing a Twitter timeline as returned | ||||||||||
| 172 | by Net::Twitter's *_timeline functions. | ||||||||||
| 173 | |||||||||||
| 174 | Returns an array of tweets found within that timeline which contain at least | ||||||||||
| 175 | one instance of (any character found in the C<prefix> setting) followed by | ||||||||||
| 176 | (any string listed in the C<tags> setting). This check is case-insensitive. | ||||||||||
| 177 | |||||||||||
| 178 | A list of tags found in each returned tweet is added to it under the "tags" | ||||||||||
| 179 | hash key. | ||||||||||
| 180 | |||||||||||
| 181 | The tag must normally stand alone as a word by itself, but can be matched as a | ||||||||||
| 182 | substring by using regular expression metacharacters in C<tags> values. | ||||||||||
| 183 | Wildcard searches may also be done in this fashion, such as using the value | ||||||||||
| 184 | "\w+" to locate all tweets containing one or more tags. | ||||||||||
| 185 | |||||||||||
| 186 | =back | ||||||||||
| 187 | |||||||||||
| 188 | =head1 AUTHOR | ||||||||||
| 189 | |||||||||||
| 190 | Dave Sherohman, C<< <dave at sherohman.org> >> | ||||||||||
| 191 | |||||||||||
| 192 | =head1 BUGS | ||||||||||
| 193 | |||||||||||
| 194 | Please report any bugs or feature requests to C<bug-twitter-taggrep at rt.cpan.org>, or through | ||||||||||
| 195 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Twitter-TagGrep>. I will be notified, and then you'll | ||||||||||
| 196 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 197 | |||||||||||
| 198 | |||||||||||
| 199 | |||||||||||
| 200 | |||||||||||
| 201 | =head1 SUPPORT | ||||||||||
| 202 | |||||||||||
| 203 | You can find documentation for this module with the perldoc command. | ||||||||||
| 204 | |||||||||||
| 205 | perldoc Twitter::TagGrep | ||||||||||
| 206 | |||||||||||
| 207 | |||||||||||
| 208 | The latest version of this module may be obtained from | ||||||||||
| 209 | |||||||||||
| 210 | git://sherohman.org/tag_grep | ||||||||||
| 211 | |||||||||||
| 212 | |||||||||||
| 213 | You can also look for information at: | ||||||||||
| 214 | |||||||||||
| 215 | =over 4 | ||||||||||
| 216 | |||||||||||
| 217 | =item * RT: CPAN's request tracker | ||||||||||
| 218 | |||||||||||
| 219 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Twitter-TagGrep> | ||||||||||
| 220 | |||||||||||
| 221 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 222 | |||||||||||
| 223 | L<http://annocpan.org/dist/Twitter-TagGrep> | ||||||||||
| 224 | |||||||||||
| 225 | =item * CPAN Ratings | ||||||||||
| 226 | |||||||||||
| 227 | L<http://cpanratings.perl.org/d/Twitter-TagGrep> | ||||||||||
| 228 | |||||||||||
| 229 | =item * Search CPAN | ||||||||||
| 230 | |||||||||||
| 231 | L<http://search.cpan.org/dist/Twitter-TagGrep/> | ||||||||||
| 232 | |||||||||||
| 233 | =back | ||||||||||
| 234 | |||||||||||
| 235 | |||||||||||
| 236 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 237 | |||||||||||
| 238 | |||||||||||
| 239 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 240 | |||||||||||
| 241 | Copyright 2009 Dave Sherohman, all rights reserved. | ||||||||||
| 242 | |||||||||||
| 243 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 244 | under the same terms as Perl itself. | ||||||||||
| 245 | |||||||||||
| 246 | |||||||||||
| 247 | =cut | ||||||||||
| 248 | |||||||||||
| 249 | 2 | 1; # End of Twitter::TagGrep | |||||||||