Finance-YahooJPN-QuoteDetail
view release on metacpan or search on metacpan
lib/Finance/YahooJPN/QuoteDetail.pm view on Meta::CPAN
unless ($$self{'symbol'} =~ /^\d{4}$/ || $$self{'symbol'} =~ /^\d{4}\.[a-zA-Z]$/) {
croak "The 'symbol' attribute must not be omitted .A stock symbol should be given with four numbers. (ex. `6758' or '6758.t' )";
}
if($$self{symbol} =~ /^(\d{4})\.([a-zA-Z])$/) {
$$self{'symbol'} = $1;
$$self{'market'} = $2;
}elsif($$self{symbol} =~ /^(\d{4})$/){
unless($$self{'market'} && $$self{'market'} =~ /^[a-zA-Z]$/){
croak "The 'market' attribute must not be omitted ";
}
}
$$self{'url'} = 'http://quote.yahoo.co.jp/q?s='.$$self{'symbol'}.'&d='.$$self{'market'};
return $self;
}
sub check(){
my $self = shift;
print "proxy is ";
print $$self{'proxy'};
print "\n";
print "symbol is ";
print $$self{'symbol'};
print "\n";
print "url is ";
print $$self{'url'};
print "\n";
}
sub quote(){
my $self = shift;
my $html_string = $self->_access_to_yahoo;
$$self{'string_ref'} = $self->_extract_data_from_table();
$self->_set_last_trade();
$self->_set_high_price();
$self->_set_low_price();
$self->_set_prev_close();
$self->_set_volume();
$self->_set_change();
}
sub quick_quote(){
my $self = shift;
my $html_string = $self->_access_to_yahoo;
$$self{'string_ref'} = $self->_extract_data_from_table();
}
sub _access_to_yahoo(){
my $self = shift;
#Create a user agent object
my $ua = LWP::UserAgent->new(env_proxy => 1,
keep_alive => 1,
timeout => 30,
);
$ua->proxy(['http'], $$self{'proxy'}) if $$self{'proxy'};
$ua->agent("MyApp/0.1");
my $req = HTTP::Request->new(GET=>$$self{'url'});
$req->content_type('application/x-www-form-urlencoded');
$req->content('');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
if ($res->is_success) {
$$self{'_html_string'} = $res->content;
return 1;
} else {
$$self{'error_msg'} = "Cannot access to Yahoo!";
return 0;
}
}
sub _extract_data_from_table(){
my $self = shift;
my $te = new HTML::TableExtract;
my $string = "";
my $ts = "";
my $line = "";
$te->parse($$self{'_html_string'});
# Examine all matching tables
foreach $ts ($te->table_states) {
if( $ts->depth eq "1" && $ts->count eq "3" ||
$ts->depth eq "0" && $ts->count eq "5" ){
foreach my $row ($ts->rows) {
$string = join(' ', @$row);
$string =~ s/\n//g;
$string =~ s/\r//g;
$line .= $string;
}
}
}
$$self{'_string'} = $line;
return \$line;
}
sub get_symbol_name(){
my ($self) = @_;
return $$self{'symbol_name'} if $$self{'symbol_name'};
$self->_set_symbol_name();
return $$self{'symbol_name'};
}
sub _set_symbol_name(){
my ($self) = @_;
( run in 0.776 second using v1.01-cache-2.11-cpan-800906f7e73 )