App-Chart
view release on metacpan or search on metacpan
lib/App/Chart/IntradayHandler.pm view on Meta::CPAN
# Intraday graphs.
# Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde
# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart. If not, see <http://www.gnu.org/licenses/>.
package App::Chart::IntradayHandler;
use 5.006;
use strict;
use warnings;
use Locale::TextDomain ('App-Chart');
use App::Chart;
# uncomment this to run the ### lines
#use Smart::Comments;
our @handler_list = ();
sub new {
my $class = shift;
my $self = bless ({ @_ }, $class);
App::Chart::Sympred::validate ($self->{'pred'});
push @handler_list, $self;
@handler_list = sort { $a->{'name'} cmp $b->{'name'} } @handler_list;
return $self;
}
sub handlers_for_symbol {
my ($class, $symbol) = @_;
App::Chart::symbol_setups ($symbol);
return grep { $_->{'pred'}->match($symbol) } @handler_list;
}
sub handler_for_symbol_and_mode {
my ($class, $symbol, $mode) = @_;
my @found
= grep {$_->{'mode'} eq $mode} $class->handlers_for_symbol ($symbol);
return $found[0];
}
sub download {
my ($self, $symbol) = @_;
### IntradayHandler download: $symbol
my ($image, $error, $resp);
my $mode = $self->{'mode'};
my $proc = $self->{'proc'};
my ($url, %options);
if (! eval { ($url, %options) = $proc->($self, $symbol, $mode); 1 }) {
# mainly errors downloading procs like Barchart.pm
$error = $@;
} else {
require App::Chart::Download;
require App::Chart::Intraday;
App::Chart::Download::status
(__x('Intraday image {symbol} {mode}',
symbol => $symbol,
mode => $mode));
App::Chart::Download::verbose_message ("Intraday image", $url);
require App::Chart::UserAgent;
my $ua = App::Chart::UserAgent->instance;
if (exists $options{'cookie_jar'}) {
### IntradayHandler cookie_jar: $options{'cookie_jar'}->as_string
$ua->cookie_jar ($options{'cookie_jar'});
} else {
$ua->cookie_jar ({});
}
require HTTP::Request;
my @headers = (Referer => $options{'referer'});
my $req = HTTP::Request->new ('GET', $url, \@headers);
$ua->prepare_request ($req);
### IntradayHandler request: $req->as_string
my $resp = $ua->request
($req,
sub {
my ($chunk, $resp, $protobj) = @_;
$resp->add_content($chunk);
( run in 1.092 second using v1.01-cache-2.11-cpan-39bf76dae61 )