Finance-Quote-Grab
view release on metacpan or search on metacpan
lib/Finance/Quote/Ghana.pm view on Meta::CPAN
# Copyright 2014, 2015, 2016, 2019 Kevin Ryde
# This file is part of Finance-Quote-Grab.
#
# Finance-Quote-Grab is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# Finance-Quote-Grab is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Finance-Quote-Grab. If not, see <http://www.gnu.org/licenses/>.
package Finance::Quote::Ghana;
use 5.004;
use strict;
use Carp;
# uncomment this to run the ### lines
# use Smart::Comments;
use vars '$VERSION';
$VERSION = 15;
use constant GHANA_MARKET_URL =>
'https://gse.com.gh/daily-shares-and-etfs-trades/';
sub methods {
return (ghana => \&ghana_quotes);
}
sub labels {
return (ghana => [ qw(date isodate
isin year_range
open net bid ask volume last
currency
method source success errormsg
) ]);
}
sub ghana_quotes {
my ($fq, @symbol_list) = @_;
if (! @symbol_list) { return; }
# The gse server (an Apache) will serve gzipped, so accept_decodable()
# here saves some data transmission.
#
# user_agent() is set only to the package. Would normally include the
# libwww-perl identifier too, but some bad setup on the gse server gives
# "406 Not acceptable" if more than one identifier.
#
require HTTP::Request;
my $req = HTTP::Request->new ('GET', GHANA_MARKET_URL);
$req->accept_decodable; # using decoded_content() below
$req->user_agent (__PACKAGE__."/".$VERSION);
### req: $req->as_string
my $ua = $fq->user_agent;
my $resp = $ua->request ($req);
### resp headers: $resp->headers->as_string
my %quotes;
_parse ($fq, $resp, \%quotes, \@symbol_list);
return wantarray() ? %quotes : \%quotes;
}
sub _parse {
my ($fq, $resp, $quotes, $symbol_list) = @_;
foreach my $symbol (@$symbol_list) {
$quotes->{$symbol,'method'} = 'ghana';
$quotes->{$symbol,'source'} = __PACKAGE__;
$quotes->{$symbol,'success'} = 0;
}
if (! $resp->is_success) {
_errormsg ($quotes, $symbol_list, $resp->status_line);
return;
}
my $content = $resp->decoded_content (raise_error => 1, charset => 'none');
# Eg. <span class="glyphicon glyphicon-calendar"></span> February 22, 2019</div>
$content =~ m{glyphicon-calendar.*?([a-z]+) (\d{1,2}), (\d\d\d\d)}i
or die "GSE: daily page cannot find trade date\n";
my $date = "$1/$2/$3"; # "October/03/2014"
require HTML::TableExtract;
my $te = HTML::TableExtract->new
(headers => [ # qr/Session/i,
qr/ISIN/i,
qr/Share Code/i,
qr/Year High/i,
qr/Year Low/i,
qr/Previous Closing Price VWAP/i,
qr/Opening/i,
qr/Closing Price VWAP/i,
qr/Price Change/i,
qr/Closing Bid Price/i,
qr/Closing Offer Price/i,
qr/Total Shares Traded/i,
qr/Last Transaction Price/i,
]);
$te->parse($content);
my $ts = $te->first_table_found;
if (! $ts) {
_errormsg ($quotes, $symbol_list, 'rates table not found in HTML');
return;
}
my %want_symbol;
@want_symbol{@$symbol_list} = (); # hash slice
my %seen_symbol;
foreach my $row (@{$ts->rows()}) {
### $row
my ($isin, $symbol, $year_high, $year_low,
$previous_vwap, $open, $close_vwap, $change,
$close_bid, $close_offer, $volume, $last)
= @$row;
if (! exists $want_symbol{$symbol}) { next; } # unwanted row
# volume is for example "58,600"
# strip the commas
$volume =~ tr/,//d;
# volume has been seen as "0.00", eg. from GLD
# prefer to return it as an integer
$volume =~ s/\.0*$//;
$fq->store_date($quotes, $symbol, {usdate => $date});
$quotes->{$symbol,'isin'} = $isin;
$quotes->{$symbol,'year_range'} = "$year_high-$year_low";
$quotes->{$symbol,'open'} = $open;
$quotes->{$symbol,'net'} = $change;
$quotes->{$symbol,'bid'} = $close_bid if defined $close_bid;
$quotes->{$symbol,'ask'} = $close_offer if defined $close_offer;
$quotes->{$symbol,'volume'} = $volume;
$quotes->{$symbol,'last'} = $last;
$quotes->{$symbol,'currency'} = 'GHS';
( run in 1.739 second using v1.01-cache-2.11-cpan-ceb78f64989 )