| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=4 sw=4 noai ic showmode showmatch: |
| 3 |
# This module is based on the Finance::Quote::BSERO module |
| 4 |
# It was first called BOMSE but has been renamed to yahooJSON |
| 5 |
# since it gets a lot of quotes besides Indian |
| 6 |
# |
| 7 |
# The code has been modified by Abhijit K to |
| 8 |
# retrieve stock information from Yahoo Finance through json calls |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation; either version 2 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program; if not, write to the Free Software |
| 22 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 23 |
# 02110-1301, USA |
| 24 |
|
| 25 |
package Finance::Quote::BorsaItaliana; |
| 26 |
|
| 27 |
use strict; |
| 28 |
use vars qw($VERSION $YIND_URL_HEAD $YIND_URL_TAIL); |
| 29 |
use LWP::UserAgent; |
| 30 |
use Web::Scraper; |
| 31 |
|
| 32 |
use constant DEBUG => $ENV{DEBUG}; |
| 33 |
use if DEBUG, 'Smart::Comments'; |
| 34 |
|
| 35 |
our $VERSION = '1.71'; # VERSION |
| 36 |
|
| 37 |
# URL example = https://www.borsaitaliana.it/borsa/search/scheda.html?code=IT0001086567&lang=it |
| 38 |
my $YIND_URL_HEAD = 'https://www.borsaitaliana.it/borsa/search/scheda.html?code='; |
| 39 |
my $YIND_URL_TAIL = '&lang=it'; |
| 40 |
|
| 41 |
our $DISPLAY = 'Borsa Italiana (Bonds), IT'; |
| 42 |
our @LABELS = qw/name last date isodate currency method exchange price symbol/; |
| 43 |
our $METHODHASH = {subroutine => \&borsa_italiana, |
| 44 |
display => $DISPLAY, |
| 45 |
labels => \@LABELS}; |
| 46 |
|
| 47 |
sub methodinfo { |
| 48 |
return ( |
| 49 |
borsa_italiana => $METHODHASH, |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
sub methods { |
| 54 |
my %m = methodinfo(); |
| 55 |
return map {$_ => $m{$_}{subroutine} } keys %m; |
| 56 |
} |
| 57 |
|
| 58 |
sub labels { |
| 59 |
my %m = methodinfo(); |
| 60 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 61 |
} |
| 62 |
|
| 63 |
sub borsa_italiana { |
| 64 |
|
| 65 |
my $quoter = shift; |
| 66 |
my @bonds = @_; |
| 67 |
my ( %info, $reply, $url, $te, $ts, $row, @cells, $ce ); |
| 68 |
my ($dd,$mm,$yy,$hh,$mi,$ss); |
| 69 |
my ( $my_date ); |
| 70 |
my $ua = $quoter->user_agent(); |
| 71 |
|
| 72 |
foreach my $bond (@bonds) { |
| 73 |
|
| 74 |
$url = $YIND_URL_HEAD . $bond . $YIND_URL_TAIL; |
| 75 |
$reply = $ua->get($url); |
| 76 |
|
| 77 |
my $code = $reply->code; |
| 78 |
my $desc = HTTP::Status::status_message($code); |
| 79 |
my $headers = $reply->headers_as_string; |
| 80 |
my $body = $reply->content; |
| 81 |
|
| 82 |
#Response variables available: |
| 83 |
#Response code: $code |
| 84 |
#Response description: $desc |
| 85 |
#HTTP Headers: $headers |
| 86 |
#Response body $body |
| 87 |
|
| 88 |
$info{ $bond, "symbol" } = $bond; |
| 89 |
|
| 90 |
if ( $code == 200 ) { |
| 91 |
|
| 92 |
### [<now>] Body: $body |
| 93 |
|
| 94 |
my $widget = scraper { |
| 95 |
process 'div.summary-value span.t-text', 'val' => 'TEXT'; |
| 96 |
}; |
| 97 |
|
| 98 |
my $result = $widget->scrape($reply); |
| 99 |
# check if found |
| 100 |
unless (exists $result->{val}) { |
| 101 |
$info{$bond, 'success'} = 0; |
| 102 |
$info{$bond, 'errormsg'} = 'Failed to find ISIN'; |
| 103 |
next; |
| 104 |
} |
| 105 |
|
| 106 |
my $value = $result->{val}; |
| 107 |
$value =~ s/[^0123456789,]//g; |
| 108 |
$value =~ s/,/./g; |
| 109 |
|
| 110 |
$widget = scraper { |
| 111 |
process 'title', 'name' => 'TEXT'; |
| 112 |
}; |
| 113 |
|
| 114 |
$result = $widget->scrape($reply); |
| 115 |
# check if found |
| 116 |
unless (exists $result->{name}) { |
| 117 |
$info{$bond, 'success'} = 0; |
| 118 |
$info{$bond, 'errormsg'} = 'Failed to find ISIN'; |
| 119 |
next; |
| 120 |
} |
| 121 |
|
| 122 |
my $name = $result->{name}; |
| 123 |
$name =~ s/quotazioni in tempo reale .* Borsa Italiana//g; |
| 124 |
|
| 125 |
$widget = scraper { |
| 126 |
process 'div.summary-fase span.t-text', 'dt[]' => 'TEXT'; |
| 127 |
}; |
| 128 |
|
| 129 |
$result = $widget->scrape($reply); |
| 130 |
# check if found |
| 131 |
unless (exists $result->{dt}) { |
| 132 |
$info{$bond, 'success'} = 0; |
| 133 |
$info{$bond, 'errormsg'} = 'Failed to find ISIN'; |
| 134 |
next; |
| 135 |
} |
| 136 |
|
| 137 |
my $date = $result->{dt}[1]; |
| 138 |
$date =~ s/.*Contratto:\ //g; |
| 139 |
$date =~ s/[^0123456789]//g; |
| 140 |
unless ( ($dd,$mm,$yy,$hh,$mi,$ss) = $date =~ /^([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{1,})([0-9]{2})([0-9]{2})\z/ ) { |
| 141 |
$info{$bond, 'success'} = 0; |
| 142 |
$info{$bond, 'errormsg'} = 'Invalid trade date. Closed or expired?'; |
| 143 |
next; |
| 144 |
} |
| 145 |
my $my_date= $dd.".".$mm.".".$yy." ".$hh.":".$mi.":".$ss; |
| 146 |
|
| 147 |
$info{ $bond, "success" } = 1; |
| 148 |
$info{ $bond, "exchange" } = "Borsa Italiana"; |
| 149 |
$info{ $bond, "method" } = "borsa_italiana"; |
| 150 |
$info{ $bond, "name" } = $name; |
| 151 |
$info{ $bond, "symbol" } = $bond; |
| 152 |
$info{ $bond, "price" } = $value; |
| 153 |
$info{ $bond, "last" } = $value; |
| 154 |
$info{ $bond, "currency" } = "EUR"; |
| 155 |
|
| 156 |
$quoter->store_date( \%info, $bond, |
| 157 |
{ eurodate => $my_date } ); |
| 158 |
} |
| 159 |
|
| 160 |
#HTTP request fail |
| 161 |
else { |
| 162 |
$info{ $bond, "success" } = 0; |
| 163 |
$info{ $bond, "errormsg" } = |
| 164 |
"Error retrieving quote for $bond. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
return wantarray() ? %info : \%info; |
| 170 |
return \%info; |
| 171 |
} |
| 172 |
|
| 173 |
1; |
| 174 |
|
| 175 |
=head1 NAME |
| 176 |
|
| 177 |
Finance::Quote::BorsaItaliana - Obtain bond quotes from Borsa Italiana site |
| 178 |
|
| 179 |
=head1 SYNOPSIS |
| 180 |
|
| 181 |
use Finance::Quote; |
| 182 |
|
| 183 |
$q = Finance::Quote->new; |
| 184 |
|
| 185 |
%info = Finance::Quote->fetch("borsa_italiana","{ISIN_CODE}"); |
| 186 |
|
| 187 |
=head1 DESCRIPTION |
| 188 |
|
| 189 |
This module fetches information from Borsa Italiana site |
| 190 |
|
| 191 |
This module is loaded by default on a Finance::Quote object. It's |
| 192 |
also possible to load it explicitly by placing "BorsaItaliana" in the argument |
| 193 |
list to Finance::Quote->new(). |
| 194 |
|
| 195 |
This module provides the "borsa_italiana" fetch method. Currently this |
| 196 |
module retrieves data for bonds only. |
| 197 |
|
| 198 |
=head1 LABELS RETURNED |
| 199 |
|
| 200 |
The following labels may be returned by Finance::Quote::BorsaItaliana : |
| 201 |
name, symbol, price, last, isodate, currency, method, exchange. |
| 202 |
|
| 203 |
=head1 SEE ALSO |
| 204 |
|
| 205 |
=cut |