File Coverage

blib/lib/Finance/Quote/IndiaMutual.pm
Criterion Covered Total %
statement 26 69 37.6
branch 0 18 0.0
condition 0 3 0.0
subroutine 10 11 90.9
pod 0 3 0.0
total 36 104 34.6


line stmt bran cond sub pod time code
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     5   3950 use strict;
  5         15  
  5         245  
24              
25 5     5   34 use constant DEBUG => $ENV{DEBUG};
  5         16  
  5         340  
26 5     5   50 use if DEBUG, 'Smart::Comments';
  5         15  
  5         59  
27              
28 5     5   239 use vars qw( $AMFI_URL $AMFI_NAV_LIST $AMFI_MAIN_URL);
  5         13  
  5         399  
29              
30 5     5   43 use LWP::UserAgent;
  5         14  
  5         93  
31 5     5   196 use HTTP::Request::Common;
  5         15  
  5         449  
32 5     5   43 use HTTP::Status;
  5         23  
  5         1916  
33 5     5   2960 use IO::String;
  5         14023  
  5         62  
34              
35             our $VERSION = '1.57_03'; # TRIAL VERSION
36              
37             # URLs of where to obtain information.
38              
39             $AMFI_MAIN_URL = ("http://www.amfiindia.com/");
40             $AMFI_URL = ("https://www.amfiindia.com/spages/NAVAll.txt");
41              
42 5     5 0 38 sub methods { return (indiamutual => \&amfiindia,
43             amfiindia => \&amfiindia); }
44              
45             {
46             my @labels = qw/method source link name currency date isodate nav/;
47 5     5 0 22 sub labels { return (indiamutual => \@labels,
48             amfiindia => \@labels); }
49             }
50              
51             #
52             # =======================================================================
53              
54             sub amfiindia {
55 0     0 0   my $quoter = shift;
56 0           my @symbols = @_;
57              
58             # Make sure symbols are requested
59             ##CAN exit more gracefully - add later##
60              
61 0 0         return unless @symbols;
62              
63             # Local Variables
64 0           my %fundquote;
65 0           my($ua, $url, $reply);
66              
67 0           $ua = $quoter->user_agent;
68              
69 0           $url = "$AMFI_URL";
70              
71             ### cache : $AMFI_NAV_LIST
72            
73 0           $reply = $ua->get($url);
74              
75             # Make sure something is returned
76 0 0 0       unless ($reply->is_success or $reply->code == RC_NOT_MODIFIED) {
77 0           foreach my $symbol (@symbols) {
78 0           $fundquote{$symbol,"success"} = 0;
79 0           $fundquote{$symbol,"errormsg"} = "HTTP failure";
80             }
81 0 0         return wantarray ? %fundquote : \%fundquote;
82             }
83              
84             # Attach body of response to IO::String object for file-like processing
85 0           my $nav_fh = IO::String->new($reply->content)
86             ;
87             # Create a hash of all stocks requested
88 0           my %symbolhash;
89 0           foreach my $symbol (@symbols)
90             {
91 0           $symbolhash{$symbol} = 0;
92             }
93              
94             #Scheme Code;ISIN Div Payout/ ISIN Growth;ISIN Div Reinvestment;Scheme Name;Net Asset Value;Date
95 0           while (<$nav_fh>) {
96 0 0         next if !/\;/;
97 0           chomp;
98 0           s/\r//;
99 0           my ($symbol1, $symbol2, $symbol3, $name, $nav, $date) = split /\s*\;\s*/;
100 0           my $symbol;
101 0 0         if (exists $symbolhash{$symbol1}) {
    0          
    0          
102 0           $symbol = $symbol1;
103             }
104             elsif(exists $symbolhash{$symbol2}) {
105 0           $symbol = $symbol2;
106             }
107             elsif(exists $symbolhash{$symbol3}) {
108 0           $symbol = $symbol3;
109             }
110             else {
111 0           next;
112             }
113 0           $fundquote{$symbol, "symbol"} = $symbol;
114 0           $fundquote{$symbol, "currency"} = "INR";
115 0           $fundquote{$symbol, "source"} = $AMFI_MAIN_URL;
116 0           $fundquote{$symbol, "link"} = $url;
117 0           $fundquote{$symbol, "method"} = "amfiindia";
118 0           $fundquote{$symbol, "name"} = $name;
119 0           $fundquote{$symbol, "nav"} = $nav;
120 0           $quoter->store_date(\%fundquote, $symbol, {eurodate => $date});
121 0           $fundquote{$symbol, "success"} = 1;
122             }
123 0           close($nav_fh);
124              
125 0           foreach my $symbol (@symbols) {
126 0 0         unless (exists $fundquote{$symbol, 'success'}) {
127 0           $fundquote{$symbol, 'success'} = 0;
128 0           $fundquote{$symbol, 'errormsg'} = 'Fund not found.';
129             }
130             }
131              
132 0 0         return wantarray ? %fundquote : \%fundquote;
133             }
134              
135             1;
136              
137             =head1 NAME
138              
139             Finance::Quote::IndiaMutual - Obtain Indian mutual fund prices from amfiindia.com
140              
141             =head1 SYNOPSIS
142              
143             use Finance::Quote;
144              
145             $q = Finance::Quote->new;
146              
147             # Can failover to other methods
148             %stockinfo = $q->fetch("indiamutual", "amfiindia-code");
149              
150             # Use this module only
151             %stockinfo = $q->fetch("amfiindia", "amfiindia-code");
152              
153             # NOTE: currently no failover methods exist for indiamutual
154              
155             =head1 DESCRIPTION
156              
157             This module obtains information about Indian Mutual Fund prices from the
158             Association of Mutual Funds India website amfiindia.com.
159             The information source "indiamutual" can be used
160             if the source of prices is irrelevant, and "amfiindia" if you
161             specifically want to use information downloaded from amfiindia.com.
162              
163             =head1 AMFIINDIA-CODE/ISIN
164              
165             In India, not all funds have an ISIN. However, they do have a scheme code.
166             You can use those if you can't find the ISIN. See AMFI site for details.
167             http://www.amfiindia.com/nav-history-download
168              
169             =head1 LABELS RETURNED
170              
171             Information available from amfiindia may include the following labels:
172              
173             =over
174              
175             =item method
176              
177             =item link
178              
179             =item source
180              
181             =item name
182              
183             =item currency
184              
185             =item nav
186              
187             =back
188              
189             The link label will be a url location for the NAV list table for all funds.
190              
191             =head1 NOTES
192              
193             AMFI provides a link to download a text file containing all the
194             L<NAVs|https://www.amfiindia.com/spages/NAVAll.txt>. It is processed
195             in memory using the L<IO::String> Perl module.
196              
197             =head1 SEE ALSO
198              
199             AMFI india website - http://www.amfiindia.com/
200              
201             Finance::Quote
202              
203             =cut