From 8e85177f32b9d45ce8de1bff80cbf336bb9b2e8f Mon Sep 17 00:00:00 2001 From: 0xmohitsen Date: Wed, 29 Jul 2026 16:32:19 +0530 Subject: [PATCH] Fixed : updated the niftyindice endpoint that returns index_history, index_pe_pb_div, index_total_returns --- nsepython/rahu.py | 75 +++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/nsepython/rahu.py b/nsepython/rahu.py index 5929a71b..094175f7 100644 --- a/nsepython/rahu.py +++ b/nsepython/rahu.py @@ -974,43 +974,60 @@ def expiry_history(symbol,start_date="",end_date="",type="options"): # # Nifty Indicies Site +# # Nifty Indicies Site + niftyindices_headers = { - 'Connection': 'keep-alive', - 'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"', 'Accept': 'application/json, text/javascript, */*; q=0.01', - 'DNT': '1', + 'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8', + 'Content-Type': 'application/json; charset=UTF-8', + 'Origin': 'https://www.niftyindices.com', + 'Referer': 'https://www.niftyindices.com/reports/historical-data', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest', + 'sec-ch-ua': '"Not;A=Brand";v="8", "Chromium";v="150", "Google Chrome";v="150"', 'sec-ch-ua-mobile': '?0', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36', - 'Content-Type': 'application/json; charset=UTF-8', - 'Origin': 'https://niftyindices.com', - 'Sec-Fetch-Site': 'same-origin', - 'Sec-Fetch-Mode': 'cors', - 'Sec-Fetch-Dest': 'empty', - 'Referer': 'https://niftyindices.com/reports/historical-data', - 'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8', + 'sec-ch-ua-platform': '"Windows"', } -def index_history(symbol,start_date,end_date): - data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"} - payload = requests.post('https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString', headers=niftyindices_headers, json=data).json() - payload = json.loads(payload["d"]) - payload=pd.DataFrame.from_records(payload) - return payload +def _niftyindices_fetch(endpoint, symbol, start_date, end_date): + session = requests.Session() + session.get( + "https://www.niftyindices.com/reports/historical-data", + headers=niftyindices_headers, + timeout=15, + ) -def index_pe_pb_div(symbol,start_date,end_date): - data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"} - payload = requests.post('https://niftyindices.com/Backpage.aspx/getpepbHistoricaldataDBtoString', headers=niftyindices_headers, json=data).json() - payload = json.loads(payload["d"]) - payload=pd.DataFrame.from_records(payload) - return payload + data = { + 'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}" + } -def index_total_returns(symbol,start_date,end_date): - data = {'cinfo': "{'name':'" + symbol + "','startDate':'" + start_date + "','endDate':'" + end_date + "','indexName':'" + symbol + "'}"} - payload = requests.post('https://niftyindices.com/Backpage.aspx/getTotalReturnIndexString', headers=niftyindices_headers, json=data).json() - payload = json.loads(payload["d"]) - payload=pd.DataFrame.from_records(payload) - return payload + response = session.post( + f"https://www.niftyindices.com/BackPage/{endpoint}", + headers=niftyindices_headers, + json=data, + timeout=15, + ) + + text = response.text.strip() + if text.startswith('