Quote/NSEIndia.pm

filter: Statementstmt 35.1% 33 / 94 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 29.5% 42 / 142 CC 19 SCAR 55.5
CC
19
Cov
27%
nseindia
380.0
BEGIN
1.0
BEGIN
1.0
CRAP 258.2
« Quote/YahooJSON.pm ↑ Summary Quote/FinanceAPI.pm »
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 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use IO::Uncompress::Unzip qw(unzip $UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 our $VERSION = '1.72_01'; # TRIAL VERSION
29  
30 5 ▶use vars qw($NSE_MAIN_URL $NSE_URL);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 $NSE_MAIN_URL = "https://www.nseindia.com";
32 5 $NSE_URL = "https://nsearchives.nseindia.com";
33  
34 5 our $DISPLAY = 'NSEIndia - NSE India';
35 5 our @LABELS = qw/close last high low open prevclose exchange name/;
36 5 our $METHODHASH = {subroutine => \&nseindia,
37 display => $DISPLAY,
38 labels => \@LABELS};
39  
40 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
41 return (
42 10 nseindia => $METHODHASH,
43 india => $METHODHASH,
44 );
45 }
46  
47 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
48 5 my %m = methodinfo();
49 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
50 }
51  
52 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
53 5 my %m = methodinfo();
54 5 return map {$_ => $m{$_}{subroutine} } keys %m;
55 }
56  
57 0 ▶sub nseindia {
Subroutine nseindia: not called CC=19 SCAR=59.4
Pod undocumented
58 0 my $quoter = shift;
59 0 my @symbols = @_;
60 0 ▶ return unless @symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
61  
62 0 my (%info, $errormsg, $fh, $ua, $url, $reply);
63 0 my $output;
64 0 my @array;
65  
66 0 $ua = $quoter->user_agent;
67 # Disable redirects - server redirects instead of 404.
68 0 $ua->requests_redirectable([]);
69  
70 # Set the ua to be blank. Server blocks default useragent.
71 0 $ua->agent('');
72
73 # Try to fetch last 10 days
74 0 ▶ for (my ($days, $now) = (0, time()); $days < 10; $days++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($days < 10) 0 0 0
75 # Ex: https://nsearchives.nseindia.com/content/cm/BhavCopy_NSE_CM_0_0_0_20240718_F_0000.csv.zip
76 0 my @lt = localtime($now - $days*24*60*60);
77 0 my ($date, $day, $month, $year, $url, $req, $output);
78  
79 0 $date = strftime "%Y%m%d", @lt;
80 0 $url = sprintf("https://nsearchives.nseindia.com/content/cm/BhavCopy_NSE_CM_0_0_0_%s_F_0000.csv.zip", $date);
81 0 $req = HTTP::Request->new(GET => $url); #added for fileless
82 0 $reply = $ua->request($req);
83 # print "$url", $reply->is_success, $reply->status_line, "\n"; #DEBUG
84 0 ▶ if ($reply->is_success or $reply->code == 304) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->is_success or $reply->code == 304) 0 0 0
Condition0%0/3
$reply->is_success or $reply->code == 304
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
85 0 last;
86 }
87 }
88  
89 0 ▶ if (!$reply->is_success and $reply->code != 304) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $reply->is_success and $reply->code != 304) 0 0 0
Condition0%0/3
not $reply->is_success and $reply->code != 304
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
90 0 $errormsg = "HTTP failure : " . $reply->status_line;
91 }
92  
93 0 ▶ if (!$errormsg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($errormsg) 0 0 0
94 #Does not use temp files. Fileless into variable $output
95 0 ▶ if (! unzip \$reply->content => \$output) {
Branch: 1 branch
BranchTrueFalseTotal
if (not unzip(\$reply->content, \$output)) { } 0 0 0
96 0 $errormsg = "Unzip error : $UnzipError";
97 } else {
98 0 @array = split("\n", $output);
99 }
100 }
101  
102 0 ▶ if ($errormsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errormsg) 0 0 0
103 0 foreach my $symbol (@symbols) {
104 0 $info{$symbol, "success"} = 0;
105 0 $info{$symbol, "errormsg"} = $errormsg;
106 }
107 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
108 }
109  
110 # Create a hash of all stocks requested
111 0 my %symbolhash;
112 0 foreach my $symbol (@symbols)
113 {
114 0 $symbolhash{$symbol} = 0;
115 }
116 0 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 0 $csvhead = $array[0];
122  
123 0 @headhash = split /\s*,s*/, $csvhead;
124 0 foreach (@array) {
125 0 my @data = split /\s*,s*/;
126 0 my %datahash;
127 my $symbol;
128 0 @datahash{@headhash} = @data;
129 0 ▶ if (exists $symbolhash{$datahash{"TckrSymb"}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $symbolhash{$datahash{'TckrSymb'}}) { } 0 0 0
elsif (exists $symbolhash{$datahash{'ISIN'}}) { } 0 0 0
130 0 $symbol = $datahash{"TckrSymb"};
131 }
132 elsif(exists $symbolhash{$datahash{"ISIN"}}) {
133 0 $symbol = $datahash{"ISIN"};
134 }
135 else {
136 0 next;
137 }
138 0 $info{$symbol, 'symbol'} = $symbol;
139 0 $info{$symbol, 'close'} = $datahash{"ClsPric"};
140 0 $info{$symbol, 'last'} = $datahash{"LastPric"};
141 0 $info{$symbol, 'high'} = $datahash{"HghPric"};
142 0 $info{$symbol, 'low'} = $datahash{"LwPric"};
143 0 $info{$symbol, 'open'} = $datahash{"OpnPric"};
144 0 $info{$symbol, 'prevclose'} = $datahash{"PrvsClsgPric"};
145 0 $info{$symbol, 'name'} = $datahash{"FinInstrmNm"};
146 0 $quoter->store_date(\%info, $symbol, {isodate => $datahash{"TradDt"}});
147 0 $info{$symbol, 'method'} = 'nseindia';
148 0 $info{$symbol, 'currency'} = 'INR';
149 0 $info{$symbol, 'exchange'} = 'NSE';
150 0 $info{$symbol, 'success'} = 1;
151 }
152  
153 0 foreach my $symbol (@symbols) {
154 0 ▶ unless (exists $info{$symbol, 'success'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $info{join $;, $symbol, "success"}) 0 0 0
155 0 $info{$symbol, 'success'} = 0;
156 0 $info{$symbol, 'errormsg'} = 'Stock not found on NSE.';
157 }
158 }
159  
160 0 ▶ return wantarray ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
161 }
162  
163  
164 5 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
« Quote/YahooJSON.pm ↑ Summary Quote/FinanceAPI.pm »