| 1 | # Casablanca Stock Exchange setups. | ||
| 2 | |||
| 3 | # Copyright 2005, 2006, 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::Suffix::CAS; | ||
| 20 | use 5.006; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use URI::Escape; | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 27 | use App::Chart; | ||
| 28 | use App::Chart::Download; | ||
| 29 | use App::Chart::FinanceQuote; | ||
| 30 | use App::Chart::Sympred; | ||
| 31 | use App::Chart::TZ; | ||
| 32 | use App::Chart::Weblink; | ||
| 33 | use App::Chart::Yahoo; | ||
| 34 | |||
| 35 | |||
| 36 | # Casablanca post 1978 is GMT with no daylight savings, so | ||
| 37 | # Time::TZ->tz_known() can't tell if it's ok | ||
| 38 | my $timezone_casablanca = App::Chart::TZ->new | ||
| 39 | (name => __('Casablanca'), | ||
| 40 | tz => 'Africa/Casablanca'); | ||
| 41 | |||
| 42 | my $pred = App::Chart::Sympred::Suffix->new ('.CAS'); | ||
| 43 | $timezone_casablanca->setup_for_symbol ($pred); | ||
| 44 | App::Chart::setup_source_help | ||
| 45 | ($pred, __p('manual-node','Casablanca Stock Exchange')); | ||
| 46 | |||
| 47 | |||
| 48 | #------------------------------------------------------------------------------ | ||
| 49 | # weblink - company info | ||
| 50 | # | ||
| 51 | # French or English | ||
| 52 | # http://www.casablanca-bourse.com/cgi/ASP/Fiches/fiche.asp?TICKER=NEJ | ||
| 53 | # http://www.casablanca-bourse.com/cgi/ASP/Fiches/anglais/fiche.asp?TICKER=NEJ | ||
| 54 | |||
| 55 | App::Chart::Weblink->new | ||
| 56 | (pred => $pred, | ||
| 57 | name => __('Casablanca _Company Information'), | ||
| 58 | desc => __('Open web browser at the Casablanca Stock Exchange page for this company'), | ||
| 59 | proc => sub { | ||
| 60 | my ($symbol) = @_; | ||
| 61 | $symbol = URI::Escape::uri_escape(App::Chart::symbol_sans_suffix($symbol)); | ||
| 62 | my $lang = App::Chart::Glib::Ex::MoreUtils::lang_select (fr => '', | ||
| 63 | en => '/anglais'); | ||
| 64 | return "http://www.casablanca-bourse.com/cgi/ASP/Fiches$lang/fiche.asp?TICKER=$symbol"; | ||
| 65 | }); | ||
| 66 | |||
| 67 | |||
| 68 | #----------------------------------------------------------------------------- | ||
| 69 | # latest quotes, using Finance::Quote::Casablanca | ||
| 70 | |||
| 71 | App::Chart::FinanceQuote->setup (pred => $pred, | ||
| 72 | suffix => '.CAS', | ||
| 73 | modules => ['Casablanca'], | ||
| 74 | method => 'casablanca', | ||
| 75 | max_symbols => 1); | ||
| 76 | |||
| 77 | # ;; return current Casablanca adate/time, with 15-minute delay | ||
| 78 | # ;; pre-open 9:00am through to closure 15:30 | ||
| 79 | # ;; | ||
| 80 | # (define (casablanca-quote-adate-time symbol) | ||
| 81 | # (tm->adate-time-within (localtime (- (current-time) #,(hms->seconds 0 15 0)) | ||
| 82 | # (timezone-casablanca)) | ||
| 83 | # #,(hms->seconds 9 0 0) | ||
| 84 | # #,(hms->seconds 15 30 0))) | ||
| 85 | |||
| 86 | |||
| 87 | #----------------------------------------------------------------------------- | ||
| 88 | # download - dividends | ||
| 89 | # | ||
| 90 | # This uses the Market Transaction (OST in French) pages like | ||
| 91 | # | ||
| 92 | # http://www.casablanca-bourse.com/fiches/valeurs/MNG/fr/ost.html | ||
| 93 | # | ||
| 94 | # The english pages for these are in-progress, apparently, as of Jan 2007, | ||
| 95 | # so the French pages are used. | ||
| 96 | # | ||
| 97 | # The server provides an ETag and Last-Modified which we use to avoid | ||
| 98 | # getting another copy of data we've already seen and processed. The | ||
| 99 | # download is about 11k. | ||
| 100 | |||
| 101 | App::Chart::DownloadHandler->new | ||
| 102 | (pred => $pred, | ||
| 103 | proc => \÷nds_download, | ||
| 104 | max_symbols => 1, | ||
| 105 | recheck_key => 'CAS-dividends', | ||
| 106 | recheck_days => 7, | ||
| 107 | # low priority to get prices first | ||
| 108 | priority => -10); | ||
| 109 | |||
| 110 | sub dividends_download { | ||
| 111 | my ($symbol_list) = @_; | ||
| 112 | my $symbol = $symbol_list->[0]; | ||
| 113 | |||
| 114 | my $url = 'http://www.casablanca-bourse.com/fiches/valeurs/' | ||
| 115 | . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol)) | ||
| 116 | . '/fr/ost.html'; | ||
| 117 | App::Chart::Download::status (__x('Casablanca dividends {symbol}', | ||
| 118 | symbol => $symbol)); | ||
| 119 | my $resp = App::Chart::Download->get ($url, | ||
| 120 | symbol => $symbol, | ||
| 121 | url_tags_key => 'CAS-dividends'); | ||
| 122 | my $h = dividends_parse ($symbol, $resp); | ||
| 123 | |||
| 124 | App::Chart::Download::write_daily_group ($h); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub dividends_parse { | ||
| 128 | my ($symbol, $resp) = @_; | ||
| 129 | |||
| 130 | my @dividends = (); | ||
| 131 | my $h = { source => __PACKAGE__, | ||
| 132 | resp => $resp, | ||
| 133 | url_tags_key => 'CAS-dividends', | ||
| 134 | currency => 'MAD', | ||
| 135 | date_format => 'dmy', # eg. "26.05.00" | ||
| 136 | dividends => \@dividends }; | ||
| 137 | if (! $resp->is_success) { | ||
| 138 | return $h; | ||
| 139 | } | ||
| 140 | my $content = $resp->decoded_content (raise_error=>1); | ||
| 141 | |||
| 142 | require HTML::TableExtract; | ||
| 143 | my $te = HTML::TableExtract->new | ||
| 144 | (headers => [ qr/Dividend.*Brut/is, | ||
| 145 | # qr/Num.*Coupon/is, # coupon number | ||
| 146 | qr/tachement.*la.*Bourse/is ]); | ||
| 147 | $te->parse($content); | ||
| 148 | if (! $te->tables) { | ||
| 149 | die 'Casablanca dividends table not matched'; | ||
| 150 | } | ||
| 151 | |||
| 152 | foreach my $row ($te->rows) { | ||
| 153 | my ($amount, $ex_date) = @$row; | ||
| 154 | |||
| 155 | # "NEANT" (nothing) when current year has no dividends yet | ||
| 156 | if ($ex_date =~ /NEANT/i) { next; } | ||
| 157 | |||
| 158 | # amount with comma like "22,00" | ||
| 159 | $amount =~ s/,/./g; | ||
| 160 | |||
| 161 | push @dividends, { symbol => $symbol, | ||
| 162 | amount => $amount, | ||
| 163 | ex_date => $ex_date, | ||
| 164 | }; | ||
| 165 | } | ||
| 166 | return $h; | ||
| 167 | } | ||
| 168 | |||
| 169 | 1; | ||
| 170 | __END__ |