| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||||
| 2 | # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch: | ||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2023, Bruce Schuck <bschuck@asgard-systems.com> | ||||||||||||||||||||||||||||||
| 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 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use HTML::TreeBuilder; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use HTML::TableExtract; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use if DEBUG, 'Smart::Comments', '###'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | 5 | our $VERSION = '1.72_02'; # TRIAL VERSION | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | 5 | my $BVB_URL = 'https://bvb.ro/FinancialInstruments/Details/FinancialInstrumentsDetails.aspx?s='; | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 5 | our $DISPLAY = 'BVB - Bucharest Stock Exchange'; | |||||||||||||||||||||||||||||
| 41 | 5 | our @LABELS = | |||||||||||||||||||||||||||||
| 42 | qw/symbol name open high low last bid ask date currency method/; | ||||||||||||||||||||||||||||||
| 43 | 5 | our $METHODHASH = {subroutine => \&bvb, | |||||||||||||||||||||||||||||
| 44 | display => $DISPLAY, | ||||||||||||||||||||||||||||||
| 45 | labels => \@LABELS}; | ||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 10 | ▶ | sub methodinfo { | ||||||||||||||||||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 48 | return ( | ||||||||||||||||||||||||||||||
| 49 | 10 | bvb => $METHODHASH, | |||||||||||||||||||||||||||||
| 50 | romania => $METHODHASH, | ||||||||||||||||||||||||||||||
| 51 | tradeville => $METHODHASH, | ||||||||||||||||||||||||||||||
| 52 | europe => $METHODHASH, | ||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | 5 | ▶ | sub labels { | ||||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 57 | 5 | my %m = methodinfo(); | |||||||||||||||||||||||||||||
| 58 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | 5 | ▶ | sub methods { | ||||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 62 | 5 | my %m = methodinfo(); | |||||||||||||||||||||||||||||
| 63 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub bvb { | ||||||||||||||||||||||||||||
|
Subroutine
bvb: not called CC=15 SCAR=54.8
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||
| 68 | 0 | my $quoter = shift; | |||||||||||||||||||||||||||||
| 69 | 0 | my @stocks = @_; | |||||||||||||||||||||||||||||
| 70 | 0 | my (%info, $tree, $table, $pricetable, $url, $reply); | |||||||||||||||||||||||||||||
| 71 | 0 | my $ua = $quoter->user_agent(); | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | 0 | 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 | 0 | foreach my $stock (@stocks) { | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 0 | $url = $BVB_URL . $stock; | |||||||||||||||||||||||||||||
| 82 | 0 | $reply = $ua->get($url, @ua_headers); | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 0 | my $code = $reply->code; | |||||||||||||||||||||||||||||
| 85 | 0 | my $desc = HTTP::Status::status_message($code); | |||||||||||||||||||||||||||||
| 86 | 0 | my $headers = $reply->headers_as_string; | |||||||||||||||||||||||||||||
| 87 | 0 | my $body = decode('UTF-8', $reply->content); | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | ### Body: $body | ||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | 0 | my ($name, $bid, $ask, $last, $open, $high, $low, $date); | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 0 | $info{ $stock, "symbol" } = $stock; | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( $code == 200 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | # Use HTML::TreeBuilder to parse HTML in $body | ||||||||||||||||||||||||||||||
| 98 | 0 | $tree = HTML::TreeBuilder->new; | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($tree->parse($body)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 0 | $tree->eof; | |||||||||||||||||||||||||||||
| 102 | ### [<now>] Tree: $tree | ||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $tree->look_down(_tag => 'div', id => 'ctl00_body_divNoData') ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 104 | 0 | $info{ $stock, "success" } = 0; | |||||||||||||||||||||||||||||
| 105 | 0 | $info{ $stock, "errormsg" } = | |||||||||||||||||||||||||||||
| 106 | "Error retrieving quote for $stock. No data returned"; | ||||||||||||||||||||||||||||||
| 107 | 0 | next; | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | 0 | my $name_element = $tree->look_down(_tag => 'h2', class => qr/^mBot0 large textStyled/); | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (defined $name_element) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 111 | 0 | $name = $name_element->as_text; | |||||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||||
| 113 | 0 | $info{ $stock, "success" } = 0; | |||||||||||||||||||||||||||||
| 114 | 0 | $info{ $stock, "errormsg" } = | |||||||||||||||||||||||||||||
| 115 | "Error retrieving quote for $stock. No data returned"; | ||||||||||||||||||||||||||||||
| 116 | 0 | next; | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | 0 | $info{ $stock, 'success' } = 1; | |||||||||||||||||||||||||||||
| 119 | 0 | ($info{ $stock, 'name' } = $name) =~ s/^\s+|\s+$//g ; | |||||||||||||||||||||||||||||
| 120 | 0 | $info{ $stock, 'currency' } = 'RON'; | |||||||||||||||||||||||||||||
| 121 | 0 | $info{ $stock, 'method' } = 'bvb'; | |||||||||||||||||||||||||||||
| 122 | 0 | $table = $tree->look_down(_tag => 'table', id => qr/^ctl00_body_ctl02_PricesControl_dvCPrices/)->as_HTML; | |||||||||||||||||||||||||||||
| 123 | 0 | $pricetable = HTML::TableExtract->new(); | |||||||||||||||||||||||||||||
| 124 | 0 | $pricetable->parse($table); | |||||||||||||||||||||||||||||
| 125 | 0 | foreach my $row ($pricetable->rows) { | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ( @$row[0] =~ m|Ask$| ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 127 | ### [<now>] Big Ask Row: $row | ||||||||||||||||||||||||||||||
| 128 | 0 | ($bid, $ask) = @$row[1] =~ m|^\s*([\d\.,]+)\s+\/\s+([\d\.,]+)|; | |||||||||||||||||||||||||||||
| 129 | 0 | $info{ $stock, 'bid' } = $bid =~ s|,|.|r; | |||||||||||||||||||||||||||||
| 130 | 0 | $info{ $stock, 'ask' } = $ask =~ s|,|.|r; | |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||
| 132 | elsif ( @$row[0] =~ m|^Data/ora| ) { | ||||||||||||||||||||||||||||||
| 133 | 0 | ($date) = @$row[1] =~ m|^([\d\.,/]+)\s|; | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $quoter->store_date(\%info, $stock, {eurodate => $1}) if $date =~ m|([0-9]{1,2}.[0-9]{1,2}.[0-9]{4})|; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||
| 136 | elsif ( @$row[0] =~ m|^Ultimul pret| ) { | ||||||||||||||||||||||||||||||
| 137 | 0 | ($last) = @$row[1] =~ m|^([\d\.,]+)|; | |||||||||||||||||||||||||||||
| 138 | 0 | $info{ $stock, 'last' } = $last =~ s|,|.|r; | |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||
| 140 | elsif ( @$row[0] =~ m|^Pret deschidere| ) { | ||||||||||||||||||||||||||||||
| 141 | 0 | ($open) = @$row[1] =~ m|^([\d\.,]+)|; | |||||||||||||||||||||||||||||
| 142 | 0 | $info{ $stock, 'open' } = $open =~ s|,|.|r; | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | elsif ( @$row[0] =~ m|^Pret maxim| ) { | ||||||||||||||||||||||||||||||
| 145 | 0 | ($high) = @$row[1] =~ m|^([\d\.,]+)|; | |||||||||||||||||||||||||||||
| 146 | 0 | $info{ $stock, 'high' } = $high =~ s|,|.|r; | |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||
| 148 | elsif ( @$row[0] =~ m|^Pret minim| ) { | ||||||||||||||||||||||||||||||
| 149 | 0 | ($low) = @$row[1] =~ m|^([\d\.,]+)|; | |||||||||||||||||||||||||||||
| 150 | 0 | $info{ $stock, 'low' } = $low =~ s|,|.|r; | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||||
| 155 | 0 | $tree->eof; | |||||||||||||||||||||||||||||
| 156 | 0 | $info{ $stock, "success" } = 0; | |||||||||||||||||||||||||||||
| 157 | 0 | $info{ $stock, "errormsg" } = | |||||||||||||||||||||||||||||
| 158 | "Error retrieving quote for $stock. Could not parse HTML returned from $url."; | ||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | } else { # HTTP Request failed (code != 200) | ||||||||||||||||||||||||||||||
| 162 | 0 | $info{ $stock, "success" } = 0; | |||||||||||||||||||||||||||||
| 163 | 0 | $info{ $stock, "errormsg" } = | |||||||||||||||||||||||||||||
| 164 | "Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; | ||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 170 | 0 | return \%info; | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | 5 | 1; | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||||