| 1 | # Copyright 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::DownloadHandler::IndivInfo; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | # use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use base 'App::Chart::DownloadHandler'; | ||
| 26 | use App::Chart; | ||
| 27 | use App::Chart::Database; | ||
| 28 | use App::Chart::DBI; | ||
| 29 | |||
| 30 | |||
| 31 | use constant DEFAULT_INFO_CHECK_DAYS => 10; | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my ($class, %options) = @_; | ||
| 35 | $options{'name'} or croak "missing name for ".__PACKAGE__; | ||
| 36 | $options{'key'} or croak "missing key for ".__PACKAGE__; | ||
| 37 | $options{'url_func'} or croak "missing chunk_size for ".__PACKAGE__; | ||
| 38 | $options{'recheck_days'} ||= DEFAULT_INFO_CHECK_DAYS; | ||
| 39 | |||
| 40 | return $class->SUPER::new (%options, | ||
| 41 | proc => \&indivinfo_download, | ||
| 42 | proc_with_self => 1, | ||
| 43 | recheck_key => $options{'key'} . '-timestamp', | ||
| 44 | max_symbols => 1); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub indivinfo_download { | ||
| 48 | my ($self, $symbol_list) = @_; | ||
| 49 | my $symbol = $symbol_list->[0]; | ||
| 50 | |||
| 51 | my $dbh = App::Chart::DBI->instance; | ||
| 52 | my $recheck_key = $self->{'recheck_key'}; | ||
| 53 | my $timestamp = App::Chart::Download::timestamp_now(); | ||
| 54 | |||
| 55 | App::Chart::Download::status ($self->{'name'}, $symbol); | ||
| 56 | |||
| 57 | my $url = $self->{'url_func'}->($symbol); | ||
| 58 | my $resp = App::Chart::Download->get ($url, allow_404 => 1); | ||
| 59 | |||
| 60 | my $h; | ||
| 61 | if ($resp->is_success) { | ||
| 62 | $h = $self->{'parse'}->($symbol, $resp); | ||
| 63 | } else { | ||
| 64 | $h = {}; | ||
| 65 | } | ||
| 66 | my $changed = 0; | ||
| 67 | App::Chart::Database::call_with_transaction | ||
| 68 | ($dbh, sub { | ||
| 69 | if (exists $h->{'name'}) { | ||
| 70 | App::Chart::Download::set_symbol_name ($symbol, $h->{'name'}); | ||
| 71 | $changed = 1; | ||
| 72 | } | ||
| 73 | if (exists $h->{'currency'}) { | ||
| 74 | App::Chart::Download::set_currency ($symbol, $h->{'currency'}); | ||
| 75 | $changed = 1; | ||
| 76 | } | ||
| 77 | App::Chart::Database->write_extra | ||
| 78 | ($symbol, $recheck_key, $timestamp); | ||
| 79 | }); | ||
| 80 | if ($changed) { | ||
| 81 | App::Chart::chart_dirbroadcast()->send ('data-changed', { $symbol => 1 }); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | __END__ | ||
| 87 | |||
| 88 | # =head1 NAME | ||
| 89 | # | ||
| 90 | # App::Chart::DownloadHandler::IndivInfo -- individual symbols in date range chunks | ||
| 91 | # | ||
| 92 | # =for test_synopsis my ($pred) | ||
| 93 | # | ||
| 94 | # =head1 SYNOPSIS | ||
| 95 | # | ||
| 96 | # use App::Chart::DownloadHandler::IndivInfo; | ||
| 97 | # App::Chart::DownloadHandler::IndivInfo->new | ||
| 98 | # (name => __('FooEX'), | ||
| 99 | # pred => $pred, | ||
| 100 | # url_func => \&my_url, | ||
| 101 | # parse => \&my_page_parser); | ||
| 102 | # | ||
| 103 | # =head1 DESCRIPTION | ||
| 104 | # | ||
| 105 | # This module downloads and processes daily data in date range chunks for one | ||
| 106 | # symbol at a time. This is the style required for Yahoo Finance for | ||
| 107 | # instance. | ||
| 108 | # | ||
| 109 | # =head1 SEE ALSO | ||
| 110 | # | ||
| 111 | # L<App::Chart::DownloadHandler> | ||
| 112 | # | ||
| 113 | # =cut |