| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Copyright (C) 1998, Dj Padzensky <[email protected]> |
| 4 |
# Copyright (C) 1998, 1999 Linas Vepstas <[email protected]> |
| 5 |
# Copyright (C) 2000, Yannick LE NY <[email protected]> |
| 6 |
# Copyright (C) 2000, Paul Fenwick <[email protected]> |
| 7 |
# Copyright (C) 2000, Brent Neal <[email protected]> |
| 8 |
# Copyright (C) 2001, Rob Sessink <[email protected]> |
| 9 |
# Copyright (C) 2005, Morten Cools <[email protected]> |
| 10 |
# Copyright (C) 2006, Dominique Corbex <[email protected]> |
| 11 |
# Copyright (C) 2008, Bernard Fuentes <[email protected]> |
| 12 |
# Copyright (C) 2009, Erik Colson <[email protected]> |
| 13 |
# Copyright (C) 2018, Jean-Marie Pacquet <[email protected]> |
| 14 |
# |
| 15 |
# This program is free software; you can redistribute it and/or modify |
| 16 |
# it under the terms of the GNU General Public License as published by |
| 17 |
# the Free Software Foundation; either version 2 of the License, or |
| 18 |
# (at your option) any later version. |
| 19 |
# |
| 20 |
# This program is distributed in the hope that it will be useful, |
| 21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
# GNU General Public License for more details. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU General Public License |
| 26 |
# along with this program; if not, write to the Free Software |
| 27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 28 |
# 02110-1301, USA |
| 29 |
# |
| 30 |
# |
| 31 |
# This code derived from Padzensky's work on package Finance::YahooQuote, |
| 32 |
# but extends its capabilites to encompas a greater number of data sources. |
| 33 |
# |
| 34 |
# |
| 35 |
# Changelog |
| 36 |
# |
| 37 |
# 2018-04-08 Jean-Marie Pacquet |
| 38 |
# |
| 39 |
# * (1.49) Major site change (html 5) |
| 40 |
# |
| 41 |
# 2014-01-12 Arnaud Gardelein |
| 42 |
# |
| 43 |
# * changes on website |
| 44 |
# |
| 45 |
# 2009-04-12 Erik Colson |
| 46 |
# |
| 47 |
# * Major site change. |
| 48 |
# |
| 49 |
# 2008-11-09 Bernard Fuentes |
| 50 |
# |
| 51 |
# * changes on website |
| 52 |
# |
| 53 |
# 2006-12-26 Dominique Corbex <[email protected]> |
| 54 |
# |
| 55 |
# * (1.4) changes on web site |
| 56 |
# |
| 57 |
# 2006-09-02 Dominique Corbex <[email protected]> |
| 58 |
# |
| 59 |
# * (1.3) changes on web site |
| 60 |
# |
| 61 |
# 2006-06-28 Dominique Corbex <[email protected]> |
| 62 |
# |
| 63 |
# * (1.2) changes on web site |
| 64 |
# |
| 65 |
# 2006-02-22 Dominique Corbex <[email protected]> |
| 66 |
# |
| 67 |
# * (1.0) iniial release |
| 68 |
# |
| 69 |
|
| 70 |
require 5.005; |
| 71 |
|
| 72 |
use strict; |
| 73 |
|
| 74 |
package Finance::Quote::Bourso; |
| 75 |
|
| 76 |
use vars qw( $Bourso_URL ); |
| 77 |
|
| 78 |
use constant DEBUG => $ENV{DEBUG}; |
| 79 |
use if DEBUG, 'Smart::Comments'; |
| 80 |
|
| 81 |
use HTTP::Request::Common; |
| 82 |
use HTML::TreeBuilder; |
| 83 |
use Encode qw(decode); |
| 84 |
use JSON qw( decode_json ); |
| 85 |
use utf8; |
| 86 |
|
| 87 |
our $VERSION = '1.71'; # VERSION |
| 88 |
|
| 89 |
my $Bourso_URL = 'https://www.boursorama.com/cours/'; |
| 90 |
|
| 91 |
our $DISPLAY = 'Bourso'; |
| 92 |
our @LABELS = qw/name last date isodate p_change open high low close volume currency method exchange/; |
| 93 |
our $METHODHASH = {subroutine => \&bourso, |
| 94 |
display => $DISPLAY, |
| 95 |
labels => \@LABELS, |
| 96 |
}; |
| 97 |
|
| 98 |
sub methodinfo { |
| 99 |
return ( |
| 100 |
bourso => $METHODHASH, |
| 101 |
europe => $METHODHASH, |
| 102 |
france => $METHODHASH, |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
sub labels { |
| 107 |
my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 108 |
} |
| 109 |
|
| 110 |
sub methods { |
| 111 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 112 |
} |
| 113 |
|
| 114 |
sub bourso_to_number { |
| 115 |
my $x = shift(@_); |
| 116 |
$x =~ s/\s//g; # remove spaces etc in number |
| 117 |
return $x; |
| 118 |
} |
| 119 |
|
| 120 |
sub bourso { |
| 121 |
my $quoter = shift; |
| 122 |
my @stocks = @_; |
| 123 |
my $ua = $quoter->user_agent(); |
| 124 |
my %info; |
| 125 |
|
| 126 |
### UA max_redirect: $ua->max_redirect |
| 127 |
|
| 128 |
foreach my $stock (@stocks) { |
| 129 |
eval { |
| 130 |
my $query = $Bourso_URL . $stock; |
| 131 |
my $reply = $ua->request(GET $query); |
| 132 |
|
| 133 |
### Search: $query, $reply->code |
| 134 |
|
| 135 |
my $body = decode('UTF-8', $reply->content); |
| 136 |
my $root = HTML::TreeBuilder->new_from_content($body); |
| 137 |
|
| 138 |
my $div = $root->look_down(_tag => 'div', |
| 139 |
class => qr/^c-faceplate/); |
| 140 |
|
| 141 |
my $name = $div->look_down(_tag => 'a', class => qr/^c-faceplate__company-link/)->as_text(); |
| 142 |
$name =~ s/^\s+|\s+$//g; |
| 143 |
utf8::encode($name); |
| 144 |
|
| 145 |
my $currency = $div->look_down(_tag => 'span', class => qr/^c-faceplate__price-currency/)->as_text(); |
| 146 |
$currency =~ s/^\s+|\s+$//g; |
| 147 |
|
| 148 |
my ($date, $last, $symbol, $low, $high, $close, $exchange, $volume, $net); |
| 149 |
|
| 150 |
if ($div->attr('data-ist-init')) { |
| 151 |
my $json = JSON::decode_json($div->attr('data-ist-init')); |
| 152 |
$date = $json->{'tradeDate'}; |
| 153 |
$last = $json->{'last'}; |
| 154 |
$symbol = $json->{'symbol'}; |
| 155 |
$low = $json->{'low'}; |
| 156 |
$high = $json->{'high'}; |
| 157 |
$close = $json->{'previousClose'}; |
| 158 |
$exchange = $json->{'exchangeCode'}; |
| 159 |
$volume = $json->{'totalVolume'}; |
| 160 |
$net = $json->{'variation'}; |
| 161 |
} |
| 162 |
else { |
| 163 |
# date captures more than the date, but the regular expression below extracts just the date |
| 164 |
$date = $div->look_down(_tag => 'div', class => qr/^c-faceplate__real-time/)->as_text(); |
| 165 |
$last = $div->look_down(_tag => 'span', class => qr/^c-instrument c-instrument--last/)->as_text(); |
| 166 |
$symbol = $stock; |
| 167 |
# last here will contain a comma (,) instead of a decimal point |
| 168 |
$last =~ s/,/./; |
| 169 |
} |
| 170 |
|
| 171 |
$info{$stock, 'symbol'} = $symbol; |
| 172 |
$info{$stock, 'name'} = $name; |
| 173 |
$info{$stock, 'currency'} = $currency; |
| 174 |
($info{$stock, 'last'} = $last) =~ s/[^0-9.]//g; |
| 175 |
($info{$stock, 'high'} = $high) =~ s/[^0-9.]//g if $high; |
| 176 |
($info{$stock, 'low'} = $low) =~ s/[^0-9.]//g if $low; |
| 177 |
($info{$stock, 'close'} = $close) =~ s/[^0-9.]//g if $close; |
| 178 |
$info{$stock, 'exchange'} = $exchange if $exchange; |
| 179 |
($info{$stock, 'volume'} = $volume) =~ s/[^0-9.]//g if $volume; |
| 180 |
($info{$stock, 'net'} = $net) =~ s/[^0-9.]//g if $net; |
| 181 |
|
| 182 |
# 2020-07-17 17:03:45 |
| 183 |
$quoter->store_date(\%info, $stock, {isodate => $1}) if $date =~ m|([0-9]{4}-[0-9]{2}-[0-9]{2})|; |
| 184 |
|
| 185 |
# dd/mm/yyyy |
| 186 |
$quoter->store_date(\%info, $stock, {eurodate => $1}) if $date =~ m|([0-9]{2}/[0-9]{2}/[0-9]{4})|; |
| 187 |
|
| 188 |
$info{$stock, 'method' } = 'bourso'; |
| 189 |
$info{$stock, 'success'} = 1; |
| 190 |
}; |
| 191 |
if ($@) { |
| 192 |
$info{$stock, 'success'} = 0; |
| 193 |
$info{$stock, 'errormsg'} = 'Failed to retrieve quote'; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return wantarray() ? %info : \%info; |
| 198 |
return \%info; |
| 199 |
} |
| 200 |
|
| 201 |
1; |
| 202 |
|
| 203 |
=head1 NAME |
| 204 |
|
| 205 |
Finance::Quote::Bourso - Obtain quotes from Boursorama. |
| 206 |
|
| 207 |
=head1 SYNOPSIS |
| 208 |
|
| 209 |
use Finance::Quote; |
| 210 |
|
| 211 |
$q = Finance::Quote->new; |
| 212 |
|
| 213 |
%info = Finance::Quote->fetch("bourso","ml"); # Only query Bourso |
| 214 |
|
| 215 |
=head1 DESCRIPTION |
| 216 |
|
| 217 |
This module fetches information from the "Paris Stock Exchange", |
| 218 |
https://www.boursorama.com. All stocks are available. |
| 219 |
|
| 220 |
This module is loaded by default on a Finance::Quote object. It's |
| 221 |
also possible to load it explicitly by placing "bourso" in the argument |
| 222 |
list to Finance::Quote->new(). |
| 223 |
|
| 224 |
Information obtained by this module may be covered by www.boursorama.com |
| 225 |
terms and conditions See https://www.boursorama.com/ for details. |
| 226 |
|
| 227 |
=head1 LABELS RETURNED |
| 228 |
|
| 229 |
The following labels will be returned by Finance::Quote::Bourso : name, last, |
| 230 |
symbol, date, isodate, method, currency. For some symbols, additional |
| 231 |
information is available: exchange, high, low, close, net, volume. |
| 232 |
|
| 233 |
=head1 SEE ALSO |
| 234 |
|
| 235 |
Boursorama (french web site), https://www.boursorama.com |
| 236 |
|
| 237 |
=cut |