| 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 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 5 | ▶ | use vars qw($VERSION $YIND_URL_HEAD $YIND_URL_TAIL); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 5 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 5 | ▶ | use Web::Scraper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 5 | our $VERSION = '1.72'; # VERSION | |||||||||
| 36 | |||||||||||
| 37 | # URL example = https://www.borsaitaliana.it/borsa/search/scheda.html?code=IT0001086567&lang=it | ||||||||||
| 38 | 5 | my $YIND_URL_HEAD = 'https://www.borsaitaliana.it/borsa/search/scheda.html?code='; | |||||||||
| 39 | 5 | my $YIND_URL_TAIL = '&lang=it'; | |||||||||
| 40 | |||||||||||
| 41 | 5 | our $DISPLAY = 'Borsa Italiana (Bonds), IT'; | |||||||||
| 42 | 5 | our @LABELS = qw/name last date isodate currency method exchange price symbol/; | |||||||||
| 43 | 5 | our $METHODHASH = {subroutine => \&borsa_italiana, | |||||||||
| 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 | borsa_italiana => $METHODHASH, | |||||||||
| 50 | ); | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 5 | ▶ | sub methods { | ||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | 5 | my %m = methodinfo(); | |||||||||
| 55 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 5 | ▶ | sub labels { | ||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 59 | 5 | my %m = methodinfo(); | |||||||||
| 60 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub borsa_italiana { | ||||||||
|
Subroutine
borsa_italiana: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||
| 64 | |||||||||||
| 65 | 0 | my $quoter = shift; | |||||||||
| 66 | 0 | my @bonds = @_; | |||||||||
| 67 | 0 | my ( %info, $reply, $url, $te, $ts, $row, @cells, $ce ); | |||||||||
| 68 | 0 | my ($dd,$mm,$yy,$hh,$mi,$ss); | |||||||||
| 69 | 0 | my ( $my_date ); | |||||||||
| 70 | 0 | my $ua = $quoter->user_agent(); | |||||||||
| 71 | |||||||||||
| 72 | 0 | foreach my $bond (@bonds) { | |||||||||
| 73 | |||||||||||
| 74 | 0 | $url = $YIND_URL_HEAD . $bond . $YIND_URL_TAIL; | |||||||||
| 75 | 0 | $reply = $ua->get($url); | |||||||||
| 76 | |||||||||||
| 77 | 0 | my $code = $reply->code; | |||||||||
| 78 | 0 | my $desc = HTTP::Status::status_message($code); | |||||||||
| 79 | 0 | my $headers = $reply->headers_as_string; | |||||||||
| 80 | 0 | 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 | 0 | $info{ $bond, "symbol" } = $bond; | |||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | if ( $code == 200 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | |||||||||||
| 92 | ### [<now>] Body: $body | ||||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | my $widget = scraper { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | 0 | process 'div.summary-value span.t-text', 'val' => 'TEXT'; | |||||||||
| 96 | 0 | }; | |||||||||
| 97 | |||||||||||
| 98 | 0 | my $result = $widget->scrape($reply); | |||||||||
| 99 | # check if found | ||||||||||
| 100 | 0 | ▶ | unless (exists $result->{val}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | $info{$bond, 'success'} = 0; | |||||||||
| 102 | 0 | $info{$bond, 'errormsg'} = 'Failed to find ISIN'; | |||||||||
| 103 | 0 | next; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | my $value = $result->{val}; | |||||||||
| 107 | 0 | $value =~ s/[^0123456789,]//g; | |||||||||
| 108 | 0 | $value =~ s/,/./g; | |||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | $widget = scraper { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | process 'title', 'name' => 'TEXT'; | |||||||||
| 112 | 0 | }; | |||||||||
| 113 | |||||||||||
| 114 | 0 | $result = $widget->scrape($reply); | |||||||||
| 115 | # check if found | ||||||||||
| 116 | 0 | ▶ | unless (exists $result->{name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | $info{$bond, 'success'} = 0; | |||||||||
| 118 | 0 | $info{$bond, 'errormsg'} = 'Failed to find ISIN'; | |||||||||
| 119 | 0 | next; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 0 | my $name = $result->{name}; | |||||||||
| 123 | 0 | $name =~ s/quotazioni in tempo reale .* Borsa Italiana//g; | |||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | $widget = scraper { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 126 | 0 | process 'div.summary-fase span.t-text', 'dt[]' => 'TEXT'; | |||||||||
| 127 | 0 | }; | |||||||||
| 128 | |||||||||||
| 129 | 0 | $result = $widget->scrape($reply); | |||||||||
| 130 | # check if found | ||||||||||
| 131 | 0 | ▶ | unless (exists $result->{dt}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | $info{$bond, 'success'} = 0; | |||||||||
| 133 | 0 | $info{$bond, 'errormsg'} = 'Failed to find ISIN'; | |||||||||
| 134 | 0 | next; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 0 | my $date = $result->{dt}[1]; | |||||||||
| 138 | 0 | $date =~ s/.*Contratto:\ //g; | |||||||||
| 139 | 0 | $date =~ s/[^0123456789]//g; | |||||||||
| 140 | 0 | ▶ | 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/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | $info{$bond, 'success'} = 0; | |||||||||
| 142 | 0 | $info{$bond, 'errormsg'} = 'Invalid trade date. Closed or expired?'; | |||||||||
| 143 | 0 | next; | |||||||||
| 144 | } | ||||||||||
| 145 | 0 | my $my_date= $dd.".".$mm.".".$yy." ".$hh.":".$mi.":".$ss; | |||||||||
| 146 | |||||||||||
| 147 | 0 | $info{ $bond, "success" } = 1; | |||||||||
| 148 | 0 | $info{ $bond, "exchange" } = "Borsa Italiana"; | |||||||||
| 149 | 0 | $info{ $bond, "method" } = "borsa_italiana"; | |||||||||
| 150 | 0 | $info{ $bond, "name" } = $name; | |||||||||
| 151 | 0 | $info{ $bond, "symbol" } = $bond; | |||||||||
| 152 | 0 | $info{ $bond, "price" } = $value; | |||||||||
| 153 | 0 | $info{ $bond, "last" } = $value; | |||||||||
| 154 | 0 | $info{ $bond, "currency" } = "EUR"; | |||||||||
| 155 | |||||||||||
| 156 | 0 | $quoter->store_date( \%info, $bond, | |||||||||
| 157 | { eurodate => $my_date } ); | ||||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | #HTTP request fail | ||||||||||
| 161 | else { | ||||||||||
| 162 | 0 | $info{ $bond, "success" } = 0; | |||||||||
| 163 | 0 | $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 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | return \%info; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 5 | 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 | ||||||||||