ハブ君の寝言

日記のような何か

habu_bot

Twitter上で人気者だったhabu_botは,
本日2012/03/27をもちまして終了することになりました.
今まで応援してありがとう!!

という訳で,ソースコード公開するよ!

#!/usr/bin/env perl
use strict;
use warnings;
use Net::Twitter::Lite;
my $consumer_key = 'xxxxxxxxxxxxxxxx';
my $consumer_key_secret = 'xxxxxxxxxxxxxxxxx';
my $access_token = 'xxxxxxxxxxxxxxxx';
my $access_token_secret = 'xxxxxxxxxxxxxxxxxxx';
my $nt = Net::Twitter::Lite->new(
#  traits          => ['API::REST', 'OAuth'],
  consumer_key    => $consumer_key,
  consumer_secret => $consumer_key_secret,
);
$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);
my $cursor = -1;
my $home;
my $tl;
  open(FH,"< habu_id.ini");
  my $since_id = <FH>;
  close(FH);
my $since_id_home = $since_id;
my $since_id_habu = $since_id;
my @sid = (0,0,0,0);
my $old_hour = 0;
while(1){
  my $times = time();
  my ($sec,$min,$hour,$mday,$month,$year,$wday,$stime) = localtime($times);

  if($old_hour != $hour){
    &my_follow($nt);
  }

  eval{
    $home = $nt->home_timeline({since_id => $since_id});
    $since_id_home = @{$home}[0]->{'id'};
  };
    foreach my $list(@{$home}){
        my $txt = $list->{'text'};
        my $name = $list->{'user'}->{'screen_name'};
        my $id = $list->{'id'};
        if($txt =~ /habu_/ && $txt !~ /http/){
          &my_fav($nt,$id);
          #&my_rt($nt,$id);
        }
    }

$sid[0] = &my_search($nt,"habu",$sid[0]);
$sid[1] = &my_search($nt,"ハブ",$sid[1]);
$sid[2] = &my_search($nt,"はぶ",$sid[2]);

$since_id = $since_id_home;
#$since_id = (sort { $b <=> $a } ($since_id , $since_id_home, $since_id_habu))[0];
    open(OUT, "> habu_id.ini");
    print OUT $since_id;
    close(OUT);
  sleep(60);
  $old_hour = $hour;
}


sub my_post{
my $nt = $_[0];
my $post = $_[1];
        eval{
              $nt->update({ status =>$post });
        };

}

sub my_fav{
my $nt = $_[0];
my $fav = $_[1];
        eval{
                $nt->create_favorite($fav);
        };
}

sub my_rep{
my $nt = $_[0];
my $rep = $_[1];
my $rep_id = $_[2];
        eval{
              $nt->update({ status =>$rep, in_reply_to_status_id => $rep_id });
        };

}

sub my_rt{
my $nt = $_[0];
my $rt = $_[1];
        eval{
              $nt->retweet($rt);
        };
}

sub my_search{
my $nt = $_[0];
my $sc = $_[1];
my $sid = $_[2];
eval{
    my $r = $nt->search($sc,{since_id => $sid});
    $tl = $r->{results};
    $since_id_habu = @{$tl}[0]->{id};
};

    foreach my $list(@{$tl}){
        my $txt = $list->{text};

        my $name = $list->{'user'}->{'screen_name'};
        #my $name = $list->{user}{screen_name};
        my $id = $list->{id};
        if($txt =~ /@/ || $txt =~ /http/){
          if($txt =~ /habu_bot|はぶbot|ハブbot/ ){
            &my_fav($nt,$id);
          }
        }
        else{
          if($txt =~ /ハブられ|はぶられ|はぶる|ハブる|bot/ && $txt !~ /USB|usb/ && $name !~ /bot|BOT/){
            &my_fav($nt,$id);
          }
          &my_rt($nt,$id);
        }
    }
return $since_id_habu;
}


sub my_follow{
  my $nt = $_[0];
  my $flw;
  eval{
    $flw = $nt->followers({count=>10});
  };
  foreach my $list(@{$flw}){
    eval{
      $nt->create_friend($list->{id});
    };
  }
}

参照:

Net::Twitter::Lite

http://d.hatena.ne.jp/hab_kun/20101215/1292393039