| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 14 | # along with this program; if not, write to the Free Software | ||||||||||||||||||||||||||||
| 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||||||||||||||||||||||||||
| 16 | # 02110-1301, USA | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package Finance::Quote::BSEIndia; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | #use IO::Uncompress::Unzip qw(unzip $UnzipError); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use if DEBUG, 'Smart::Comments', '###'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | our $VERSION = '1.72'; # VERSION | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use vars qw($BSE_URL); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | $BSE_URL = "https://www.bseindia.com"; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 5 | our $DISPLAY = 'BSE India'; | |||||||||||||||||||||||||||
| 33 | 5 | our $FEATURES = {}; | |||||||||||||||||||||||||||
| 34 | 5 | our @LABELS = qw/close last high low open prevclose exchange name/; | |||||||||||||||||||||||||||
| 35 | 5 | our $METHODHASH = {subroutine => \&bseindia, | |||||||||||||||||||||||||||
| 36 | display => $DISPLAY, | ||||||||||||||||||||||||||||
| 37 | labels => \@LABELS, | ||||||||||||||||||||||||||||
| 38 | features => $FEATURES}; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | ▶ | sub methodinfo { | ||||||||||||||||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | return ( | ||||||||||||||||||||||||||||
| 42 | 10 | bseindia => $METHODHASH, | |||||||||||||||||||||||||||
| 43 | india => $METHODHASH, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | sub methods { | ||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 5 | my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | ▶ | sub labels { | ||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 5 | my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub bseindia { | ||||||||||||||||||||||||||
|
Subroutine
bseindia: not called CC=18 SCAR=58.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my $quoter = shift; | |||||||||||||||||||||||||||
| 57 | 0 | my @symbols = @_; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | return unless @symbols; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my (%info, $errormsg, $fh, $ua, $url, $reply); | |||||||||||||||||||||||||||
| 61 | 0 | my $output; | |||||||||||||||||||||||||||
| 62 | 0 | my @array; | |||||||||||||||||||||||||||
| 63 | 0 | my $meuradate; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | $ua = $quoter->user_agent; | |||||||||||||||||||||||||||
| 66 | # Set the ua to be blank. Server blocks default useragent. | ||||||||||||||||||||||||||||
| 67 | 0 | $ua->agent(''); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Try to fetch last 10 days | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | for (my ($days, $now) = (0, time()); $days < 10; $days++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | # Ex: https://www.bseindia.com/download/BhavCopy/Equity/BhavCopy_BSE_CM_0_0_0_20240718_F_0000.CSV | ||||||||||||||||||||||||||||
| 72 | 0 | my @lt = localtime($now - $days*24*60*60); | |||||||||||||||||||||||||||
| 73 | 0 | my ($date, $url, $req, $output); # added $req, $output for fileless | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $date = strftime "%Y%m%d", @lt; | |||||||||||||||||||||||||||
| 76 | 0 | $url = sprintf("https://www.bseindia.com/download/BhavCopy/Equity/BhavCopy_BSE_CM_0_0_0_%s_F_0000.CSV", $date); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | $req = HTTP::Request->new(GET => $url); #added for fileless | |||||||||||||||||||||||||||
| 79 | 0 | $reply = $ua->request($req); | |||||||||||||||||||||||||||
| 80 | #print "$url", $reply->is_success, $reply->status_line, "\n"; #DEBUG | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($reply->is_success or $reply->code == 304) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$reply->is_success or $reply->code == 304
MC/DC0%0/2
$reply->is_success or $reply->code == 304
$reply->is_success
$reply->code == 304
Truth table0%0/3
A or B
A= $reply->is_successB= $reply->code == 304
| |||||||||||||||||||||||||||||
| 82 | 0 | last; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (!$reply->is_success and $reply->code != 304) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $reply->is_success and $reply->code != 304
MC/DC0%0/2
not $reply->is_success and $reply->code != 304
not $reply->is_success
$reply->code != 304
Truth table0%0/3
not(A) and B
A= not $reply->is_successB= $reply->code != 304
| |||||||||||||||||||||||||||||
| 87 | 0 | $errormsg = "HTTP failure : " . $reply->status_line; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (!$errormsg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | #Does not use temp files. Fileless into variable $output | ||||||||||||||||||||||||||||
| 92 | #There is no zip file anymore | ||||||||||||||||||||||||||||
| 93 | #@array = split("\n", $output); | ||||||||||||||||||||||||||||
| 94 | 0 | @array = split("\n", $reply->content); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($errormsg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | foreach my $symbol (@symbols) { | |||||||||||||||||||||||||||
| 99 | 0 | $info{$symbol, "success"} = 0; | |||||||||||||||||||||||||||
| 100 | 0 | $info{$symbol, "errormsg"} = $errormsg; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Create a hash of all stocks requested | ||||||||||||||||||||||||||||
| 106 | 0 | my %symbolhash; | |||||||||||||||||||||||||||
| 107 | 0 | foreach my $symbol (@symbols) | |||||||||||||||||||||||||||
| 108 | { | ||||||||||||||||||||||||||||
| 109 | 0 | $symbolhash{$symbol} = 0; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | my $csvhead; | |||||||||||||||||||||||||||
| 112 | my @headhash; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # TradDt,BizDt,Sgmt,Src,FinInstrmTp,FinInstrmId,ISIN,TckrSymb,SctySrs,XpryDt,FininstrmActlXpryDt,StrkPric,OptnTp,FinInstrmNm,OpnPric,HghPric,LwPric,ClsPric,LastPric,PrvsClsgPric,UndrlygPric,SttlmPric,OpnIntrst,ChngInOpnIntrst,TtlTradgVol,TtlTrfVal,TtlNbOfTxsExctd,SsnId,NewBrdLotQty,Rmks,Rsvd1,Rsvd2,Rsvd3,Rsvd4 | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $csvhead = $array[0]; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | @headhash = split /\s*,s*/, $csvhead; | |||||||||||||||||||||||||||
| 119 | 0 | foreach (@array) { | |||||||||||||||||||||||||||
| 120 | 0 | my @data = split(",", $_); | |||||||||||||||||||||||||||
| 121 | 0 | my %datahash; | |||||||||||||||||||||||||||
| 122 | my $symbol; | ||||||||||||||||||||||||||||
| 123 | 0 | @datahash{@headhash} = @data; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (exists $symbolhash{$datahash{"FinInstrmId"}}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 0 | $symbol = $datahash{"FinInstrmId"}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | elsif(exists $symbolhash{$datahash{"ISIN"}}) { | ||||||||||||||||||||||||||||
| 129 | 0 | $symbol = $datahash{"ISIN"}; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 0 | next; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | $info{$symbol, 'symbol'} = $symbol; | |||||||||||||||||||||||||||
| 136 | 0 | $info{$symbol, 'close'} = $datahash{"ClsPric"}; | |||||||||||||||||||||||||||
| 137 | 0 | $info{$symbol, 'last'} = $datahash{"LastPric"}; | |||||||||||||||||||||||||||
| 138 | 0 | $info{$symbol, 'high'} = $datahash{"HghPric"}; | |||||||||||||||||||||||||||
| 139 | 0 | $info{$symbol, 'low'} = $datahash{"LwPric"}; | |||||||||||||||||||||||||||
| 140 | 0 | $info{$symbol, 'open'} = $datahash{"OpnPric"}; | |||||||||||||||||||||||||||
| 141 | 0 | $info{$symbol, 'prevclose'} = $datahash{"PrvsClsgPric"}; | |||||||||||||||||||||||||||
| 142 | 0 | $info{$symbol, 'name'} = $datahash{"FinInstrmNm"}; | |||||||||||||||||||||||||||
| 143 | 0 | $quoter->store_date(\%info, $symbol, {isodate => $datahash{"TradDt"}}); | |||||||||||||||||||||||||||
| 144 | 0 | $info{$symbol, 'method'} = 'bseindia'; | |||||||||||||||||||||||||||
| 145 | 0 | $info{$symbol, 'currency'} = 'INR'; | |||||||||||||||||||||||||||
| 146 | 0 | $info{$symbol, 'exchange'} = 'BSE'; | |||||||||||||||||||||||||||
| 147 | 0 | $info{$symbol, 'success'} = 1; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | foreach my $symbol (@symbols) { | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | unless (exists $info{$symbol, 'success'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | ### Not Found: $symbol | ||||||||||||||||||||||||||||
| 153 | 0 | $info{$symbol, 'success'} = 0; | |||||||||||||||||||||||||||
| 154 | 0 | $info{$symbol, 'errormsg'} = 'Stock not found on BSE.'; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | return wantarray ? %info : \%info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | 1; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 NAME | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Finance::Quote::BSEIndia - Obtain quotes from BSE (India). | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | use Finance::Quote; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $q = Finance::Quote->new(); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | %info = $q->fetch('bseindia', 'INE001A01036'); # Only query BSE. | ||||||||||||||||||||||||||||
| 176 | %info = $q->fetch('india', 'INE001A01036'); # Failover to other sources OK. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | This module obtains information about shares listed on the BSE (India). | ||||||||||||||||||||||||||||
| 181 | Source is the daily bhav copy (zipped CSV). | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | This module provides both the "bseindia" and "india" fetch methods. Please use the "india" fetch method if you wish to have failover with other sources for Indian stocks (such as NSE). | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 LABELS RETURNED | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | The following labels may be returned by Finance::Quote::BSEIndia: | ||||||||||||||||||||||||||||
| 188 | close, last, high, low, open, prevclose, exchange, name | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | BSE (formerly known as Bombay Stock Exchange Ltd.), http://www.bseindia.com/ | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||