| 1 | # Copyright 2008, 2009, 2010, 2011, 2017, 2024 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::IndivChunks; | ||
| 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::Download; | ||
| 28 | |||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($class, %options) = @_; | ||
| 32 | $options{'name'} or croak "missing name for ".__PACKAGE__; | ||
| 33 | $options{'chunk_size'} or croak "missing chunk_size for ".__PACKAGE__; | ||
| 34 | $options{'url_func'} or croak "missing url_func for ".__PACKAGE__; | ||
| 35 | |||
| 36 | return $class->SUPER::new (%options, | ||
| 37 | proc => \&indivchunks_download, | ||
| 38 | backto => \&backto, | ||
| 39 | proc_with_self => 1); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub indivchunks_download { | ||
| 43 | my ($self, $symbol_list) = @_; | ||
| 44 | my $allow_http_codes = $self->{'allow_http_codes'} // [404]; | ||
| 45 | |||
| 46 | foreach my $symbol (@$symbol_list) { | ||
| 47 | my $avail_tdate = $self->available_tdate_for_symbol ($symbol) | ||
| 48 | + ($self->{'available_tdate_extra'} || 0); | ||
| 49 | my $lo_tdate = App::Chart::Download::start_tdate_for_update ($symbol); | ||
| 50 | my $empty_count = 0; | ||
| 51 | |||
| 52 | if ($lo_tdate > $avail_tdate) { | ||
| 53 | App::Chart::Download::verbose_message | ||
| 54 | (__x("{name} nothing further expected yet for {symbol}", | ||
| 55 | name => $self->{'name'}, | ||
| 56 | symbol => $symbol)); | ||
| 57 | next; | ||
| 58 | } | ||
| 59 | |||
| 60 | while ($lo_tdate <= $avail_tdate) { | ||
| 61 | my $hi_tdate = min ($lo_tdate + $self->{'chunk_size'} - 1, $avail_tdate); | ||
| 62 | |||
| 63 | App::Chart::Download::status | ||
| 64 | ($self->{'name'}, __('data'), | ||
| 65 | $symbol, | ||
| 66 | App::Chart::Download::tdate_range_string ($lo_tdate, $hi_tdate)); | ||
| 67 | |||
| 68 | my $url = $self->{'url_func'}->($symbol, $lo_tdate, $hi_tdate); | ||
| 69 | my $resp = App::Chart::Download->get | ||
| 70 | ($url, allow_http_codes => $allow_http_codes); | ||
| 71 | |||
| 72 | my $h; | ||
| 73 | if ($resp->is_success) { | ||
| 74 | $h = $self->{'parse'}->($symbol, $resp); | ||
| 75 | } else { | ||
| 76 | $h = { data => [] }; | ||
| 77 | } | ||
| 78 | |||
| 79 | if ($hi_tdate == $avail_tdate) { | ||
| 80 | $h->{'last_download'} = 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (! @{$h->{'data'}}) { | ||
| 84 | $empty_count++; | ||
| 85 | if ($empty_count >= 2) { | ||
| 86 | $h->{'last_download'} = 1; | ||
| 87 | } | ||
| 88 | } else { | ||
| 89 | $empty_count = 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | App::Chart::Download::write_daily_group ($h); | ||
| 93 | |||
| 94 | # FIXME: This works badly for recently listed shares where | ||
| 95 | # there can be a lot of empty chunks until current. | ||
| 96 | # Maybe should download "backto" from newest to oldest. | ||
| 97 | # | ||
| 98 | # if ($empty_count >= 2) { | ||
| 99 | # App::Chart::Download::verbose_message | ||
| 100 | # (__x("{name} two empty chunks, end of data for {symbol}", | ||
| 101 | # name => $self->{'name'}, | ||
| 102 | # symbol => $symbol)); | ||
| 103 | # last; | ||
| 104 | # } | ||
| 105 | $lo_tdate = $hi_tdate + 1; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | sub backto { | ||
| 111 | my ($self, $symbol_list, $backto_tdate) = @_; | ||
| 112 | my $allow_http_codes = $self->{'allow_http_codes'} // [404]; | ||
| 113 | |||
| 114 | foreach my $symbol (@$symbol_list) { | ||
| 115 | my $hi_tdate = App::Chart::Download::start_tdate_for_backto ($symbol); | ||
| 116 | my $empty_count = 0; | ||
| 117 | |||
| 118 | while ($hi_tdate >= $backto_tdate) { | ||
| 119 | my $lo_tdate = $hi_tdate - $self->{'chunk_size'} + 1; | ||
| 120 | |||
| 121 | App::Chart::Download::status | ||
| 122 | ($self->{'name'}, __('data'), | ||
| 123 | App::Chart::Download::tdate_range_string ($lo_tdate, $hi_tdate)); | ||
| 124 | |||
| 125 | my $url = $self->{'url_func'}->($symbol, $lo_tdate, $hi_tdate); | ||
| 126 | my $resp = App::Chart::Download->get | ||
| 127 | ($url, allow_http_codes => $allow_http_codes); | ||
| 128 | |||
| 129 | my $h; | ||
| 130 | if ($resp->is_success) { | ||
| 131 | $h = $self->{'parse'}->($symbol, $resp); | ||
| 132 | } else { | ||
| 133 | $h = { data => [] }; | ||
| 134 | } | ||
| 135 | |||
| 136 | if (! @{$h->{'data'}}) { | ||
| 137 | $empty_count++; | ||
| 138 | if ($empty_count >= 2) { | ||
| 139 | App::Chart::Download::verbose_message | ||
| 140 | (__x("{name} apparent limit of data for {symbol}", | ||
| 141 | name => $self->{'name'}, | ||
| 142 | symbol => $symbol)); | ||
| 143 | last; | ||
| 144 | } | ||
| 145 | } else { | ||
| 146 | $empty_count = 0; | ||
| 147 | } | ||
| 148 | App::Chart::Download::write_daily_group ($h); | ||
| 149 | |||
| 150 | $hi_tdate = $lo_tdate - 1; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; | ||
| 156 | __END__ |