Skip to content

Update json_handler.py #5

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 16 additions & 19 deletions json_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,27 @@
filename = "option_chain.json"


def get_OC_json(index_name):

headers = {"User-Agent": "Mozilla/5.0"}

# when USD INR pair is added the word indices can be loaded as a conditional
# and be change to currency
url = f"https://www.nseindia.com/api/option-chain-indices?symbol={index_name}"

r = requests.get(url, headers=headers)

content = r.json()
def get_OC_json(index_name='NIFTY'):
url=''
if index_name.upper() in ["NIFTY", "BANKNIFTY", "FINNIFTY", "MIDCPNITFY"]:
url = f"https://www.nseindia.com/api/option-chain-indices?symbol={index_name}"
elif index_name.upper()=='USDINR':
url = f"https://www.nseindia.com/api/option-chain-currency?symbol=USDINR"
else:
url = f"https://www.nseindia.com/api/option-chain-equities?symbol={index_name}"

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','X-Requested-With': 'XMLHttpRequest','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',}
base_url='https://www.nseindia.com/option-chain'
sess=requests.Session()
request=sess.get(base_url, headers=headers, timeout=25)
cookies=dict(request.cookies)
data=sess.get(url, headers=headers, cookies=cookies, timeout=35)
content = data.json()

with open(filename, "w") as json_file:

json.dump(content, json_file, indent=4)

# The below sleep command will interfere with tkinter mainloop
# and will reflect as a button freeze
# alternative is to use tkinter action function with 5000 ms delay

# time.sleep(5)
# print(type(content))

return


def filter_OC_json_data():
Expand Down