| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||
| 2 | # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch: | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # Copyright (C) 2026, Kalpesh Patel <wrackguard+f_and_q@gmail.com> | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | # This file is part of Finance::Quote. | ||||||||||||||||||||
| 7 | # | ||||||||||||||||||||
| 8 | # Finance::Quote is free software: you can redistribute it and/or | ||||||||||||||||||||
| 9 | # modify it under the terms of the GNU General Public License as | ||||||||||||||||||||
| 10 | # published by the Free Software Foundation, either version 2 of | ||||||||||||||||||||
| 11 | # the License, or (at your option) any later version. | ||||||||||||||||||||
| 12 | # | ||||||||||||||||||||
| 13 | # Finance::Quote is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||||||||
| 16 | # General Public License for more details. | ||||||||||||||||||||
| 17 | # | ||||||||||||||||||||
| 18 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||
| 19 | # along with Finance::Quote. | ||||||||||||||||||||
| 20 | # If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | # This code is derived from TSP.pm module. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | package Finance::Quote::NPSNAV; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG} || $ENV{NPSNAV_DEBUG}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$ENV{'DEBUG'} || $ENV{'NPSNAV_DEBUG'}
MC/DC0%0/2
$ENV{'DEBUG'} || $ENV{'NPSNAV_DEBUG'}
$ENV{'DEBUG'}
$ENV{'NPSNAV_DEBUG'}
Truth table33%1/3
A || B
A= $ENV{'DEBUG'}B= $ENV{'NPSNAV_DEBUG'}
| |||||||||||||||||||||
| 29 | 5 | ▶ | use if DEBUG, 'Smart::Comments', '###'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 5 | ▶ | use vars qw( $NPSNAV_URL $NPSNAV_MAIN_URL @HEADERS ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 5 | ▶ | use POSIX; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 5 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 5 | our $VERSION = '1.72_01'; # TRIAL VERSION | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # URLs of where to obtain information | ||||||||||||||||||||
| 41 | 5 | $NPSNAV_URL = 'https://npsnav.in/api/detailed'; | |||||||||||||||||||
| 42 | 5 | $NPSNAV_MAIN_URL = 'http://npsnav.in'; | |||||||||||||||||||
| 43 | 5 | @HEADERS = ('user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36'); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | our $DISPLAY = 'NPSNAV - India National Pension System'; | |||||||||||||||||||
| 46 | 5 | our @LABELS = qw/name lastupdated isodate currency nav/; | |||||||||||||||||||
| 47 | 5 | our $METHODHASH = {subroutine => \&npsnav, | |||||||||||||||||||
| 48 | display => $DISPLAY, | ||||||||||||||||||||
| 49 | labels => \@LABELS}; | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 10 | ▶ | sub methodinfo { | ||||||||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 52 | return ( | ||||||||||||||||||||
| 53 | 10 | npsnav => $METHODHASH, | |||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 5 | ▶ | sub labels { | ||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 58 | 5 | my %m = methodinfo(); | |||||||||||||||||||
| 59 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 5 | ▶ | sub methods { | ||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 63 | 5 | my %m = methodinfo(); | |||||||||||||||||||
| 64 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub format_name { | ||||||||||||||||||
|
Subroutine
format_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 68 | 0 | my $name = shift; | |||||||||||||||||||
| 69 | 0 | $name =~ s/ //g; | |||||||||||||||||||
| 70 | 0 | $name = lc($name); | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | return $1 if $name =~ /^(.)fund$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | return $name; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 5 | ▶ | sub currency_fields { | ||||||||||||||||||
|
Subroutine
currency_fields: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 77 | 5 | return qw/nav 1D 1M 1Y 3M 3Y 5Y 6M 7D/; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # ============================================================================== | ||||||||||||||||||||
| 81 | 0 | ▶ | sub npsnav { | ||||||||||||||||||
|
Subroutine
npsnav: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 82 | 0 | my $quoter = shift; | |||||||||||||||||||
| 83 | 0 | my @symbols = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | return unless @symbols; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | my %info; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | my $ua = $quoter->user_agent; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | ## | ||||||||||||||||||||
| 92 | ## https://npsnav.in/api/detailed/SM001001 | ||||||||||||||||||||
| 93 | ## | ||||||||||||||||||||
| 94 | ## { | ||||||||||||||||||||
| 95 | ## "Last Updated": "01-10-2024", | ||||||||||||||||||||
| 96 | ## "PFM Code": "PFM001", | ||||||||||||||||||||
| 97 | ## "PFM Name": "SBI PENSION FUNDS PRIVATE LIMITED", | ||||||||||||||||||||
| 98 | ## "Scheme Code": "SM001001", | ||||||||||||||||||||
| 99 | ## "Scheme Name": "SBI PENSION FUND SCHEME - CENTRAL GOVT", | ||||||||||||||||||||
| 100 | ## "NAV": "46.7686", | ||||||||||||||||||||
| 101 | ## "1D": "0.10", | ||||||||||||||||||||
| 102 | ## "7D": "0.13", | ||||||||||||||||||||
| 103 | ## "1M": "1.34", | ||||||||||||||||||||
| 104 | ## "3M": "3.51", | ||||||||||||||||||||
| 105 | ## "6M": "6.73", | ||||||||||||||||||||
| 106 | ## "1Y": "13.98", | ||||||||||||||||||||
| 107 | ## "3Y": "8.16", | ||||||||||||||||||||
| 108 | ## "5Y": "9.23" | ||||||||||||||||||||
| 109 | ## } | ||||||||||||||||||||
| 110 | ## | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | foreach my $symbol (@symbols) { | |||||||||||||||||||
| 113 | 0 | my $url = "$NPSNAV_URL/$symbol"; | |||||||||||||||||||
| 114 | 0 | my $reply = $ua->get($url, @HEADERS); | |||||||||||||||||||
| 115 | 0 | my $body = $reply->content; | |||||||||||||||||||
| 116 | 0 | my $success = $reply->is_success; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | ### [<now>] url : $url | ||||||||||||||||||||
| 119 | ### [<now>] reply: $reply | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | ▶ | if ($success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | $info{$symbol, 'success'} = 1; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | my $json_data = decode_json ($body); | |||||||||||||||||||
| 125 | 0 | ▶ | while (my ($key, $value) = each %$json_data) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | my $mkey = $key; | |||||||||||||||||||
| 127 | 0 | ▶ | if ($mkey eq 'NAV') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 0 | $mkey = 'nav'; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | 0 | $info{$symbol, $mkey} = $value; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | $quoter->store_date(\%info, $symbol, {eurodate => $json_data->{"Last Updated"} =~ s/-/\//gr}); # not sure if dash separator is correct, but it works for now | |||||||||||||||||||
| 134 | 0 | $info{$symbol, 'method'} = 'npsnav'; | |||||||||||||||||||
| 135 | 0 | $info{$symbol, 'source'} = $NPSNAV_MAIN_URL; | |||||||||||||||||||
| 136 | 0 | $info{$symbol, 'symbol'} = $symbol; | |||||||||||||||||||
| 137 | 0 | $info{$symbol, 'currency'} = 'INR'; | |||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||
| 139 | 0 | $info{$symbol, "success"} = 0; | |||||||||||||||||||
| 140 | 0 | $info{$symbol, "errormsg"} = "NPSNAV fetch failed. No data for $symbol. ".$reply->status_line; | |||||||||||||||||||
| 141 | ### Failure: %info | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | ▶ | return %info if wantarray; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | return \%info; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | 5 | 1; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head1 NAME | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Finance::Quote::NPSNAV - Obtain fund prices for India's National Pension Scheme (NPS) funds. | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =head1 SYNOPSIS | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | use Finance::Quote; | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | $q = Finance::Quote->new; | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | %info = $q->fetch('npsnav','SM001001'); #get quotes for SBI PENSION FUND SCHEME - CENTRAL GOVT | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =head1 DESCRIPTION | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | This module fetches NAV for pension schemes from the India's National Pension System. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | http://npsnav.in | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head1 LABELS RETURNED | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | The following labels are returned by Finance::Quote::NPSNAV : | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | lastupdated latest date, eg. "21/02/10" | ||||||||||||||||||||
| 173 | isodate latest date, eg. "2010-02-21" | ||||||||||||||||||||
| 174 | nav latest available price, eg. "16.1053" | ||||||||||||||||||||
| 175 | currency "INR" | ||||||||||||||||||||
| 176 | method "npsnav" | ||||||||||||||||||||
| 177 | source NPSNAV URL | ||||||||||||||||||||
| 178 | 1D 7D 1M 3M 6M 1Y 3Y 5Y performance metrics 1-day to 5-year | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||