Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved401Error #31

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion nsepython/rahu.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def nsefetch(payload):
if(mode=='local'):
def nsefetch(payload):
try: #Try the normal way.
cookies = get_cookies_from_nse()
headerCookies = ""
for cookie in cookies:
headerCookies += f"{cookie.name}={cookie.value};"
headers['Cookie'] = headerCookies
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
Expand Down Expand Up @@ -62,6 +67,14 @@ def nsefetch(payload):
'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8',
}

def get_cookies_from_nse():
try:
url = "https://www.nseindia.com/" # Replace with your desired URL
response = requests.get(url, headers=headers)
return response.cookies
except:
# print(f"Error while fetching cookies: {e}")
return None
#Curl headers
curl_headers = ''' -H "authority: beta.nseindia.com" -H "cache-control: max-age=0" -H "dnt: 1" -H "upgrade-insecure-requests: 1" -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" -H "sec-fetch-user: ?1" -H "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" -H "sec-fetch-site: none" -H "sec-fetch-mode: navigate" -H "accept-encoding: gzip, deflate, br" -H "accept-language: en-US,en;q=0.9,hi;q=0.8" --compressed'''

Expand Down Expand Up @@ -899,4 +912,4 @@ def nse_expirydetails_by_symbol(symbol,meta ="Futures",i=0):
currentExpiry=expiry_dates[i]
currentExpiry = datetime.datetime.strptime(currentExpiry,'%d-%b-%Y').date()
dte = (currentExpiry - datetime.datetime.now().date()).days
return currentExpiry,dte
return currentExpiry,dte