Debian Codegraph @

lib/Finance/Quote/BSEIndia.pm

raw
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 use strict;
21 use POSIX qw(strftime);
22 #use IO::Uncompress::Unzip qw(unzip $UnzipError);
23
24 use constant DEBUG => $ENV{DEBUG};
25 use if DEBUG, 'Smart::Comments', '###';
26
27 our $VERSION = '1.71'; # VERSION
28
29 use vars qw($BSE_URL);
30 $BSE_URL = "https://www.bseindia.com";
31
32 our $DISPLAY = 'BSE India';
33 our $FEATURES = {};
34 our @LABELS = qw/close last high low open prevclose exchange name/;
35 our $METHODHASH = {subroutine => \&bseindia,
36 display => $DISPLAY,
37 labels => \@LABELS,
38 features => $FEATURES};
39
40 sub methodinfo {
41 return (
42 bseindia => $METHODHASH,
43 india => $METHODHASH,
44 );
45 }
46
47 sub methods {
48 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
49 }
50
51 sub labels {
52 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
53 }
54
55 sub bseindia {
56 my $quoter = shift;
57 my @symbols = @_;
58 return unless @symbols;
59
60 my (%info, $errormsg, $fh, $ua, $url, $reply);
61 my $output;
62 my @array;
63 my $meuradate;
64
65 $ua = $quoter->user_agent;
66 # Set the ua to be blank. Server blocks default useragent.
67 $ua->agent('');
68
69 # Try to fetch last 10 days
70 for (my ($days, $now) = (0, time()); $days < 10; $days++) {
71 # Ex: https://www.bseindia.com/download/BhavCopy/Equity/BhavCopy_BSE_CM_0_0_0_20240718_F_0000.CSV
72 my @lt = localtime($now - $days*24*60*60);
73 my ($date, $url, $req, $output); # added $req, $output for fileless
74
75 $date = strftime "%Y%m%d", @lt;
76 $url = sprintf("https://www.bseindia.com/download/BhavCopy/Equity/BhavCopy_BSE_CM_0_0_0_%s_F_0000.CSV", $date);
77
78 $req = HTTP::Request->new(GET => $url); #added for fileless
79 $reply = $ua->request($req);
80 #print "$url", $reply->is_success, $reply->status_line, "\n"; #DEBUG
81 if ($reply->is_success or $reply->code == 304) {
82 last;
83 }
84 }
85
86 if (!$reply->is_success and $reply->code != 304) {
87 $errormsg = "HTTP failure : " . $reply->status_line;
88 }
89
90 if (!$errormsg) {
91 #Does not use temp files. Fileless into variable $output
92 #There is no zip file anymore
93 #@array = split("\n", $output);
94 @array = split("\n", $reply->content);
95 }
96
97 if ($errormsg) {
98 foreach my $symbol (@symbols) {
99 $info{$symbol, "success"} = 0;
100 $info{$symbol, "errormsg"} = $errormsg;
101 }
102 return wantarray() ? %info : \%info;
103 }
104
105 # Create a hash of all stocks requested
106 my %symbolhash;
107 foreach my $symbol (@symbols)
108 {
109 $symbolhash{$symbol} = 0;
110 }
111 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 $csvhead = $array[0];
117
118 @headhash = split /\s*,s*/, $csvhead;
119 foreach (@array) {
120 my @data = split(",", $_);
121 my %datahash;
122 my $symbol;
123 @datahash{@headhash} = @data;
124
125 if (exists $symbolhash{$datahash{"FinInstrmId"}}) {
126 $symbol = $datahash{"FinInstrmId"};
127 }
128 elsif(exists $symbolhash{$datahash{"ISIN"}}) {
129 $symbol = $datahash{"ISIN"};
130 }
131 else {
132 next;
133 }
134
135 $info{$symbol, 'symbol'} = $symbol;
136 $info{$symbol, 'close'} = $datahash{"ClsPric"};
137 $info{$symbol, 'last'} = $datahash{"LastPric"};
138 $info{$symbol, 'high'} = $datahash{"HghPric"};
139 $info{$symbol, 'low'} = $datahash{"LwPric"};
140 $info{$symbol, 'open'} = $datahash{"OpnPric"};
141 $info{$symbol, 'prevclose'} = $datahash{"PrvsClsgPric"};
142 $info{$symbol, 'name'} = $datahash{"FinInstrmNm"};
143 $quoter->store_date(\%info, $symbol, {isodate => $datahash{"TradDt"}});
144 $info{$symbol, 'method'} = 'bseindia';
145 $info{$symbol, 'currency'} = 'INR';
146 $info{$symbol, 'exchange'} = 'BSE';
147 $info{$symbol, 'success'} = 1;
148 }
149
150 foreach my $symbol (@symbols) {
151 unless (exists $info{$symbol, 'success'}) {
152 ### Not Found: $symbol
153 $info{$symbol, 'success'} = 0;
154 $info{$symbol, 'errormsg'} = 'Stock not found on BSE.';
155 }
156 }
157
158 return wantarray ? %info : \%info;
159 }
160
161
162 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