| 1 | # Intraday graphs. | ||
| 2 | |||
| 3 | # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 4 | |||
| 5 | # This file is part of Chart. | ||
| 6 | # | ||
| 7 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 8 | # terms of the GNU General Public License as published by the Free Software | ||
| 9 | # Foundation; either version 3, or (at your option) any later version. | ||
| 10 | # | ||
| 11 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 12 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 13 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 14 | # details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License along | ||
| 17 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | package App::Chart::IntradayHandler; | ||
| 20 | use 5.006; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use App::Chart; | ||
| 26 | |||
| 27 | # uncomment this to run the ### lines | ||
| 28 | #use Smart::Comments; | ||
| 29 | |||
| 30 | our @handler_list = (); | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $class = shift; | ||
| 34 | my $self = bless ({ @_ }, $class); | ||
| 35 | App::Chart::Sympred::validate ($self->{'pred'}); | ||
| 36 | push @handler_list, $self; | ||
| 37 | @handler_list = sort { $a->{'name'} cmp $b->{'name'} } @handler_list; | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub handlers_for_symbol { | ||
| 42 | my ($class, $symbol) = @_; | ||
| 43 | App::Chart::symbol_setups ($symbol); | ||
| 44 | return grep { $_->{'pred'}->match($symbol) } @handler_list; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub handler_for_symbol_and_mode { | ||
| 48 | my ($class, $symbol, $mode) = @_; | ||
| 49 | my @found | ||
| 50 | = grep {$_->{'mode'} eq $mode} $class->handlers_for_symbol ($symbol); | ||
| 51 | return $found[0]; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub download { | ||
| 55 | my ($self, $symbol) = @_; | ||
| 56 | ### IntradayHandler download: $symbol | ||
| 57 | my ($image, $error, $resp); | ||
| 58 | |||
| 59 | my $mode = $self->{'mode'}; | ||
| 60 | my $proc = $self->{'proc'}; | ||
| 61 | my ($url, %options); | ||
| 62 | if (! eval { ($url, %options) = $proc->($self, $symbol, $mode); 1 }) { | ||
| 63 | # mainly errors downloading procs like Barchart.pm | ||
| 64 | $error = $@; | ||
| 65 | } else { | ||
| 66 | require App::Chart::Download; | ||
| 67 | require App::Chart::Intraday; | ||
| 68 | |||
| 69 | App::Chart::Download::status | ||
| 70 | (__x('Intraday image {symbol} {mode}', | ||
| 71 | symbol => $symbol, | ||
| 72 | mode => $mode)); | ||
| 73 | App::Chart::Download::verbose_message ("Intraday image", $url); | ||
| 74 | |||
| 75 | require App::Chart::UserAgent; | ||
| 76 | my $ua = App::Chart::UserAgent->instance; | ||
| 77 | |||
| 78 | if (exists $options{'cookie_jar'}) { | ||
| 79 | ### IntradayHandler cookie_jar: $options{'cookie_jar'}->as_string | ||
| 80 | $ua->cookie_jar ($options{'cookie_jar'}); | ||
| 81 | } else { | ||
| 82 | $ua->cookie_jar ({}); | ||
| 83 | } | ||
| 84 | |||
| 85 | require HTTP::Request; | ||
| 86 | my @headers = (Referer => $options{'referer'}); | ||
| 87 | my $req = HTTP::Request->new ('GET', $url, \@headers); | ||
| 88 | $ua->prepare_request ($req); | ||
| 89 | ### IntradayHandler request: $req->as_string | ||
| 90 | |||
| 91 | my $resp = $ua->request | ||
| 92 | ($req, | ||
| 93 | sub { | ||
| 94 | my ($chunk, $resp, $protobj) = @_; | ||
| 95 | $resp->add_content($chunk); | ||
| 96 | |||
| 97 | # if the message is deflate/gzip/etc compressed then decoded_content | ||
| 98 | # returns undef until the whole image -- but don't worry about that | ||
| 99 | # until we get a server sending us compressed images | ||
| 100 | # | ||
| 101 | my $image = $resp->decoded_content(charset=>'none'); | ||
| 102 | if ($image && length $image >= 256) { | ||
| 103 | App::Chart::Intraday::write_intraday_image (symbol => $symbol, | ||
| 104 | mode => $mode, | ||
| 105 | image => $image); | ||
| 106 | } | ||
| 107 | }); | ||
| 108 | |||
| 109 | if ($resp->is_success) { | ||
| 110 | $image = $resp->decoded_content(charset=>'none',raise_error=>1); | ||
| 111 | } else { | ||
| 112 | $error = __x('Error: {status_line}', | ||
| 113 | status_line => $resp->status_line); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | require App::Chart::Intraday; | ||
| 117 | App::Chart::Intraday::write_intraday_image (symbol => $symbol, | ||
| 118 | mode => $mode, | ||
| 119 | image => $image, | ||
| 120 | resp => $resp, | ||
| 121 | error => $error); | ||
| 122 | ### response: $resp->status_line | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub name_sans_mnemonic { | ||
| 127 | my ($self) = @_; | ||
| 128 | my $name = $self->{'name'}; | ||
| 129 | $name =~ s/_//; | ||
| 130 | return $name; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub name_as_markup { | ||
| 134 | my ($self) = @_; | ||
| 135 | my $name = $self->{'name'}; | ||
| 136 | $name =~ s{_(.)}{<u>$1</u>}; | ||
| 137 | return $name; | ||
| 138 | } | ||
| 139 | sub name_mnemonic_key { | ||
| 140 | my ($self) = @_; | ||
| 141 | my $name = $self->{'name'}; | ||
| 142 | $name =~ s/__//; | ||
| 143 | return ($name =~ /_(.)/ && $1); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | __END__ |