| 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__ | ||
| 148 | |||
| 149 | # =for stopwords intraday | ||
| 150 | # | ||
| 151 | # =head1 NAME | ||
| 152 | # | ||
| 153 | # App::Chart::IntradayHandler -- intraday download handlers | ||
| 154 | # | ||
| 155 | # =for test_synopsis my (@handlers) | ||
| 156 | # | ||
| 157 | # =head1 SYNOPSIS | ||
| 158 | # | ||
| 159 | # use App::Chart::IntradayHandler; | ||
| 160 | # | ||
| 161 | # # register new | ||
| 162 | # App::Chart::IntradayHandler->new (); | ||
| 163 | # | ||
| 164 | # # find | ||
| 165 | # @handlers = App::Chart::IntradayHandler->handlers_for_symbol ('GM'); | ||
| 166 | # | ||
| 167 | # =head1 FUNCTIONS | ||
| 168 | # | ||
| 169 | # =over 4 | ||
| 170 | # | ||
| 171 | # =item C<< App::Chart::IntradayHandler->new (...) >> | ||
| 172 | # | ||
| 173 | # Create and register a new intraday image handler. The return is a new | ||
| 174 | # C<App::Chart::IntradayHandler> object, though usually this is not of interest | ||
| 175 | # (only all the handlers later with C<handlers_for_symbol> below). | ||
| 176 | # | ||
| 177 | # my $pred = App::Chart::Sympred::Suffix->new ('.NZ'); | ||
| 178 | # | ||
| 179 | # sub intraday_url { | ||
| 180 | # my ($self, $symbol, $mode) = @_; | ||
| 181 | # return 'http://ichart.finance.yahoo.com/z?s=' | ||
| 182 | # . URI::Escape::uri_escape ($symbol) | ||
| 183 | # . '&t=' . $mode | ||
| 184 | # . '&l=off&z=m&q=l&a=v'; | ||
| 185 | # } | ||
| 186 | # | ||
| 187 | # App::Chart::IntradayHandler->new | ||
| 188 | # (pred => $pred, | ||
| 189 | # proc => \&intraday_url, | ||
| 190 | # mode => '1d', | ||
| 191 | # name => '1 Day'); | ||
| 192 | # | ||
| 193 | # =item C<< @handler_list = App::Chart::IntradayHandler->handlers_for_symbol ($symbol) >> | ||
| 194 | # | ||
| 195 | # Return a list of C<App::Chart::IntradayHandler> objects which are available | ||
| 196 | # for use with C<$symbol>. This is an empty list if there's nothing available. | ||
| 197 | # | ||
| 198 | # =item C<< $handler = App::Chart::IntradayHandler->handler_for_symbol_and_mode ($symbol, $mode) >> | ||
| 199 | # | ||
| 200 | # Return a C<App::Chart::IntradayHandler> object for use on the given | ||
| 201 | # C<$symbol> and C<$mode>. C<$mode> is a string matched against the C<mode> | ||
| 202 | # specified in the handlers available for C<$symbol>. | ||
| 203 | # | ||
| 204 | # =item $handler->download ($symbol) | ||
| 205 | # | ||
| 206 | # Do a download on C<$handler> for C<$symbol>. The result is written to the | ||
| 207 | # database. | ||
| 208 | # | ||
| 209 | # =back | ||
| 210 | # | ||
| 211 | # =head1 SEE ALSO | ||
| 212 | # | ||
| 213 | # L<App::Chart::Intraday> | ||
| 214 | # | ||
| 215 | # =cut |