| 1 | # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Intraday; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | use App::Chart; | ||
| 23 | |||
| 24 | # uncomment this to run the ### lines | ||
| 25 | #use Smart::Comments; | ||
| 26 | |||
| 27 | sub command_line_download { | ||
| 28 | my ($class, $args) = @_; | ||
| 29 | |||
| 30 | ## no critic (ProhibitExit, ProhibitExitInSubroutines) | ||
| 31 | my ($symbol, $mode) = @$args; | ||
| 32 | if (@$args != 2 || ref($symbol) || ref($mode)) { | ||
| 33 | print "Expect symbol and mode arguments for --intraday\n"; | ||
| 34 | exit 1; | ||
| 35 | } | ||
| 36 | require App::Chart::IntradayHandler; | ||
| 37 | my $handler = App::Chart::IntradayHandler->handler_for_symbol_and_mode | ||
| 38 | ($symbol, $mode); | ||
| 39 | if (! $handler) { | ||
| 40 | print "No intraday handler for symbol \"$symbol\" and mode \"$mode\"\n"; | ||
| 41 | exit 1; | ||
| 42 | } | ||
| 43 | $handler->download ($symbol); | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | #------------------------------------------------------------------------------ | ||
| 48 | |||
| 49 | sub write_intraday_image { | ||
| 50 | my %args = @_; | ||
| 51 | |||
| 52 | require App::Chart::DBI; | ||
| 53 | my $dbh = App::Chart::DBI->instance; | ||
| 54 | my $sth = $dbh->prepare_cached | ||
| 55 | ('INSERT OR REPLACE INTO intraday_image (symbol, mode, image, error, fetch_timestamp, url, etag, last_modified) | ||
| 56 | VALUES (?,?,?,?,?,?,?,?)'); | ||
| 57 | |||
| 58 | my $image = $args{'image'}; | ||
| 59 | if (defined $image && length($image) == 0) { $image = undef; } | ||
| 60 | my $resp = $args{'resp'}; | ||
| 61 | my $etag = (defined $resp ? scalar $resp->header('ETag') : undef); | ||
| 62 | my $last_modified = (defined $resp ? $resp->last_modified : undef); | ||
| 63 | |||
| 64 | my $symbol = $args{'symbol'}; | ||
| 65 | my $mode = $args{'mode'}; | ||
| 66 | $sth->bind_param (1, $symbol); | ||
| 67 | $sth->bind_param (2, $mode); | ||
| 68 | $sth->bind_param (3, $image, DBI::SQL_BLOB()); | ||
| 69 | $sth->bind_param (4, $args{'error'}); | ||
| 70 | $sth->bind_param (5, App::Chart::Download::timestamp_now()); | ||
| 71 | $sth->bind_param (6, $args{'url'}); | ||
| 72 | $sth->bind_param (7, $etag); | ||
| 73 | $sth->bind_param (8, $last_modified); | ||
| 74 | $sth->execute; | ||
| 75 | $sth->finish; | ||
| 76 | |||
| 77 | ### send intraday-changed | ||
| 78 | ### $symbol | ||
| 79 | ### $mode | ||
| 80 | App::Chart::chart_dirbroadcast()->send ('intraday-changed', $symbol, $mode); | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | __END__ |