| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai expandtab ic showmode showmatch: |
| 3 |
# |
| 4 |
# Copyright (C) 2023, Bruce Schuck <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
# 02110-1301, USA |
| 20 |
# |
| 21 |
# Written as a replacement for Tradeville.pm. |
| 22 |
|
| 23 |
package Finance::Quote::BVB; |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
use Encode qw(decode); |
| 29 |
use HTTP::Request::Common; |
| 30 |
use HTML::TreeBuilder; |
| 31 |
use HTML::TableExtract; |
| 32 |
|
| 33 |
use constant DEBUG => $ENV{DEBUG}; |
| 34 |
use if DEBUG, 'Smart::Comments', '###'; |
| 35 |
|
| 36 |
our $VERSION = '1.71'; # VERSION |
| 37 |
|
| 38 |
my $BVB_URL = 'https://bvb.ro/FinancialInstruments/Details/FinancialInstrumentsDetails.aspx?s='; |
| 39 |
|
| 40 |
our $DISPLAY = 'BVB - Bucharest Stock Exchange'; |
| 41 |
our @LABELS = |
| 42 |
qw/symbol name open high low last bid ask date currency method/; |
| 43 |
our $METHODHASH = {subroutine => \&bvb, |
| 44 |
display => $DISPLAY, |
| 45 |
labels => \@LABELS}; |
| 46 |
|
| 47 |
sub methodinfo { |
| 48 |
return ( |
| 49 |
bvb => $METHODHASH, |
| 50 |
romania => $METHODHASH, |
| 51 |
tradeville => $METHODHASH, |
| 52 |
europe => $METHODHASH, |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
sub labels { |
| 57 |
my %m = methodinfo(); |
| 58 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 59 |
} |
| 60 |
|
| 61 |
sub methods { |
| 62 |
my %m = methodinfo(); |
| 63 |
return map {$_ => $m{$_}{subroutine} } keys %m; |
| 64 |
} |
| 65 |
|
| 66 |
sub bvb { |
| 67 |
|
| 68 |
my $quoter = shift; |
| 69 |
my @stocks = @_; |
| 70 |
my (%info, $tree, $table, $pricetable, $url, $reply); |
| 71 |
my $ua = $quoter->user_agent(); |
| 72 |
|
| 73 |
my @ua_headers = ( |
| 74 |
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36', |
| 75 |
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', |
| 76 |
); |
| 77 |
|
| 78 |
|
| 79 |
foreach my $stock (@stocks) { |
| 80 |
|
| 81 |
$url = $BVB_URL . $stock; |
| 82 |
$reply = $ua->get($url, @ua_headers); |
| 83 |
|
| 84 |
my $code = $reply->code; |
| 85 |
my $desc = HTTP::Status::status_message($code); |
| 86 |
my $headers = $reply->headers_as_string; |
| 87 |
my $body = decode('UTF-8', $reply->content); |
| 88 |
|
| 89 |
### Body: $body |
| 90 |
|
| 91 |
my ($name, $bid, $ask, $last, $open, $high, $low, $date); |
| 92 |
|
| 93 |
$info{ $stock, "symbol" } = $stock; |
| 94 |
|
| 95 |
if ( $code == 200 ) { |
| 96 |
|
| 97 |
# Use HTML::TreeBuilder to parse HTML in $body |
| 98 |
$tree = HTML::TreeBuilder->new; |
| 99 |
if ($tree->parse($body)) { |
| 100 |
|
| 101 |
$tree->eof; |
| 102 |
### [<now>] Tree: $tree |
| 103 |
if ( $tree->look_down(_tag => 'div', id => 'ctl00_body_divNoData') ) { |
| 104 |
$info{ $stock, "success" } = 0; |
| 105 |
$info{ $stock, "errormsg" } = |
| 106 |
"Error retrieving quote for $stock. No data returned"; |
| 107 |
next; |
| 108 |
} |
| 109 |
$name = $tree->look_down(_tag => 'h2', class => qr/^mBot0 large textStyled/)->as_text; |
| 110 |
$info{ $stock, 'success' } = 1; |
| 111 |
($info{ $stock, 'name' } = $name) =~ s/^\s+|\s+$//g ; |
| 112 |
$info{ $stock, 'currency' } = 'RON'; |
| 113 |
$info{ $stock, 'method' } = 'bvb'; |
| 114 |
$table = $tree->look_down(_tag => 'table', id => qr/^ctl00_body_ctl02_PricesControl_dvCPrices/)->as_HTML; |
| 115 |
$pricetable = HTML::TableExtract->new(); |
| 116 |
$pricetable->parse($table); |
| 117 |
foreach my $row ($pricetable->rows) { |
| 118 |
if ( @$row[0] =~ m|Ask$| ) { |
| 119 |
### [<now>] Big Ask Row: $row |
| 120 |
($bid, $ask) = @$row[1] =~ m|^\s*([\d\.,]+)\s+\/\s+([\d\.,]+)|; |
| 121 |
$info{ $stock, 'bid' } = $bid =~ s|,|.|r; |
| 122 |
$info{ $stock, 'ask' } = $ask =~ s|,|.|r; |
| 123 |
} |
| 124 |
elsif ( @$row[0] =~ m|^Data/ora| ) { |
| 125 |
($date) = @$row[1] =~ m|^([\d\.,/]+)\s|; |
| 126 |
$quoter->store_date(\%info, $stock, {eurodate => $1}) if $date =~ m|([0-9]{1,2}.[0-9]{1,2}.[0-9]{4})|; |
| 127 |
} |
| 128 |
elsif ( @$row[0] =~ m|^Ultimul pret| ) { |
| 129 |
($last) = @$row[1] =~ m|^([\d\.,]+)|; |
| 130 |
$info{ $stock, 'last' } = $last =~ s|,|.|r; |
| 131 |
} |
| 132 |
elsif ( @$row[0] =~ m|^Pret deschidere| ) { |
| 133 |
($open) = @$row[1] =~ m|^([\d\.,]+)|; |
| 134 |
$info{ $stock, 'open' } = $open =~ s|,|.|r; |
| 135 |
} |
| 136 |
elsif ( @$row[0] =~ m|^Pret maxim| ) { |
| 137 |
($high) = @$row[1] =~ m|^([\d\.,]+)|; |
| 138 |
$info{ $stock, 'high' } = $high =~ s|,|.|r; |
| 139 |
} |
| 140 |
elsif ( @$row[0] =~ m|^Pret minim| ) { |
| 141 |
($low) = @$row[1] =~ m|^([\d\.,]+)|; |
| 142 |
$info{ $stock, 'low' } = $low =~ s|,|.|r; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
} else { |
| 147 |
$tree->eof; |
| 148 |
$info{ $stock, "success" } = 0; |
| 149 |
$info{ $stock, "errormsg" } = |
| 150 |
"Error retrieving quote for $stock. Could not parse HTML returned from $url."; |
| 151 |
} |
| 152 |
|
| 153 |
} else { # HTTP Request failed (code != 200) |
| 154 |
$info{ $stock, "success" } = 0; |
| 155 |
$info{ $stock, "errormsg" } = |
| 156 |
"Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
return wantarray() ? %info : \%info; |
| 162 |
return \%info; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
1; |
| 167 |
|
| 168 |
__END__ |
| 169 |
|
| 170 |
=head1 NAME |
| 171 |
|
| 172 |
Finance::Quote::BVB - Obtain quotes from Bucharest Stock Exchange. |
| 173 |
|
| 174 |
=head1 SYNOPSIS |
| 175 |
|
| 176 |
use Finance::Quote; |
| 177 |
|
| 178 |
$q = Finance::Quote->new; |
| 179 |
|
| 180 |
%info = $q->fetch("bvb", "tlv"); # Only query bvb |
| 181 |
|
| 182 |
%info = $q->fetch("romania", "brd"); # Failover to other sources OK. |
| 183 |
|
| 184 |
=head1 DESCRIPTION |
| 185 |
|
| 186 |
This module fetches information from L<https://bvb.ro/>. |
| 187 |
|
| 188 |
This module is loaded by default on a Finance::Quote object. It's also possible |
| 189 |
to load it explicitly by placing "bvb" in the argument list to |
| 190 |
Finance::Quote->new(). |
| 191 |
|
| 192 |
This module provides "bvb", "tradeville", "romania", and "europe" |
| 193 |
fetch methods. It was written to replace a non-working Tradeville.pm |
| 194 |
module. |
| 195 |
|
| 196 |
Information obtained by this module may be covered by Bucharest Stock |
| 197 |
Exchange terms and conditions. |
| 198 |
|
| 199 |
=head1 LABELS RETURNED |
| 200 |
|
| 201 |
The following labels are returned: |
| 202 |
|
| 203 |
=over |
| 204 |
|
| 205 |
=item * |
| 206 |
|
| 207 |
name |
| 208 |
|
| 209 |
=item * |
| 210 |
|
| 211 |
symbol |
| 212 |
|
| 213 |
=item * |
| 214 |
|
| 215 |
open |
| 216 |
|
| 217 |
=item * |
| 218 |
|
| 219 |
high |
| 220 |
|
| 221 |
=item * |
| 222 |
|
| 223 |
low |
| 224 |
|
| 225 |
=item * |
| 226 |
|
| 227 |
price |
| 228 |
|
| 229 |
=item * |
| 230 |
|
| 231 |
bid |
| 232 |
|
| 233 |
=item * |
| 234 |
|
| 235 |
ask |
| 236 |
|
| 237 |
=item * |
| 238 |
|
| 239 |
date |
| 240 |
|
| 241 |
=item * |
| 242 |
|
| 243 |
currency (always RON) |
| 244 |
|
| 245 |
=back |