Debian Codegraph @

lib/Finance/Quote/IndiaMutual.pm

raw
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 use strict;
24
25 use constant DEBUG => $ENV{DEBUG};
26 use if DEBUG, 'Smart::Comments';
27
28 use vars qw( $AMFI_URL $AMFI_SIF_URL $AMFI_NAV_LIST $AMFI_MAIN_URL);
29
30 use LWP::UserAgent;
31 use HTTP::Request::Common;
32 use HTTP::Status;
33 use IO::String;
34
35 our $VERSION = '1.71'; # VERSION
36
37 # URLs of where to obtain information.
38
39 $AMFI_MAIN_URL = ("http://www.amfiindia.com/");
40 $AMFI_URL = ("https://portal.amfiindia.com/spages/NAVAll.txt");
41 $AMFI_SIF_URL = ("https://portal.amfiindia.com/spages/SIF_NAVAll.txt");
42
43 our $DISPLAY = 'IndiaMutual - Assoc of Mutual Funds in India';
44 our @LABELS = qw/method source link name currency date isodate nav plan option/;
45 our $METHODHASH = {subroutine => \&amfiindia,
46 display => $DISPLAY,
47 labels => \@LABELS};
48
49 sub methodinfo {
50 return (
51 amfiindia => $METHODHASH,
52 indiamutual => $METHODHASH,
53 );
54 }
55
56 sub labels {
57 my %m = methodinfo();
58 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
59 }
60
61 sub methods {
62 my %m = methodinfo();
63 return map {$_ => $m{$_}{subroutine} } keys %m;
64 }
65
66 #
67 # =======================================================================
68
69 sub amfiindia {
70 my $quoter = shift;
71 my @symbols = @_;
72
73 # Make sure symbols are requested
74
75 return unless @symbols;
76
77 # Local Variables
78 my %fundquote;
79 my %symbolhash;
80 foreach my $symbol (@symbols) {
81 $symbolhash{$symbol} = 0;
82 }
83
84 my $ua = $quoter->get_user_agent();
85 $ua->agent(""); # make user_agent as empty for a conducive server response
86
87 my @urls = ($AMFI_URL, $AMFI_SIF_URL);
88 my $http_success = 0;
89
90 foreach my $url (@urls) {
91 my @remaining = grep { ! exists $fundquote{$_, "success"} } @symbols;
92 last unless @remaining;
93
94 my $req = HTTP::Request->new(GET => $url);
95 my $reply = $ua->request($req);
96
97 next unless ($reply->is_success or $reply->code == RC_NOT_MODIFIED);
98 $http_success = 1;
99
100 my $output = $reply->content;
101 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 foreach (@array) {
106 next if !/\;/;
107 chomp;
108 s/\r//;
109
110 my @words = split(";", $_); #the delimiter is ; not a ,
111 next unless @words >= 8;
112
113 foreach my $sym ($words[0], $words[1], $words[2]) {
114 next unless defined $sym && length($sym) && $sym ne '-';
115 if (exists $symbolhash{$sym} && ! exists $fundquote{$sym, "success"}) {
116 $fundquote{$sym, "symbol"} = $sym;
117 $fundquote{$sym, "currency"} = "INR";
118 $fundquote{$sym, "source"} = $AMFI_MAIN_URL;
119 $fundquote{$sym, "link"} = $url;
120 $fundquote{$sym, "method"} = "amfiindia";
121 $fundquote{$sym, "name"} = $words[3];
122 $fundquote{$sym, "plan"} = $words[4] if defined $words[4] && length $words[4];
123 $fundquote{$sym, "option"} = $words[5] if defined $words[5] && length $words[5];
124 $fundquote{$sym, "nav"} = $words[6];
125 $quoter->store_date(\%fundquote, $sym, {eurodate => $words[7]});
126 $fundquote{$sym, "success"} = 1;
127 }
128 }
129 }
130 }
131
132 foreach my $symbol (@symbols) {
133 unless (exists $fundquote{$symbol, 'success'}) {
134 $fundquote{$symbol, 'success'} = 0;
135 $fundquote{$symbol, 'errormsg'} = $http_success ? 'Fund not found.' : 'HTTP failure';
136 }
137 }
138
139 return wantarray ? %fundquote : \%fundquote;
140 }
141
142 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