eshan6704 commited on
Commit
1ddfacc
·
verified ·
1 Parent(s): bcf470f

Update nse.py

Browse files
Files changed (1) hide show
  1. nse.py +10 -46
nse.py CHANGED
@@ -136,53 +136,17 @@ def open(index_name="NIFTY 50"):
136
  return full_html
137
 
138
  '''
139
- def open(index_name="NIFTY 50"):
140
- url = f"https://www.nseindia.com/api/equity-stockIndices?index={index_name.replace(' ', '%20')}"
141
- data = fetch_data(url)
142
-
143
- if data is None:
144
- return "<h3>No Data</h3>"
145
-
146
- # Extract base parts
147
- df_market = pd.DataFrame([data["marketStatus"]])
148
- df_adv = pd.DataFrame([data["advance"]])
149
- df_data = pd.DataFrame(data["data"])
150
-
151
- # ------------------------------
152
- # Merge child meta keys into df_data
153
- # ------------------------------
154
- def flatten_meta(row):
155
- meta = row.get("meta", {})
156
- flat = {}
157
- for k, v in meta.items():
158
- if isinstance(v, dict): # flatten one level
159
- for ck, cv in v.items():
160
- flat[f"{k}_{ck}"] = cv
161
- else:
162
- flat[k] = v
163
- return pd.Series(flat)
164
-
165
- # Expand meta into columns
166
- df_meta_expanded = df_data.apply(flatten_meta, axis=1)
167
-
168
- # Merge expanded meta into df_data
169
- df_data = pd.concat([df_data.drop(columns=["meta"], errors="ignore"),
170
- df_meta_expanded], axis=1)
171
-
172
- # ------------------------------
173
- # Convert to HTML
174
- # ------------------------------
175
- html_market = df_market.to_html(index=False, border=1)
176
- html_adv = df_adv.to_html(index=False, border=1)
177
- html_data = df_data.to_html(index=False, border=1)
178
-
179
- full_html = (
180
- "<h3>Market Status</h3>" + html_market +
181
- "<br><h3>Advance/Decline</h3>" + html_adv +
182
- "<br><h3>Index Data (with META merged)</h3>" + html_data
183
- )
184
 
185
- return full_html
 
 
 
 
 
 
 
186
 
187
  # ---------------------------------------------------
188
  # Option Chain DF (Raw CE/PE)
 
136
  return full_html
137
 
138
  '''
139
+ import requests
140
+ import json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
+ def fetch_data_from_nse(url):
143
+ headers = {"User-Agent": "Mozilla/5.0"}
144
+ try:
145
+ resp = requests.get(url, headers=headers)
146
+ if resp.status_code == 200:
147
+ return resp.json()
148
+ except:
149
+ return None
150
 
151
  # ---------------------------------------------------
152
  # Option Chain DF (Raw CE/PE)