Created
December 6, 2012 17:38
-
-
Save popkirby/4226388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use utf8; | |
use Encode qw/decode/; | |
use Encode qw/encode/; | |
use Net::Twitter; | |
use Config::Simple; | |
use LWP::UserAgent; | |
use HTTP::Request::Common; | |
use Web::Scraper; | |
binmode STDIN, ":encoding(shiftjis)"; | |
binmode STDOUT, ":encoding(shiftjis)"; | |
# 診断メーカーの結果を引っ張ってきてツイートするスクリプト。 | |
# 最初にアカウントを登録すること。 | |
# 設定保存用 | |
our $cfg; | |
our $cfgname = "config.ini"; | |
our $account; | |
our $access_token; | |
our $access_secret; | |
our $screen_name; | |
our $shindan_id; | |
our @names; | |
our $shindan_url = 'http://shindanmaker.com/'; | |
# 初期化 | |
sub init{ | |
# Twitterライブラリの初期化 | |
$account = Net::Twitter->new( | |
traits => ['API::REST', 'OAuth' ], | |
consumer_key => 'ここと', | |
consumer_secret => 'ここ', | |
); | |
# 設定ファイルを読み込み or 作成 | |
if( -f $cfgname ) { | |
$cfg = new Config::Simple($cfgname); | |
&restore_config(); | |
} else { | |
$cfg = new Config::Simple(syntax => 'ini'); | |
&create_conf(); | |
} | |
return; | |
} | |
# 設定ファイル作成 | |
sub create_conf{ | |
print "ようこそようこそ. 結果をツイートするアカウントと使用する診断メーカーを登録します。\n"; | |
®ist_user(); | |
®ist_shindan(); | |
®ist_names(); | |
return; | |
} | |
sub regist_user{ | |
print "次の URL にアクセスして下さい: "; | |
print $account->get_authorization_url . "\n"; | |
print "上の URL から取得した PIN コードを入力: "; | |
my $pin = <STDIN>; | |
chomp($pin); | |
($access_token, $access_secret, $screen_name) = $account->request_access_token(verifier => $pin); | |
print "次のユーザを登録: $screen_name\n"; | |
&save_config(); | |
return; | |
} | |
sub regist_shindan{ | |
print "使用する診断メーカーのID ( http://shindanmaker.com/xxxxxx ←ここの数字 )を入力: "; | |
$shindan_id = <STDIN>; | |
chomp $shindan_id; | |
&save_config(); | |
return; | |
} | |
sub regist_names{ | |
print "診断に使用する名前を登録します。(0と入力すると終了します) \n"; | |
print "名前: "; | |
while( <STDIN> ){ | |
chomp($_); | |
if($_ eq '0'){ | |
last; | |
} | |
push(@names, $_); | |
print "$_ を登録.\n"; | |
print "名前: "; | |
} | |
print "登録を終了しました. \n"; | |
&save_config(); | |
return; | |
} | |
sub save_config{ | |
my $names_str = join(';', @names); | |
$cfg->param('general.user', $screen_name); | |
$cfg->param('general.accesstoken', $access_token); | |
$cfg->param('general.accesssecret', $access_secret); | |
$cfg->param('general.shindanid', $shindan_id); | |
$cfg->param('general.names', encode('shiftjis', $names_str)); | |
$cfg->save($cfgname); | |
return; | |
} | |
sub restore_config{ | |
$screen_name = $cfg->param('general.user'); | |
$access_token = $cfg->param('general.accesstoken'); | |
$access_secret = $cfg->param('general.accesssecret'); | |
$shindan_id = $cfg->param('general.shindanid'); | |
# 追加 | |
$account->access_token($access_token); | |
$account->access_token_secret($access_secret); | |
@names = split(/;/, decode('shiftjis', $cfg->param('general.names'))); | |
return; | |
} | |
sub do_shindan{ | |
my $shindan = $shindan_url . $shindan_id; | |
my $tmp_name; | |
foreach $tmp_name (@names){ | |
my $request = POST( $shindan, Content_Type => 'form-data', Content => { u => encode('UTF-8', $tmp_name), from => ''}); | |
my $ua = LWP::UserAgent -> new; | |
my $result = decode('UTF-8', $ua -> request( $request ) -> as_string); | |
my $scraper = scraper{ | |
process "form#forcopy>textarea", 'text[]' => 'TEXT'; | |
result 'text'; | |
}; | |
my $str = $scraper->scrape($result); | |
print "$tmp_name:@$str[0]\n"; | |
my $status = $account->update(@$str[0]); | |
} | |
return; | |
} | |
sub main{ | |
print "auto_shindan.pl v0.0\n"; | |
&do_shindan(); | |
} | |
&init(); | |
&main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment