New nsepythonserver not fetching option chain data. Will using a ROTATING PROXY be a good option to integrate with nsepythonserver?

NSEPython
nsepythonserver not fetching option chain data

When I run this:

from nsepythonserver import *

# from nsepython import *

logging.basicConfig(level=logging.DEBUG)

oi_data, ltp, crontime = oi_chain_builder("NIFTY","latest","full")

print(oi_data)

print(ltp)

print(crontime)

I get this error:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

However, no error , if I change the import statement to the old one,
"from nsepythonserver import * to --> from nsepython import *"

Full Error:

curl: option --compressed: the installed libcurl version doesn't support this
curl: try 'curl --help' for more information
curl: option --compressed: the installed libcurl version doesn't support this
curl: try 'curl --help' for more information
curl: option --compressed: the installed libcurl version doesn't support this
curl: try 'curl --help' for more information
Traceback (most recent call last):
  File "C:\Workspace\Trading\tradingview_websockets\tradingview-scraper\oichain.py", line 28, in <module>
    oi_data, ltp, crontime = oi_chain_builder("NIFTY","latest","full")
  File "C:\python\PYTHON~2\lib\site-packages\nsepythonserver\rahu.py", line 93, in oi_chain_builder
    payload = nse_optionchain_scrapper(symbol)
  File "C:\python\PYTHON~2\lib\site-packages\nsepythonserver\rahu.py", line 85, in nse_optionchain_scrapper
    payload = nsefetch('https://www.nseindia.com/api/option-chain-indices?symbol='+symbol)
  File "C:\python\PYTHON~2\lib\site-packages\nsepythonserver\rahu.py", line 31, in nsefetch
    output=json.loads(output)
  File "C:\python\PYTHON~2\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\python\PYTHON~2\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\python\PYTHON~2\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Update:
Randomly getting this error, even with : from nsepython import *
Error:
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

  • Bug Reporting

Will using a ROTATING PROXY be a good option to integrate with nsepythonserver ?
Reference: Rotating Proxies For Web Requests in Python - YouTube

nsepythonserver Reference: [Update] NSEPython not working in AWS, Google Cloud and WebServers - #23 by dexter

If nsepython runs , nsepythonserver will throw an error and vice versa.If nsepython runs fine, why you are exploring with the other library.

Thanks for the reply. Coz I want to use the nsepythonserver on AWS. that’s why

Use directly there. Then only you can debug. It will not run in local system. Windows won’t allow it!

Also, i am randomly getting this error, when i make serveral requests in a minute. is there any ristriction NSE is putting here?

God knows. Unless I can replicate, I can not solve in that sense. Figure out something.

But curl method will definitely will not work in Windows FileSystem. I think you are messing with both imports.

Do a fresh nsepython only on windows. And fresh nsepythonserver in AWS

how do i execute this line : oi_data, ltp, crontime = oi_chain_builder(symbol,“latest”,“full”)
only when response.status_code = 200 ?

In your Rahu.py file I made the following changes:

def nsefetch(payload):
response = requests.get(payload, headers=headers)
content_type = response.headers.get('Content-Type')
encoding = response.encoding

print(f"Content-Type: {content_type}")
print(f"Encoding: {encoding}")
print(f'response.status_code: {response.status_code}')

if response.status_code != 200:
    print(f"Error: {response.status_code}")
    return None

# try:
#     output = response.json()
# except json.decoder.JSONDecodeError as e:
#     print(f"Invalid JSON: {e}")
#     return None

# if output:
#     with open('nsepython_json_output_response.json', 'w') as f:
#         json.dump(output, f, indent=4)
#     return output
# else:
#     print('empty json')
#     return None

# Check the content type of the response
if 'application/json' in content_type:
    # If the response is JSON, parse it using response.json()
    output = response.json()
elif 'text/html' in content_type:
    # If the response is HTML, get the raw response data using response.text,
    # and parse it as JSON using json.loads()
    try:
        output = json.loads(response.text)
    except json.decoder.JSONDecodeError as e:
        print(f"Invalid JSON: {e}")
        # s =requests.Session()
        # output = s.get("http://nseindia.com",headers=headers)
        # output = s.get(payload,headers=headers).json()
        # return output
        return None
else:
    # Handle the case where the content type is not supported
    print(f"Unsupported content type: {content_type}")
    return None

# Dump JSON to debug what we are getting from nseftech("https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY")
with open('nsepython_json_output_response.json', 'w') as f:
    json.dump(output, f, indent=4)
    
return output

and i want to call this line: oi_data, ltp, crontime = oi_chain_builder(symbol,"latest","full")

only when response.status_code = 200

how can i do this ?

You learn to use ChatGPT


import requests

url = "https://www.example.com/api"
response = requests.get(url)

if response.status_code == 200:
    oi_data, ltp, crontime = oi_chain_builder(symbol, "latest", "full")

If we debug a bit,

sometimes Content-Type: application/json; charset=utf-8

&

sometimes it is: Content-Type: text/html; charset=utf-8

when the Content-Type is application/html, response.status_code: 401, & hence it gives an error

response.status_code: 200, only when Content-Type: application/json

i think it is intentionally done by NSE’s website.

I think at first call we should dump the json file & save it,

& if the users makes call to any funtcion within 5 to 10 seconds let’s say

then we should show them the data from the dumped json file,

after a specific time, let say after 30 seconds, if the user make any call to any function

functions like :

oi_chain_builder(symbol,"latest","full")

then we should be calling the real function, this would be the best way

if NSE is intentionally doing it to limit the requests.

1 Like

but this way, we will be calling the API ‘TWO TIMES’ !!
I think it is already being called here: oi_chain_builder(symbol, “latest”, “full”)

I made hugely complex systems in Leverage - Unofficed. Do whatever you want, its just a wrapper.

The core function here is nsefetch(). Rest everything you can avoid.

:unamused:
import requests

url = “https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY” // 1st Call
response = requests.get(url)

if response.status_code == 200:
# oi_chain_builder // 2nd Call to the API, Indirectly :unamused:
oi_data, ltp, crontime = oi_chain_builder(symbol, “latest”, “full”)

Unable to understand your question…

With this url = “https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY”, we are making the first call to the API
& by calling oi_chain_builder() function after that if block, we are again calling the API for the second time

url = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY" // <------ 1st Call
response = requests.get(url)

if response.status_code == 200:
    # oi_chain_builder // <----- 2nd Call to the API, Indirectly 😒
    oi_data, ltp, crontime = oi_chain_builder(symbol, "latest", "full"

all i am saying is, we should call oi_chain_builder() or any funtion, only iff

the nsefetch(payload) function returns a 200

OI Chain Builder Function in the documentation:

says to do this to get the Option chain:

oi_data, ltp, crontime = oi_chain_builder("RELIANCE","latest","full")
print(oi_data)
print(ltp)
print(crontime)

** I understood what you wrote but I am unable to understand what is your question**

My API Wrapper says to do

nsefetch("https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY")

Why on earth you are doing Python requests when it is evident it will create multiple errors !