App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Suffix/CAS.pm  view on Meta::CPAN

# (define (casablanca-quote-adate-time symbol)
#   (tm->adate-time-within (localtime (- (current-time) #,(hms->seconds 0 15 0))
# 				    (timezone-casablanca))
# 			 #,(hms->seconds 9 0 0)
# 			 #,(hms->seconds 15 30 0)))


#-----------------------------------------------------------------------------
# download - dividends
#
# This uses the Market Transaction (OST in French) pages like
#
#     http://www.casablanca-bourse.com/fiches/valeurs/MNG/fr/ost.html
#
# The english pages for these are in-progress, apparently, as of Jan 2007,
# so the French pages are used.
#
# The server provides an ETag and Last-Modified which we use to avoid
# getting another copy of data we've already seen and processed.  The
# download is about 11k.

App::Chart::DownloadHandler->new
  (pred         => $pred,
   proc         => \&dividends_download,
   max_symbols  => 1,
   recheck_key  => 'CAS-dividends',
   recheck_days => 7,
   # low priority to get prices first
   priority     => -10);

sub dividends_download {
  my ($symbol_list) = @_;
  my $symbol = $symbol_list->[0];

  my $url = 'http://www.casablanca-bourse.com/fiches/valeurs/'
    . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol))
      . '/fr/ost.html';
  App::Chart::Download::status (__x('Casablanca dividends {symbol}',
                                   symbol => $symbol));
  my $resp = App::Chart::Download->get ($url,
                                       symbol => $symbol,
                                       url_tags_key => 'CAS-dividends');
  my $h = dividends_parse ($symbol, $resp);

  App::Chart::Download::write_daily_group ($h);
}

sub dividends_parse {
  my ($symbol, $resp) = @_;

  my @dividends = ();
  my $h = { source       => __PACKAGE__,
            resp         => $resp,
            url_tags_key => 'CAS-dividends',
            currency     => 'MAD',
            date_format  => 'dmy', # eg. "26.05.00"
            dividends    => \@dividends };
  if (! $resp->is_success) {
    return $h;
  }
  my $content = $resp->decoded_content (raise_error=>1);

  require HTML::TableExtract;
  my $te = HTML::TableExtract->new
    (headers => [ qr/Dividend.*Brut/is,
                  # qr/Num.*Coupon/is,  # coupon number
                  qr/tachement.*la.*Bourse/is ]);
  $te->parse($content);
  if (! $te->tables) {
    die 'Casablanca dividends table not matched';
  }

  foreach my $row ($te->rows) {
    my ($amount, $ex_date) = @$row;

    # "NEANT" (nothing) when current year has no dividends yet
    if ($ex_date =~ /NEANT/i) { next; }

    # amount with comma like "22,00"
    $amount =~ s/,/./g;

    push @dividends, { symbol  => $symbol,
                       amount  => $amount,
                       ex_date => $ex_date,
                     };
  }
  return $h;
}

1;
__END__



( run in 2.548 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )