Quote/Bourso.pm

filter: Statementstmt 43.7% 42 / 96 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 5 totaltotal 39.4% 54 / 137 CC 13 SCAR 45.2
CC
13
Cov
36%
bourso
182.0
bourso_to_number
2.0
BEGIN
1.0
CRAP 91.5
« Quote/SwissFundData.pm ↑ Summary Quote/GoogleWeb.pm »
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net>
4 # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org>
5 # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com>
6 # Copyright (C) 2000, Paul Fenwick <pjf@cpan.org>
7 # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net>
8 # Copyright (C) 2001, Rob Sessink <rob_ses@users.sourceforge.net>
9 # Copyright (C) 2005, Morten Cools <morten@cools.no>
10 # Copyright (C) 2006, Dominique Corbex <domcox@sourceforge.net>
11 # Copyright (C) 2008, Bernard Fuentes <bernard.fuentes@gmail.com>
12 # Copyright (C) 2009, Erik Colson <eco@ecocode.net>
13 # Copyright (C) 2018, Jean-Marie Pacquet <jmpacquet@sourceforge.net>
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 <domcox@sourceforge.net>
54 #
55 # * (1.4) changes on web site
56 #
57 # 2006-09-02 Dominique Corbex <domcox@sourceforge.net>
58 #
59 # * (1.3) changes on web site
60 #
61 # 2006-06-28 Dominique Corbex <domcox@sourceforge.net>
62 #
63 # * (1.2) changes on web site
64 #
65 # 2006-02-22 Dominique Corbex <domcox@sourceforge.net>
66 #
67 # * (1.0) iniial release
68 #
69  
70 5 require 5.005;
71  
72 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 package Finance::Quote::Bourso;
75  
76 5 ▶use vars qw( $Bourso_URL );
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
79 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 5 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 5 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
84 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
85 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 5 our $VERSION = '1.72_02'; # TRIAL VERSION
88  
89 5 my $Bourso_URL = 'https://www.boursorama.com/cours/';
90  
91 5 our $DISPLAY = 'Bourso';
92 5 our @LABELS = qw/name last date isodate p_change open high low close volume currency method exchange/;
93 5 our $METHODHASH = {subroutine => \&bourso,
94 display => $DISPLAY,
95 labels => \@LABELS,
96 };
97  
98 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
99 return (
100 10 bourso => $METHODHASH,
101 europe => $METHODHASH,
102 france => $METHODHASH,
103 );
104 }
105  
106 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
107 5 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
108 }
109  
110 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
111 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
112 }
113  
114 0 ▶sub bourso_to_number {
Subroutine bourso_to_number: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $x = shift(@_);
116 0 $x =~ s/\s//g; # remove spaces etc in number
117 0 return $x;
118 }
119  
120 0 ▶sub bourso {
Subroutine bourso: not called CC=13 SCAR=52.0
Pod undocumented
121 0 my $quoter = shift;
122 0 my @stocks = @_;
123 0 my $ua = $quoter->user_agent();
124 0 my %info;
125  
126 ### UA max_redirect: $ua->max_redirect
127  
128 0 foreach my $stock (@stocks) {
129 0 eval {
130 0 my $query = $Bourso_URL . $stock;
131 0 my $reply = $ua->request(GET $query);
132  
133 ### Search: $query, $reply->code
134  
135 0 my $body = decode('UTF-8', $reply->content);
136 0 my $root = HTML::TreeBuilder->new_from_content($body);
137  
138 0 my $div = $root->look_down(_tag => 'div',
139 class => qr/^c-faceplate/);
140  
141 0 my $name = $div->look_down(_tag => 'a', class => qr/^c-faceplate__company-link/)->as_text();
142 0 $name =~ s/^\s+|\s+$//g;
143 0 utf8::encode($name);
144  
145 0 my $currency = $div->look_down(_tag => 'span', class => qr/^c-faceplate__price-currency/)->as_text();
146 0 $currency =~ s/^\s+|\s+$//g;
147  
148 0 my ($date, $last, $symbol, $low, $high, $close, $exchange, $volume, $net);
149  
150 0 ▶ if ($div->attr('data-ist-init')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($div->attr('data-ist-init')) { } 0 0 0
151 0 my $json = JSON::decode_json($div->attr('data-ist-init'));
152 0 $date = $json->{'tradeDate'};
153 0 $last = $json->{'last'};
154 0 $symbol = $json->{'symbol'};
155 0 $low = $json->{'low'};
156 0 $high = $json->{'high'};
157 0 $close = $json->{'previousClose'};
158 0 $exchange = $json->{'exchangeCode'};
159 0 $volume = $json->{'totalVolume'};
160 0 $net = $json->{'variation'};
161 }
162 else {
163 # date captures more than the date, but the regular expression below extracts just the date
164 0 $date = $div->look_down(_tag => 'div', class => qr/^c-faceplate__real-time/)->as_text();
165 0 $last = $div->look_down(_tag => 'span', class => qr/^c-instrument c-instrument--last/)->as_text();
166 0 $symbol = $stock;
167 # last here will contain a comma (,) instead of a decimal point
168 0 $last =~ s/,/./;
169 }
170  
171 0 $info{$stock, 'symbol'} = $symbol;
172 0 $info{$stock, 'name'} = $name;
173 0 $info{$stock, 'currency'} = $currency;
174 0 ($info{$stock, 'last'} = $last) =~ s/[^0-9.]//g;
175 0 ▶ ($info{$stock, 'high'} = $high) =~ s/[^0-9.]//g if $high;
Branch: 1 branch
BranchTrueFalseTotal
if $high 0 0 0
176 0 ▶ ($info{$stock, 'low'} = $low) =~ s/[^0-9.]//g if $low;
Branch: 1 branch
BranchTrueFalseTotal
if $low 0 0 0
177 0 ▶ ($info{$stock, 'close'} = $close) =~ s/[^0-9.]//g if $close;
Branch: 1 branch
BranchTrueFalseTotal
if $close 0 0 0
178 0 ▶ $info{$stock, 'exchange'} = $exchange if $exchange;
Branch: 1 branch
BranchTrueFalseTotal
if $exchange 0 0 0
179 0 ▶ ($info{$stock, 'volume'} = $volume) =~ s/[^0-9.]//g if $volume;
Branch: 1 branch
BranchTrueFalseTotal
if $volume 0 0 0
180 0 ▶ ($info{$stock, 'net'} = $net) =~ s/[^0-9.]//g if $net;
Branch: 1 branch
BranchTrueFalseTotal
if $net 0 0 0
181  
182 # 2020-07-17 17:03:45
183 0 ▶ $quoter->store_date(\%info, $stock, {isodate => $1}) if $date =~ m|([0-9]{4}-[0-9]{2}-[0-9]{2})|;
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})/ 0 0 0
184  
185 # dd/mm/yyyy
186 0 ▶ $quoter->store_date(\%info, $stock, {eurodate => $1}) if $date =~ m|([0-9]{2}/[0-9]{2}/[0-9]{4})|;
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ m[([0-9]{2}/[0-9]{2}/[0-9]{4})] 0 0 0
187  
188 0 $info{$stock, 'method' } = 'bourso';
189 0 $info{$stock, 'success'} = 1;
190 };
191 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
192 0 $info{$stock, 'success'} = 0;
193 0 $info{$stock, 'errormsg'} = 'Failed to retrieve quote';
194 }
195 }
196  
197 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
198 0 return \%info;
199 }
200  
201 5 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
« Quote/SwissFundData.pm ↑ Summary Quote/GoogleWeb.pm »