eshan6704 commited on
Commit
8fd4df0
·
verified ·
1 Parent(s): 2a6e680

Update nsepython.py

Browse files
Files changed (1) hide show
  1. nsepython.py +82 -1
nsepython.py CHANGED
@@ -213,4 +213,85 @@ def nse_stock_hist(f,t,symbol,series="ALL"):
213
 
214
  def nse_index_live(name="NIFTY 50"):
215
  p=nsefetch(f"https://www.nseindia.com/api/equity-stockIndices?index={name.replace(' ','%20')}")
216
- return {"data":df_from_data(p.pop("data")) if "data" in p else pd.DataFrame(), "rem":df_from_data([p])}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  def nse_index_live(name="NIFTY 50"):
215
  p=nsefetch(f"https://www.nseindia.com/api/equity-stockIndices?index={name.replace(' ','%20')}")
216
+ return {"data":df_from_data(p.pop("data")) if "data" in p else pd.DataFrame(), "rem":df_from_data([p])}
217
+
218
+ import zipfile
219
+ from io import BytesIO, StringIO
220
+ import pandas as pd
221
+ import datetime
222
+ import requests
223
+
224
+ # ------------------------- RAW CSV FETCH -------------------------
225
+ def nse_csv_fetch(url):
226
+ """
227
+ Fetch RAW CSV text from NSE using existing headers/session logic
228
+ """
229
+ try:
230
+ s = requests.Session()
231
+ s.get("https://www.nseindia.com", headers=headers, timeout=10)
232
+ s.get("https://www.nseindia.com/option-chain", headers=headers, timeout=10)
233
+ r = s.get(url, headers=headers, timeout=10)
234
+ r.raise_for_status()
235
+ return r.text
236
+ except Exception:
237
+ return ""
238
+
239
+ # ------------------------- INDEX HIGH-LOW CSV -------------------------
240
+ def nse_highlow(date_str=None):
241
+ """
242
+ NSE HIGH-LOW (INDEX style CSV)
243
+ Returns RAW CSV text
244
+ """
245
+ if not date_str:
246
+ date_str = datetime.datetime.now().strftime("%d%m%Y")
247
+ else:
248
+ date_str = date_str.replace("-", "")
249
+
250
+ url = (
251
+ "https://archives.nseindia.com/content/indices/"
252
+ f"ind_close_all_{date_str}.csv"
253
+ )
254
+
255
+ return nse_csv_fetch(url)
256
+
257
+ # ------------------------- STOCK 52-WEEK HIGH-LOW CSV -------------------------
258
+ def stock_highlow(date_str=None):
259
+ """
260
+ NSE STOCK 52-week High-Low CSV
261
+ Returns RAW CSV text
262
+ """
263
+ if not date_str:
264
+ date_str = datetime.datetime.now().strftime("%d%m%Y")
265
+ else:
266
+ date_str = date_str.replace("-", "")
267
+
268
+ url = (
269
+ "https://nsearchives.nseindia.com/content/"
270
+ f"CM_52_wk_High_low_{date_str}.csv"
271
+ )
272
+
273
+ return nse_csv_fetch(url)
274
+
275
+ # ------------------------- ZIP CSV FETCH -------------------------
276
+ def nse_zip_csv_fetch(url):
277
+ """
278
+ Fetch ZIP file from NSE, return list of DataFrames (one per CSV inside)
279
+ """
280
+ try:
281
+ s = requests.Session()
282
+ s.get("https://www.nseindia.com", headers=headers, timeout=10)
283
+ s.get("https://www.nseindia.com/option-chain", headers=headers, timeout=10)
284
+
285
+ r = s.get(url, headers=headers, timeout=10)
286
+ r.raise_for_status()
287
+
288
+ z = zipfile.ZipFile(BytesIO(r.content))
289
+ dfs = []
290
+ for name in z.namelist():
291
+ if name.lower().endswith(".csv"):
292
+ with z.open(name) as f:
293
+ df = pd.read_csv(f)
294
+ dfs.append(df)
295
+ return dfs
296
+ except Exception:
297
+ return []