#!/usr/bin/perl # # The code is developed to retrieve India NPS (National Pension # System) fund NAV information from npsnav.in through json calls. # Adapted from Finance::Quote::NPSNAV by Murugan Muruganandam, # . # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA package Finance::Quote::NPSNAV; require 5.005; use strict; use JSON qw( decode_json ); use vars qw($VERSION $NPSNAV_URL_HEAD $NPSNAV_URL_TAIL); use LWP::UserAgent; use HTTP::Request::Common; use Time::Piece; our $VERSION = '1.00'; # VERSION $NPSNAV_URL_HEAD = 'https://npsnav.in/api/detailed/'; $NPSNAV_URL_TAIL = ''; sub methods { return ( npsnav => \&npsnav, ); } { my @labels = qw/name last date isodate volume currency method exchange type div_yield eps pe year_range open high low close nav symbol pfm_code pfm_name/; sub labels { return ( npsnav => \@labels, ); } } sub npsnav { #local variables my $quoter = shift; my %info; my @stocks = @_; my $stocks; foreach $stocks (@stocks) { # Input is simply the NPS scheme code, e.g. SM001026 # Optionally a currency override can be appended with "-", # e.g. SM001026-INR (defaults to INR when not supplied). my @inputsplit = split /-/, $stocks; my $schemecode = $inputsplit[0]; my $currency = $inputsplit[1] || 'INR'; #Request my $UserAgent = new LWP::UserAgent; my $url = $NPSNAV_URL_HEAD . $schemecode . $NPSNAV_URL_TAIL; my $Request = new HTTP::Request('GET', $url); $UserAgent->default_header('Accept', 'application/json,text/plain,*/*'); $UserAgent->default_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0'); # Response my $Response = $UserAgent->request($Request); # Response Details my $code = $Response->code; my $desc = HTTP::Status::status_message($code); my $body = $Response->content; if ( $code == 200 ) { my $json_data = JSON::decode_json $body; my $pfmcode = $json_data->{'PFM Code'}; my $pfmname = $json_data->{'PFM Name'}; my $schemeid = $json_data->{'Scheme Code'}; my $schemename = $json_data->{'Scheme Name'}; my $nav = $json_data->{'NAV'}; my $lastupdate = $json_data->{'Last Updated'}; if ( defined($nav) && defined($lastupdate) ) { $info{ $stocks, "success" } = 1; $info{ $stocks, "symbol" } = $schemeid; $info{ $stocks, "exchange" } = "Sourced from npsnav.in (as JSON)"; $info{ $stocks, "method" } = "npsnav"; $info{ $stocks, "name" } = $schemename; $info{ $stocks, "currency" } = $currency; $info{ $stocks, "nav" } = $nav; $info{ $stocks, "last" } = $nav; $info{ $stocks, "close" } = $nav; $info{ $stocks, "pfm_code" } = $pfmcode; $info{ $stocks, "pfm_name" } = $pfmname; $info{ $stocks, "type" } = "NPS"; # "Last Updated" is returned as DD-MM-YYYY my @dt = split /-/, $lastupdate; my $isodate = $dt[2] . '-' . $dt[1] . '-' . $dt[0]; $info{ $stocks, "isodate" } = $isodate; my $localdt = $dt[0] . '/' . $dt[1] . '/' . $dt[2]; $info{ $stocks, "date" } = $localdt; my $my_date = $dt[0] . '.' . $dt[1] . '.' . $dt[2]; $quoter->store_date( \%info, $stocks, { eurodate => $my_date } ); } else { $info{ $stocks, "success" } = 0; $info{ $stocks, "errormsg" } = "Error retrieving quote for $stocks. Unexpected JSON structure returned from $url"; } } else { $info{ $stocks, "success" } = 0; $info{ $stocks, "errormsg" } = "Error retrieving quote for $stocks. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; } } return wantarray() ? %info : \%info; }; 1; =head1 NAME Finance::Quote::NPSNAV - Obtain India NPS (National Pension System) fund NAVs through JSON call =head1 SYNOPSIS use Finance::Quote; $q = Finance::Quote->new; %info = Finance::Quote->fetch("npsnav","SM001026"); =head1 DESCRIPTION This module fetches National Pension System (NPS) fund NAV information from npsnav.in as JSON. It provides the "npsnav" fetch method. The input is the NPS scheme code, e.g. SM001026. Currency defaults to INR and is not returned by the npsnav.in webservice; an override can optionally be appended to the scheme code with a "-", e.g. SM001026-INR. =head1 LABELS RETURNED The following labels may be returned by Finance::Quote::NPSNAV : name, last, close, date, isodate, currency, method, exchange, type, symbol, nav, pfm_code, pfm_name =head1 SEE ALSO https://npsnav.in/nps-api =cut