Debian Codegraph @

lib/Finance/Quote/NSEIndia.pm

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