Perlで作ったSeleniumのプログラム置き場

書いたやつ。まだ大したことやってない(Selenium IDEでポチポチいじっただけ)。他のプログラム書きたいけどネタがない。

Pex.jpの検索ポイント乞食

1日一回起動で5p(=0.5円)。似たようなことは他のポイントサイトでも作れる。アドレスとボタンのIDと待機時間いじるだけですね。

# This script is gets PEX.JP's search point automatically
# Please run this script each day
# @vecter

use strict;
use warnings;
use WWW::Selenium;
use Time::HiRes qw(sleep);

my $count = 0;

my WWW::Selenium $sel = WWW::Selenium->new(
    host => "localhost", 
    port => 4444, 
    browser => "*chrome", 
    browser_url => "http://pex.jp",
);

$sel->start;
$sel->open("/");
$sel->open("/login");
$sel->type("id=email", "hogehoge@hoge.jp"); #ID
$sel->type("id=login_password", "foobar");  #Password
$sel->click("name=commit");
sleep(3);
while(1)
{
	$sel->type("id=keyword", "renger$count");
	$sel->click("id=web_search_form_search_submit");
	if($count < 4){$count++;}else{last;}
	sleep(310);
}
$sel->stop;

15qm.comを使った捨てアド生成

捨てアド作成サイトの15qm.com様より捨てアドを作る。$1に捨てアドが格納される。捨てアド生成したら会員登録するなり応募するなり。
各サイトの会員認証するのに受信箱の操作をしなきゃいけないので、これ単体では何も出来ない。
あと15qm.com様の負荷を考えてループ回す時はsleepを適度にかけてください。

use strict;
use warnings;
use WWW::Selenium;
use Time::HiRes qw(sleep);
use Encode;
use utf8;

#set selenium instance
my WWW::Selenium $sel = WWW::Selenium->new(
    host => "localhost",
    port => 4444,
    browser => "*chrome",
    browser_url => "http://15qm.com/",
);

#start and open 15qm.com
$sel->start;
$sel->open("/");
my $at = $sel->get_location();

#create address
$sel->click("css=img.rollover");
$sel->wait_for_page_to_load(5000);
while($sel->get_location() eq $at)
{
	print "wait page load!";
	$sel->wait_for_page_to_load(5000);
}
my $page = $sel->get_html_source();

#output address
if( $page =~ /(m\d+\@hiru-dea.com)/ )
{
	print $1; #$1 = email address
}

#edit submit process

#quit current email survice
$sel->click("xpath=(//img[\@alt='利用を終了する'])[2]");
$sel->wait_for_page_to_load(5000);

$sel->stop;

でぃばプロ BOT

その名の通り自動でライブやってくれるBOT。立ち上げとくだけで攻略出来るようにした。ちょっと改良して実行ユニットと次にログインする時間を表示させるようにした。

  • 現在セットされてるステージしか攻略しないので、ステージ制覇しても同じ所をやり続ける。
    • 後半ステージはユニット組み直さないとクリア出来ないのでこのままでいいかな。
  • PCのスリープかけるとselenium自体がセッションタイムアウト起こして止まる
    • seleniumタイムアウト時間いじれば解決。スリープ復帰直後にページが見つからないと出た場合は更新すればそのまま動くっぽい。スリープ復帰後にブラウザ更新しとけばおk
  • BOT行為なのでBANされる可能性大。自己責任。
    • 待機時間の関係で大して負荷かからないけどね。待機数十分に対して実操作は数十秒くらい

使用する場合は最低限、チュートリアルとユニットの設定済ませてご利用のこと

# This script is captures to DIVAPRO automatically.
# if use it, you prepare for DIVAPRO's unit in advance.
# @vecter

use strict;
use warnings;
use WWW::Selenium;
use Time::HiRes qw(sleep);
use Time::Local;
use Encode;
use utf8;
binmode(STDOUT, ":utf8");

#set selenium instance
my WWW::Selenium $sel = WWW::Selenium->new(
	host => "localhost",
	port => 4444,
	browser => "*chrome",
	browser_url => "https://project-diva-ac.net/",
	);

my @live_stage_per = (0,0,0,0,0,0,0,0); #each stage percent
my @unit_state = ( -1, -1, -1 ); # -1 = unavaliable, 0 = waiting live, etc = remain_status
my @exec_live_locate = ("css=button.command.failure",
						"css=#unit2 > form[name=\"divaProActionForm\"] > button.command.failure",
						"css=#unit3 > form[name=\"divaProActionForm\"] > button.command.failure");

my @jump_locate = ("css=#units-tab-group > li",
					"//ul[\@id=\'units-tab-group\']/li[2]",
					"//ul[\@id=\'units-tab-group\']/li[3]");

my $count = 0; #use counter
my $page; #use analyzing page


#start and opem DIVA.NET
$sel->start;
LOGIN: # re login
$sel->open("/divanet");

#login to DIVA.NET & goto divapro
$sel->type("name=accessCode", "xxxxxxxxxxxxxxxxxxx"); # edit ID
$sel->type("name=password", "hogehoge"); # edit pass
$sel->click("id=submit");
sleep(4);
$sel->click("css=img");
sleep(4);
$sel->click("link=ユニット");
sleep(3);

my $timer = 4000; #use time counter

###### analyze all unit's status #####
$page = $sel->get_html_source();

for($count = 1; $count < 4; $count++){
	my $remain = "0";
	if($count == 1){$remain = "(remain1st.+?<\/span)";}
	elsif($count == 2){$remain = "(remain2nd.+?<\/span)";}
	elsif($count == 3){$remain = "(remain3rd.+?<\/span)";}

	# analyze unit's exist
	if($page =~ /(Unit($count))/){
		if($page =~ /(liveSetting\/($count))/){
			$unit_state[$count-1] = 0;  #not selected
		}elsif($page =~ /($remain)/){
			if($1 =~ /((\d+(秒|分|(時間)))|(START))/){
				$unit_state[$count-1] = $1;  # waaiting time
			}
		}
	}else{
		$unit_state[$count-1] = -1; # unavaliable unit
	}
}

##### control divapro #####
$count = 1;
foreach my $status (@unit_state){

	#jump to unit page
	if($count < 1){
		$sel->click($jump_locate[$count - 1]);
	}

	# exec live
	if($status eq "START"){
		$sel->click($exec_live_locate[$count - 1]); #click [live start] 
		sleep(3);
		$sel->click("css=button.command"); #click [skip]
		sleep(3);
		$sel->click("link=ユニット");
		print "exec unit". $count;
		sleep(3);

		# continued set live
		if($count < 1){
			$sel->click($jump_locate[$count - 1]);
		}
		goto SET;

	# set live timer
	}elsif($status eq 0){
		SET:
		$sel->click("//section[\@id='unit".$count."']/form[2]/button"); #click [live setting]
		sleep(3);
		$sel->click("css=button.command"); #click [go to live]
		sleep(3);
		$sel->click("link=ユニット");
		sleep(3);
	}

	#culculate wait time
	my $temp = 60;
	if($unit_state[$count - 1] =~ /時間/){ $temp = 3600;}
	elsif($unit_state[$count - 1] =~ /(\d+)()/){ $temp = $1;}
	elsif($unit_state[$count - 1] =~ /(\d+)()/){ $temp = $1 * 60;}
	elsif($unit_state[$count - 1] =~ /-1/){ $temp = $timer;}
	elsif($unit_state[$count - 1] =~ /START/){ $temp = 0}

	if( $timer > $temp ) {
		$timer = $temp;
	}
	$count++;
}

# set login timer
$sel->open("/");
if($timer > 180){
	my $tm_now = time; #get current time
	$tm_now += $timer;
	print "next login time is " . localtime($tm_now) . "\n";
	while(1){
		sleep(120);
		my $temp = time;
		if($tm_now < $temp){last;} #end to wait if current time is more than timer
	}
}else{
	sleep($timer);
}
goto LOGIN; #jump to login

$sel->stop;

その他

文字コード絡みの処理に手間取り中。get_body_textとかget_html_sourceからどうやってTreebuilderなりに渡せばよいのやら。
今ん所どうやればいいのか分からんので、現在開いてるページのソースを取得して正規表現かけるという方法をとっている。よほど複雑なことしなければこっちのほうが簡単で処理が早い。