Solution: equity_history() function doesn't work

I have started using the NSEpython library since yesterday. I found a bug. Please find the fix. I am not aware who and how to update the fix to the master. Kindly someone please do the needful.

Bug: nsefetch() function doesn’t work.
Explantion: the function equity_history() and many oher functions use a function called “nsefetch()”. This function uses ‘requests’ to scrape data from nse website. But the problem what I observed is it’s not working. At this moment this function “nsefetch()” doesn’t use ‘session()’ concept to scrape data from nse.

Solution: implement the ‘sessions()’ concept of requests library. I did following modification and it started working as expected.

fix: the highlighted in bold are my changes.
s = requests.session()
mode =‘local’

if(mode==‘local’):
def nsefetch(payload):
output = s.get(payload,headers=headers).json()
return output

So the beginning part of “rahu.py” file should be something as follows-

import os,sys
# os.chdir(os.path.dirname(os.path.abspath(__file__)))
# sys.path.insert(1, os.path.join(sys.path[0], '..'))

import requests
import pandas as pd
import json
import random
import datetime,time
import logging
import re
import urllib.parse


headers = {
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'DNT': '1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
    'Sec-Fetch-User': '?1',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-Mode': 'navigate',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8',
}



s = requests.session()
url_nse = "https://www.nseindia.com"
r = s.get(url_nse, headers=headers)


mode ='local'

if(mode=='local'):
    def nsefetch(payload):
        output = s.get(payload,headers=headers).json()
        return output



Regards
Praty

2 Likes

i also faced this issue …and after your solution of session creation … now it fine

1 Like

I had it fixed in first. The last update I copied some details from some other file and fucked the stuff which was already unfucked.

No worries, I will unfuck it again, and this time, we’ll make sure it stays unfucked for good! Forgive me.

1 Like

Updating NSEPython tonight.

This was the last version’s version


if(mode=='vpn'):
    def nsefetch(payload):
        if (("%26" in payload) or ("%20" in payload)):
            encoded_url = payload
        else:
            encoded_url = urllib.parse.quote(payload, safe=':/?&=')
        payload_var = 'curl -b cookies.txt "' + encoded_url + '"' + curl_headers + ''
        try:
            output = os.popen(payload_var).read()
            output=json.loads(output)
        except ValueError:  # includes simplejson.decoder.JSONDecodeError:
            payload2 = "https://www.nseindia.com"
            output2 = os.popen('curl -c cookies.txt "'+payload2+'"'+curl_headers+'').read()
    
            output = os.popen(payload_var).read()
            output=json.loads(output)
        return output
if(mode=='local'):
    def nsefetch(payload):
        output = requests.get(payload,headers=headers).json()
        return output

You can see the issue is dealt with nsepython server version. I am basically extending the same now in normal mode.

if(mode=='local'):
    def nsefetch(payload):
        try: #Try the normal way.
            output = requests.get(payload,headers=headers).json()
        except: 
            try: # Follow Praty's hack as mentioned here at https://forum.unofficed.com/t/solution-equity-history-function-doesnt-work/1197
                output = requests.session().get(payload,headers=headers).json()

            except: #Mimick FrontPage Visit
                s=requests.session()

                payload2 = "https://www.nseindia.com"
                output2 = requests.get(payload2,headers=headers).json()

                output = requests.get(payload,headers=headers).json()
        return output

I have followed the above suggested solution. But still it is not working. I am unable to fetch nse equity details using nsefetch method.

Please suggest.

I am also facing the same issue. Did you find the solution?

Try the nsepythonserver version in Google Collab. Let me know if there is error.