Net-Twitter-Friend-Finder

昨日のコードをパッケージっぽくした。
http://tomyhero-perl-module.googlecode.com/svn/trunk/Net-Twitter-Friend-Finder/

追記: 移動しました。

http://coderepos.org/share/browser/lang/perl/Net-Twitter-Friend-Finder

package Net::Twitter::Friend::Finder;

use strict;
use warnings;
use base qw/Class::Accessor::Fast/;
use Net::Twitter;

__PACKAGE__->mk_accessors(qw/limit ids username password on_echo/);

sub search {
    die 'please implement search method';
}

sub show {
    my $self = shift;

    my $t1 = Text::SimpleTable->new( 3, 20 );
    $t1->row( '#', 'Twitter id');
    print $t1->draw;
    
    my $t2 = Text::SimpleTable->new( '3', 20 );

    my $cnt = 1;
    for my $id ( @{ $self->{ids} } ) {
        $t2->row( $cnt, $id );
        $cnt++;
    }

    print $t2->draw;

}

sub follow {
    my $self = shift;
    my $twit = Net::Twitter->new( username=>$self->{username} ,password=> $self->{password});
    foreach my $id (  @{ $self->{ids} } ) {
        my $result = $twit->follow( $id );
        print "follow $id \n" if $self->{on_echo};
    }

}

1;

=head1 NAME

Net::Twitter::Friend::Finder - Find Twiiter Frined 

=head1 DESCRIPTION

this package is just template so that you should not use this directly.

=head1 SEE ALSO

Net::Twitter::Friend::Finder::FromGoogle

=head1 AUTHOR

Tomohiro Teranishi <tomohiro.teranishi@gmail.com>

=cut
package Net::Twitter::Friend::Finder::FromGoogle;

use strict;
use warnings;
use base qw/Net::Twitter::Friend::Finder/;
use Web::Scraper;
use URI;
use URI::Escape;
use Text::SimpleTable ;
use Net::Twitter;

__PACKAGE__->mk_accessors(qw/lang _count _keyword/);

my $url = "http://www.google.com/search?q=%s+site:.twitter.com&num=%d&start=%d&hl=%s";

sub search{
    my $self     = shift;
    my $keyword  = shift || 'twitter';
    my $args     = shift || {};
    my $end_page = $args->{end_page}    || 3;

    my $limit    = $self->{limit}       || 10 ;
    my $lang     = $self->{lang}        || '';

    $self->{_keyword} = $keyword;
    $keyword = URI::Escape::uri_escape( $keyword );
    $keyword =~ s/%20/+/g;
    my $google_url = scraper {
        process "table> tr > td.j > font > span.a",
        description => 'TEXT',
    };

    my $google = scraper {
        process "div.g",
        "urls[]" => $google_url;
    };

    my $data = {};
    for( my $page = 1 ; $page <= $end_page ; $page++ ) {
        my $target_uri = sprintf( $url , $keyword , 100 , ( $page-1 ) * 100  , $lang )  ;
        my $res = $google->scrape( URI->new( $target_uri ) );

        for my $item ( @{ $res->{urls} } ) {
            my ( $name ) = $item->{description} =~ m|twitter.com/(\w+)/|;
            next unless defined $name;
            $data->{ $name } = $data->{ $name } ? $data->{ $name }+1 : 1 ;
        }
    }

    my @ids = sort { $data->{ $a } <=> $data->{ $b } } keys %{ $data };
    @ids = reverse @ids;
    if( scalar @ids > $limit ) {
        @ids = @ids[0...$limit-1];
    }
    $self->{ids} = \@ids;
    $self->{_count} = $data;


}

sub show {
    my $self = shift;

    my $t0 = Text::SimpleTable->new( 44 );
    $t0->row( __PACKAGE__ );
    print $t0->draw;

    my $t1 = Text::SimpleTable->new( 7 , 34 );
    $t1->row('Keyword' , $self->{_keyword} );
    print $t1->draw;

    my $t2 = Text::SimpleTable->new( 3, 20, 15 );
    $t2->row( '#', 'Twitter id' , 'Found count' );
    print $t2->draw;
    
    my $t3 = Text::SimpleTable->new( '3', 20, 15 );

    my $cnt = 1;
    for my $id ( @{ $self->{ids} } ) {
        $t3->row( $cnt, $id, $self->{_count}{ $id } );
        $cnt++;
    }
    print $t3->draw;

}

1;

=head1 NAME

Net::Twitter::Friend::Finder::FromGoogle - Find Twitter Friend from Google

=head1 SYNOPSYS

 use strict;
 use warnings;
 use FindBin::libs;
 use Net::Twitter::Friend::Finder::FromGoogle;
 my $word = "エロ";

 my $twitter = Net::Twitter::Friend::Finder::FromGoogle->new( { on_echo=>1 , username=>"*****" , password=>"*****" , limit => 20 , lang=> 'ja' });
 $twitter->search( $word , { end_page => 5 } );
 $twitter->show();
 $twitter->follow();

=head1 DESCRIPTION

Try keyword search at google.com then find twitter users!

=head1 AUTHOR

Tomohiro Teranishi <tomohiro.teranishi@gmail.com>

=cut

出力結果

いくつかの結果を画像で。



結果

これで、興味のあるキーワドを発言している人を見つけて追加できるよ!!
(ちなみに、twiiterのpeople searchを使えば、プロフィールコメント限定だけど、twitter.comサイト上で見つけれるね。)

ウーン

CPANに上げるには微妙かなぁ。どうでしょうか。その場合google以外も欲しいところだなぁ。

TOOD

  • グーグルのページ数を見てないので、見た方がいいかも。