| 1 | package Net::Twitter::FriendFinder; |
|---|
| 2 | |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | use UNIVERSAL::require; |
|---|
| 6 | use base qw/Class::Accessor::Fast/; |
|---|
| 7 | __PACKAGE__->mk_accessors(qw/default from setting ids scores resources filters)/); |
|---|
| 8 | use Text::SimpleTable; |
|---|
| 9 | use Net::Twitter::Diff; |
|---|
| 10 | |
|---|
| 11 | our $VERSION = '0.02'; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | sub search { |
|---|
| 15 | my $self = shift; |
|---|
| 16 | my $keyword = shift; |
|---|
| 17 | |
|---|
| 18 | $self->{keyword} = $keyword; |
|---|
| 19 | # set default of default :-p |
|---|
| 20 | my $default = {}; |
|---|
| 21 | $default->{coverage} = $self->{default}{coverage} || 1; |
|---|
| 22 | |
|---|
| 23 | $self->{filters} = []; |
|---|
| 24 | |
|---|
| 25 | my @resources = keys %{ $self->{from} }; |
|---|
| 26 | my $data = {}; |
|---|
| 27 | $self->{resources} = (); |
|---|
| 28 | for my $resource ( @resources ) { |
|---|
| 29 | my $module = substr( $resource , 0 , 1 ) eq '+' ? substr( $resource ,1): "Net::Twitter::FriendFinder::From::" . $resource; |
|---|
| 30 | push @{ $self->{resources} } , $module ; |
|---|
| 31 | $module->require or die $@; |
|---|
| 32 | my $conf = $self->{from}{ $resource } ; |
|---|
| 33 | $conf->{coverage} = $conf->{coverage} || $default->{coverage}; |
|---|
| 34 | $conf->{handicap} = $conf->{handicap} || 1; |
|---|
| 35 | |
|---|
| 36 | my $from = $module->new( $conf ); |
|---|
| 37 | my $results = $from->search( $keyword ); |
|---|
| 38 | |
|---|
| 39 | foreach my $id ( keys %{ $results } ) { |
|---|
| 40 | $data->{ $id } = $results->{ $id } + ( $data->{ $id } || 0 ); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | if( $self->{setting}{filter_already_followers} ) { |
|---|
| 45 | my $twitter = Net::Twitter::Diff->new( username=> $self->{setting}{username} , password => $self->{setting}{password} ); |
|---|
| 46 | my $xfollowing = $twitter->xfollowing; |
|---|
| 47 | |
|---|
| 48 | my @filters = (); |
|---|
| 49 | |
|---|
| 50 | for my $item ( @{ $xfollowing } ) { |
|---|
| 51 | my $screen_name = $item->{screen_name} ; |
|---|
| 52 | if( defined $data->{ $screen_name } ) { |
|---|
| 53 | push @filters , { id => $screen_name , score => $data->{ $screen_name } }; |
|---|
| 54 | delete $data->{ $screen_name }; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | $self->{filters} = \@filters; |
|---|
| 58 | |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | my @ids = sort { $data->{ $a } <=> $data->{ $b } } keys %{ $data }; |
|---|
| 62 | |
|---|
| 63 | my $limit = $self->{setting}{limit} || scalar @ids; |
|---|
| 64 | @ids = reverse @ids; |
|---|
| 65 | if( scalar @ids > $limit ) { |
|---|
| 66 | @ids = @ids[0...$limit-1]; |
|---|
| 67 | } |
|---|
| 68 | $self->{ids} = \@ids; |
|---|
| 69 | $self->{scores} = $data; |
|---|
| 70 | |
|---|
| 71 | return 1; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | sub show { |
|---|
| 75 | my $self = shift; |
|---|
| 76 | |
|---|
| 77 | my $t = Text::SimpleTable->new( 44 ); |
|---|
| 78 | $t->row( 'Resources' ); |
|---|
| 79 | print $t->draw; |
|---|
| 80 | |
|---|
| 81 | my $t0 = Text::SimpleTable->new( 44 ); |
|---|
| 82 | for my $resource ( @{ $self->{resources} } ) { |
|---|
| 83 | $t0->row( $resource ); |
|---|
| 84 | } |
|---|
| 85 | print $t0->draw; |
|---|
| 86 | |
|---|
| 87 | my $t1 = Text::SimpleTable->new( 7 , 34 ); |
|---|
| 88 | $t1->row('Keyword' , $self->{keyword} ); |
|---|
| 89 | print $t1->draw; |
|---|
| 90 | |
|---|
| 91 | my $t2 = Text::SimpleTable->new( 3, 20, 15 ); |
|---|
| 92 | $t2->row( '#', 'Twitter id' , 'Score' ); |
|---|
| 93 | print $t2->draw; |
|---|
| 94 | |
|---|
| 95 | my $t3 = Text::SimpleTable->new( '3', 20, 15 ); |
|---|
| 96 | |
|---|
| 97 | my $cnt = 1; |
|---|
| 98 | for my $id ( @{ $self->{ids} } ) { |
|---|
| 99 | $t3->row( $cnt, $id, $self->{scores}{ $id } ); |
|---|
| 100 | $cnt++; |
|---|
| 101 | } |
|---|
| 102 | print $t3->draw; |
|---|
| 103 | |
|---|
| 104 | if( scalar @{ $self->{filters} } ) { |
|---|
| 105 | my $t4 = Text::SimpleTable->new( 44 ); |
|---|
| 106 | $t4->row( 'Filter Users' ); |
|---|
| 107 | print $t4->draw; |
|---|
| 108 | |
|---|
| 109 | my $t5= Text::SimpleTable->new( 3, 20, 15 ); |
|---|
| 110 | my $cnt = 1; |
|---|
| 111 | for my $filter ( @{ $self->{filters} } ) { |
|---|
| 112 | $t5->row( $cnt , $filter->{id} , $filter->{score} ); |
|---|
| 113 | $cnt++; |
|---|
| 114 | } |
|---|
| 115 | print $t5->draw; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | sub follow { |
|---|
| 121 | use Data::Dumper; |
|---|
| 122 | my $self = shift; |
|---|
| 123 | my $twit = Net::Twitter->new( username=>$self->{setting}{username} ,password=> $self->{setting}{password}); |
|---|
| 124 | foreach my $id ( @{ $self->{ids} } ) { |
|---|
| 125 | my $result = $twit->follow( $id ); |
|---|
| 126 | sleep( $self->{setting}{sleep} ) if defined $self->{setting}{sleep}; |
|---|
| 127 | |
|---|
| 128 | my $res = defined $result ? 'ok' : 'fail'; |
|---|
| 129 | print "follow $id [$res]\n" if $self->{setting}{on_echo}; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | 1; |
|---|
| 134 | |
|---|
| 135 | =head1 NAME |
|---|
| 136 | |
|---|
| 137 | Net::Twitter::FriendFinder - find your twitter friend :-) |
|---|
| 138 | |
|---|
| 139 | =head1 DESCRIPTION |
|---|
| 140 | |
|---|
| 141 | Hello. I started twitter but I did not have much friends. Since I am shay, so that I created this |
|---|
| 142 | module. |
|---|
| 143 | |
|---|
| 144 | You can find twitter friends by using keyword search and then you can follow friends whith this module. |
|---|
| 145 | |
|---|
| 146 | =head1 SYNOPSYS |
|---|
| 147 | |
|---|
| 148 | use Net::Twitter::FriendFinder; |
|---|
| 149 | |
|---|
| 150 | my $tf |
|---|
| 151 | = Net::Twitter::FriendFinder->new({ |
|---|
| 152 | setting => { |
|---|
| 153 | limit => 20, |
|---|
| 154 | on_echo=> 1, |
|---|
| 155 | username => '****', |
|---|
| 156 | password => '****', |
|---|
| 157 | filter_already_followers => 1, |
|---|
| 158 | sleep => 60, |
|---|
| 159 | }, |
|---|
| 160 | default => { |
|---|
| 161 | coverage => 1, |
|---|
| 162 | } , |
|---|
| 163 | from => { |
|---|
| 164 | Google => { coverage => 4 }, |
|---|
| 165 | Twitter => { handicap => 1.3 } , |
|---|
| 166 | TwitterKensaku => { coverage => 2 }, |
|---|
| 167 | +My::Resource => {}, # your own resource |
|---|
| 168 | } |
|---|
| 169 | }); |
|---|
| 170 | |
|---|
| 171 | $tf->search( $keyword ); |
|---|
| 172 | $tf->show(); |
|---|
| 173 | $tf->follow(); |
|---|
| 174 | |
|---|
| 175 | =head1 MODULE |
|---|
| 176 | |
|---|
| 177 | =head2 new |
|---|
| 178 | |
|---|
| 179 | You should set configulation here. |
|---|
| 180 | |
|---|
| 181 | =over 2 |
|---|
| 182 | |
|---|
| 183 | =item B<setting> |
|---|
| 184 | |
|---|
| 185 | =back |
|---|
| 186 | |
|---|
| 187 | =over 4 |
|---|
| 188 | |
|---|
| 189 | =item ->{limit} |
|---|
| 190 | |
|---|
| 191 | you can set limit for how many friends you want to find for max. |
|---|
| 192 | |
|---|
| 193 | =item ->{on_echo} |
|---|
| 194 | |
|---|
| 195 | when on_echo = 1 then , $tf->follow() method print out who you are going to follow. |
|---|
| 196 | |
|---|
| 197 | =item ->{username} |
|---|
| 198 | |
|---|
| 199 | twitter username |
|---|
| 200 | |
|---|
| 201 | =item ->{password} |
|---|
| 202 | |
|---|
| 203 | twitter password |
|---|
| 204 | |
|---|
| 205 | =item ->{sleep} |
|---|
| 206 | |
|---|
| 207 | since Twitter API has request count limitation, you may want to set sleep time when you try to follow a lot of people. |
|---|
| 208 | |
|---|
| 209 | filter_already_followers - when true, it will check your current followers and not try to follow again. required username and password for this. NOTICE: you may miss some friends to filter who add recently because Twitter API does not return them. |
|---|
| 210 | |
|---|
| 211 | =back |
|---|
| 212 | |
|---|
| 213 | =over 2 |
|---|
| 214 | |
|---|
| 215 | =item B<default> |
|---|
| 216 | |
|---|
| 217 | you can set default setting here. |
|---|
| 218 | |
|---|
| 219 | =back |
|---|
| 220 | |
|---|
| 221 | =over 4 |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | =item ->{coverage} |
|---|
| 225 | |
|---|
| 226 | you can set how much you cover. It depend on where you get resource from , so check the resource POD. |
|---|
| 227 | |
|---|
| 228 | =back |
|---|
| 229 | |
|---|
| 230 | =over 2 |
|---|
| 231 | |
|---|
| 232 | =item B<from> |
|---|
| 233 | |
|---|
| 234 | You can set where to search friends from. you can find resource from Net::Twitter::FriendFinder::From::* |
|---|
| 235 | |
|---|
| 236 | from => { |
|---|
| 237 | Google => {}, |
|---|
| 238 | Twitter=> {} |
|---|
| 239 | }, |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | Also if you made your own resource package then set your package name with +. |
|---|
| 243 | |
|---|
| 244 | from => { |
|---|
| 245 | +My::Resource => { }, |
|---|
| 246 | }, |
|---|
| 247 | |
|---|
| 248 | =back |
|---|
| 249 | |
|---|
| 250 | =head2 search |
|---|
| 251 | |
|---|
| 252 | this seach method try to search your friends. |
|---|
| 253 | |
|---|
| 254 | =head2 show |
|---|
| 255 | |
|---|
| 256 | display who you find from your criteria. |
|---|
| 257 | |
|---|
| 258 | =head2 follow |
|---|
| 259 | |
|---|
| 260 | follow people you find. I recommend that before calling this module , you |
|---|
| 261 | should check who you are going t follow with show() method. |
|---|
| 262 | |
|---|
| 263 | =head1 HOW TO MAKE YOUR OWN RESOURCE |
|---|
| 264 | |
|---|
| 265 | this is just simple one. |
|---|
| 266 | |
|---|
| 267 | package My::Recomend::User; |
|---|
| 268 | |
|---|
| 269 | use strict; |
|---|
| 270 | use warnings; |
|---|
| 271 | |
|---|
| 272 | use base qw/Net::Twitter::FriendFinder::From/; |
|---|
| 273 | |
|---|
| 274 | sub search { |
|---|
| 275 | return { |
|---|
| 276 | 'tomyhero' => 100, |
|---|
| 277 | 'perl' => 30, |
|---|
| 278 | }; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | 1; |
|---|
| 282 | |
|---|
| 283 | then |
|---|
| 284 | |
|---|
| 285 | my $tw = Net::Twitter::FriendFinder->new({ |
|---|
| 286 | from => { |
|---|
| 287 | +My::Recomend::User => {}, |
|---|
| 288 | } |
|---|
| 289 | }); |
|---|
| 290 | |
|---|
| 291 | $tw->search(); |
|---|
| 292 | $tw->show(); |
|---|
| 293 | $tw->follow(); |
|---|
| 294 | |
|---|
| 295 | =head1 SEE ALSO |
|---|
| 296 | |
|---|
| 297 | L<Net::Twitter> |
|---|
| 298 | |
|---|
| 299 | L<Net::Twitter::FriendFinder::From::Google> |
|---|
| 300 | |
|---|
| 301 | L<Net::Twitter::FriendFinder::From::Twitter> |
|---|
| 302 | |
|---|
| 303 | L<Net::Twitter::FriendFinder::From::TwitterKensaku> |
|---|
| 304 | |
|---|
| 305 | L<Net::Twitter::FriendFinder::From::TwitterDiff> |
|---|
| 306 | |
|---|
| 307 | L<Net::Twitter::FriendFinder::From::URL> |
|---|
| 308 | |
|---|
| 309 | =head1 AUTHOR |
|---|
| 310 | |
|---|
| 311 | Tomohiro Teranishi<tomohiro.teranishi@gmail.com> |
|---|
| 312 | |
|---|
| 313 | =cut |
|---|