root/lang/perl/Net-Twitter-FriendFinder/tags/0.03/lib/Net/Twitter/FriendFinder.pm @ 1737

Revision 1737, 7.6 kB (checked in by xcezx, 6 years ago)

/dotfiles/irssi/xcezx-config: 最近の。

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