root/lang/perl/Net-Twitter-FriendFinder/tags/0.02/lib/Net/Twitter/FriendFinder.pm @ 1738

Revision 1738, 7.0 kB (checked in by xcezx, 6 years ago)

ごめんなさいごめんなさいごめんなさいごめんなさい

Line 
1package Net::Twitter::FriendFinder;
2
3use warnings;
4use strict;
5use UNIVERSAL::require;
6use base qw/Class::Accessor::Fast/;
7__PACKAGE__->mk_accessors(qw/default from setting ids scores resources filters)/);
8use Text::SimpleTable;
9use Net::Twitter::Diff;
10
11our $VERSION = '0.02';
12
13
14sub 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
74sub 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
120sub follow {
121use 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
1331;
134
135=head1 NAME
136
137Net::Twitter::FriendFinder - find your twitter friend :-)
138
139=head1 DESCRIPTION
140
141Hello. I started twitter but I did not have much friends.  Since I am shay, so that I created this
142module.
143
144You 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
179You 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
191you can set limit for how many friends you want to find for max.
192
193=item ->{on_echo}
194
195when on_echo = 1 then , $tf->follow() method print out who you are going to follow.
196
197=item ->{username}
198
199twitter username
200
201=item ->{password}
202
203twitter password
204
205=item ->{sleep}
206
207since Twitter API has request count limitation, you may want to set sleep time when you try to follow a lot of people.
208
209filter_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
217you can set default setting here.
218
219=back
220
221=over 4
222
223
224=item ->{coverage}
225
226you 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
234You 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
242Also 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
256display who you find from your criteria.
257
258=head2 follow
259
260follow people you find. I recommend that before calling this module , you
261should check who you are going t follow with show() method.
262
263=head1 HOW TO MAKE YOUR OWN RESOURCE
264
265this 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
283then
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
297L<Net::Twitter>
298
299L<Net::Twitter::FriendFinder::From::Google>
300
301L<Net::Twitter::FriendFinder::From::Twitter>
302
303L<Net::Twitter::FriendFinder::From::TwitterKensaku>
304
305L<Net::Twitter::FriendFinder::From::TwitterDiff>
306
307L<Net::Twitter::FriendFinder::From::URL>
308
309=head1 AUTHOR
310
311Tomohiro Teranishi<tomohiro.teranishi@gmail.com>
312
313=cut
Note: See TracBrowser for help on using the browser.