Quote/IndiaMutual.pm

filter: Statementstmt 47.0% 40 / 85 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 4 totaltotal 33.1% 51 / 154 CC 24 SCAR 60.2
CC
24
Cov
31%
amfiindia
600.0
BEGIN
1.0
BEGIN
1.0
CRAP 411.7
« Quote/TreasuryDirect.pm ↑ Summary Quote/CurrencyRates/AllRatesToday.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai 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 # Version 0.1 preliminary version using Cdnfundlibrary.pm v0.4 as an example
20  
21 package Finance::Quote::IndiaMutual;
22  
23 5 ▶use strict;
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 ▶use vars qw( $AMFI_URL $AMFI_SIF_URL $AMFI_NAV_LIST $AMFI_MAIN_URL);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 5 our $VERSION = '1.72_01'; # TRIAL VERSION
36  
37 # URLs of where to obtain information.
38  
39 5 $AMFI_MAIN_URL = ("http://www.amfiindia.com/");
40 5 $AMFI_URL = ("https://portal.amfiindia.com/spages/NAVAll.txt");
41 5 $AMFI_SIF_URL = ("https://portal.amfiindia.com/spages/SIF_NAVAll.txt");
42  
43 5 our $DISPLAY = 'IndiaMutual - Assoc of Mutual Funds in India';
44 5 our @LABELS = qw/method source link name currency date isodate nav plan option/;
45 5 our $METHODHASH = {subroutine => \&amfiindia,
46 display => $DISPLAY,
47 labels => \@LABELS};
48  
49 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
50 return (
51 10 amfiindia => $METHODHASH,
52 indiamutual => $METHODHASH,
53 );
54 }
55  
56 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
57 5 my %m = methodinfo();
58 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
59 }
60  
61 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
62 5 my %m = methodinfo();
63 5 return map {$_ => $m{$_}{subroutine} } keys %m;
64 }
65  
66 #
67 # =======================================================================
68  
69 0 ▶sub amfiindia {
Subroutine amfiindia: not called CC=24 SCAR=64.0
Pod undocumented
70 0 my $quoter = shift;
71 0 my @symbols = @_;
72  
73 # Make sure symbols are requested
74  
75 0 ▶ return unless @symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
76  
77 # Local Variables
78 0 my %fundquote;
79 my %symbolhash;
80 0 foreach my $symbol (@symbols) {
81 0 $symbolhash{$symbol} = 0;
82 }
83  
84 0 my $ua = $quoter->get_user_agent();
85 0 $ua->agent(""); # make user_agent as empty for a conducive server response
86  
87 0 my @urls = ($AMFI_URL, $AMFI_SIF_URL);
88 0 my $http_success = 0;
89  
90 0 foreach my $url (@urls) {
91 0 my @remaining = grep { ! exists $fundquote{$_, "success"} } @symbols;
92 0 ▶ last unless @remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless @remaining 0 0 0
93  
94 0 my $req = HTTP::Request->new(GET => $url);
95 0 my $reply = $ua->request($req);
96  
97 0 ▶ next unless ($reply->is_success or $reply->code == RC_NOT_MODIFIED);
Branch: 1 branch
BranchTrueFalseTotal
unless $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
98 0 $http_success = 1;
99  
100 0 my $output = $reply->content;
101 0 my @array = split("\n", $output);
102  
103 # Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Plan;Option;Net Asset Value;Date
104 # Note it is best to use Scheme Code as not all rows have ISINs in the source file
105 0 foreach (@array) {
106 0 ▶ next if !/\;/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\;/ 0 0 0
107 0 chomp;
108 0 s/\r//;
109  
110 0 my @words = split(";", $_); #the delimiter is ; not a ,
111 0 ▶ next unless @words >= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @words >= 8 0 0 0
112  
113 0 foreach my $sym ($words[0], $words[1], $words[2]) {
114 0 ▶ next unless defined $sym && length($sym) && $sym ne '-';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sym and length $sym and $sym ne "-" 0 0 0
Condition0%0/6
defined $sym and length $sym and $sym ne "-"
!ll&&!rl&&r
000
defined $sym and length $sym
!ll&&!rl&&r
000
MC/DC0%0/3
defined $sym and length $sym and $sym ne "-"
defined $sym length $sym $sym ne "-"
Truth table0%0/4
A and B and C
A= defined $symB= length $symC= $sym ne "-"
ABCresult
0XX0
10X0
1100
1111
115 0 ▶ if (exists $symbolhash{$sym} && ! exists $fundquote{$sym, "success"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $symbolhash{$sym} and not exists $fundquote{join $;, $sym, "success"}) 0 0 0
Condition0%0/3
exists $symbolhash{$sym} and not exists $fundquote{join $;, $sym, "success"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $symbolhash{$sym} and not exists $fundquote{join $;, $sym, "success"}
exists $symbolhash{$sym} not exists $fundquote{join $;, $sym, "success"}
Truth table0%0/3
A and not(B)
A= exists $symbolhash{$sym}B= not exists $fundquote{join $;, $sym, "success"}
ABresult
0X0
100
111
116 0 $fundquote{$sym, "symbol"} = $sym;
117 0 $fundquote{$sym, "currency"} = "INR";
118 0 $fundquote{$sym, "source"} = $AMFI_MAIN_URL;
119 0 $fundquote{$sym, "link"} = $url;
120 0 $fundquote{$sym, "method"} = "amfiindia";
121 0 $fundquote{$sym, "name"} = $words[3];
122 0 ▶ $fundquote{$sym, "plan"} = $words[4] if defined $words[4] && length $words[4];
Branch: 1 branch
BranchTrueFalseTotal
if defined $words[4] and length $words[4] 0 0 0
Condition0%0/3
defined $words[4] and length $words[4]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $words[4] and length $words[4]
defined $words[4] length $words[4]
Truth table0%0/3
A and B
A= defined $words[4]B= length $words[4]
ABresult
0X0
100
111
123 0 ▶ $fundquote{$sym, "option"} = $words[5] if defined $words[5] && length $words[5];
Branch: 1 branch
BranchTrueFalseTotal
if defined $words[5] and length $words[5] 0 0 0
Condition0%0/3
defined $words[5] and length $words[5]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $words[5] and length $words[5]
defined $words[5] length $words[5]
Truth table0%0/3
A and B
A= defined $words[5]B= length $words[5]
ABresult
0X0
100
111
124 0 $fundquote{$sym, "nav"} = $words[6];
125 0 $quoter->store_date(\%fundquote, $sym, {eurodate => $words[7]});
126 0 $fundquote{$sym, "success"} = 1;
127 }
128 }
129 }
130 }
131  
132 0 foreach my $symbol (@symbols) {
133 0 ▶ unless (exists $fundquote{$symbol, 'success'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fundquote{join $;, $symbol, "success"}) 0 0 0
134 0 $fundquote{$symbol, 'success'} = 0;
135 0 ▶ $fundquote{$symbol, 'errormsg'} = $http_success ? 'Fund not found.' : 'HTTP failure';
Branch: 1 branch
BranchTrueFalseTotal
$http_success ? : 0 0 0
136 }
137 }
138  
139 0 ▶ return wantarray ? %fundquote : \%fundquote;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
140 }
141  
142 5 1;
143  
144 =head1 NAME
145  
146 Finance::Quote::IndiaMutual - Obtain Indian mutual fund prices from amfiindia.com
147  
148 =head1 SYNOPSIS
149  
150 use Finance::Quote;
151  
152 $q = Finance::Quote->new;
153  
154 # Can failover to other methods
155 %stockinfo = $q->fetch("indiamutual", "amfiindia-code");
156  
157 # Use this module only
158 %stockinfo = $q->fetch("amfiindia", "amfiindia-code");
159  
160 # NOTE: currently no failover methods exist for indiamutual
161  
162 =head1 DESCRIPTION
163  
164 This module obtains information about Indian Mutual Fund prices from the
165 Association of Mutual Funds India website amfiindia.com.
166 The information source "indiamutual" can be used
167 if the source of prices is irrelevant, and "amfiindia" if you
168 specifically want to use information downloaded from amfiindia.com.
169  
170 =head1 AMFIINDIA-CODE/ISIN
171  
172 In India, not all funds have an ISIN. However, they do have a scheme code.
173 You can use those if you can't find the ISIN. See AMFI site for details.
174 http://www.amfiindia.com/nav-history-download
175  
176 =head1 LABELS RETURNED
177  
178 Information available from amfiindia may include the following labels:
179  
180 =over
181  
182 =item method
183  
184 =item link
185  
186 =item source
187  
188 =item name
189  
190 =item currency
191  
192 =item nav
193  
194 =item plan
195  
196 =item option
197  
198 =back
199  
200 The link label will be a url location for the NAV list table for all funds.
201  
202 =head1 NOTES
203  
204 AMFI provides links to download text files containing all the
205 L<NAVs|https://www.amfiindia.com/spages/NAVAll.txt> and
206 L<SIF NAVs|https://portal.amfiindia.com/spages/SIF_NAVAll.txt>. They are processed
207 in memory using the L<IO::String> Perl module.
208  
209 =head1 SEE ALSO
210  
211 AMFI india website - http://www.amfiindia.com/
212  
213 Finance::Quote
214  
215 =cut
« Quote/TreasuryDirect.pm ↑ Summary Quote/CurrencyRates/AllRatesToday.pm »