eshan6704 commited on
Commit
2b9b305
·
verified ·
1 Parent(s): 2037660

Delete nsepython2.py

Browse files
Files changed (1) hide show
  1. nsepython2.py +0 -374
nsepython2.py DELETED
@@ -1,374 +0,0 @@
1
- # ==============================
2
- # Standard library imports
3
- # ==============================
4
- import os
5
- import sys
6
- import json
7
- import random
8
- import datetime
9
- import time
10
- import logging
11
- import re
12
- import urllib.parse
13
- from collections import Counter
14
-
15
- # ==============================
16
- # Third-party imports
17
- # ==============================
18
- import requests
19
- import pandas as pd
20
-
21
- mode = 'local'
22
-
23
- # ------------------------- NSE FETCH -------------------------
24
- if mode == "vpn":
25
- def nsefetch(payload):
26
- def encode(url): return url if "%26" in url or "%20" in url else urllib.parse.quote(url, safe=":/?&=")
27
- def refresh_cookies():
28
- os.popen(f'curl -c cookies.txt "https://www.nseindia.com" {curl_headers}').read()
29
- os.popen(f'curl -b cookies.txt -c cookies.txt "https://www.nseindia.com/option-chain" {curl_headers}').read()
30
-
31
- if not os.path.exists("cookies.txt"): refresh_cookies()
32
- encoded = encode(payload)
33
- cmd = f'curl -b cookies.txt "{encoded}" {curl_headers}'
34
- raw = os.popen(cmd).read()
35
- try: return json.loads(raw)
36
- except:
37
- refresh_cookies()
38
- raw = os.popen(cmd).read()
39
- try: return json.loads(raw)
40
- except: return {}
41
-
42
- if mode == 'local':
43
- def nsefetch(payload):
44
- try:
45
- s = requests.Session()
46
- s.get("https://www.nseindia.com", headers=headers, timeout=10)
47
- s.get("https://www.nseindia.com/option-chain", headers=headers, timeout=10)
48
- return s.get(payload, headers=headers, timeout=10).json()
49
- except:
50
- return {}
51
-
52
- # ------------------------- HEADERS -------------------------
53
- headers = {
54
- "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
55
- "accept-language": "en-US,en;q=0.9,en-IN;q=0.8,en-GB;q=0.7",
56
- "cache-control": "max-age=0",
57
- "priority": "u=0, i",
58
- "sec-ch-ua": '"Microsoft Edge";v="129","Not=A?Brand";v="8","Chromium";v="129"',
59
- "sec-ch-ua-mobile": "?0",
60
- "sec-ch-ua-platform": '"Windows"',
61
- "sec-fetch-dest": "document",
62
- "sec-fetch-mode": "navigate",
63
- "sec-fetch-site": "none",
64
- "sec-fetch-user": "?1",
65
- "upgrade-insecure-requests": "1",
66
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0"
67
- }
68
-
69
- niftyindices_headers = {
70
- 'Connection': 'keep-alive',
71
- 'sec-ch-ua': '"Not;A Brand";v="99","Google Chrome";v="91","Chromium";v="91"',
72
- 'Accept': 'application/json,text/javascript,*/*;q=0.01',
73
- 'DNT': '1',
74
- 'X-Requested-With': 'XMLHttpRequest',
75
- 'sec-ch-ua-mobile': '?0',
76
- 'User-Agent': 'Mozilla/5.0',
77
- 'Content-Type': 'application/json; charset=UTF-8',
78
- 'Origin': 'https://niftyindices.com',
79
- 'Sec-Fetch-Site': 'same-origin',
80
- 'Sec-Fetch-Mode': 'cors',
81
- 'Sec-Fetch-Dest': 'empty',
82
- 'Referer': 'https://niftyindices.com/reports/historical-data',
83
- 'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8'
84
- }
85
-
86
- 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" -H "sec-fetch-user: ?1" -H "accept: */*" -H "sec-fetch-site: none" -H "accept-language: en-US,en;q=0.9" --compressed'''
87
-
88
- run_time = datetime.datetime.now()
89
- indices = ['NIFTY','FINNIFTY','BANKNIFTY']
90
-
91
- # ------------------------- HELPERS -------------------------
92
- def nsesymbolpurify(s): return s.replace('&','%26')
93
-
94
- def flatten_dict(d, parent="", sep="."):
95
- items={}
96
- for k,v in d.items():
97
- nk = f"{parent}{sep}{k}" if parent else k
98
- if isinstance(v, dict): items.update(flatten_dict(v, nk, sep))
99
- else: items[nk] = v
100
- return items
101
-
102
- def flatten_nested(d, prefix=""):
103
- flat={}
104
- for k,v in d.items():
105
- nk = f"{prefix}{k}" if prefix=="" else f"{prefix}.{k}"
106
- if isinstance(v, dict):
107
- flat.update(flatten_nested(v, nk))
108
- elif isinstance(v, list):
109
- if v and isinstance(v[0], dict):
110
- for i,x in enumerate(v): flat.update(flatten_nested(x, f"{nk}.{i}"))
111
- else: flat[nk]=v
112
- else: flat[nk]=v
113
- return flat
114
-
115
- def rename_col(cols):
116
- child=[c.split('.')[-1] for c in cols]
117
- cnt=Counter(child)
118
- new=[]
119
- for c,ch in zip(cols,child):
120
- if cnt[ch]==1: new.append(ch)
121
- else:
122
- p=c.split('.')
123
- new.append(f"{p[-1]}_{p[-2]}" if len(p)>=2 else p[-1])
124
- return new
125
-
126
- def df_from_data(data):
127
- rows=[ flatten_nested(x) if isinstance(x,dict) else {"value":x} for x in data ]
128
- df=pd.DataFrame(rows)
129
- df.columns=rename_col(df.columns)
130
- return df
131
-
132
- # ------------------------- API FUNCTIONS -------------------------
133
- def indices():
134
- p=nsefetch("https://www.nseindia.com/api/allIndices")
135
- return {"data":pd.DataFrame(p.pop("data")), "dates":pd.DataFrame([p.pop("dates")]), "indices":pd.DataFrame([p])}
136
-
137
- def eq(symbol):
138
- symbol=nsesymbolpurify(symbol)
139
- df=nsefetch(f'https://www.nseindia.com/api/quote-equity?symbol={symbol}')
140
- pre=df.pop('preOpenMarket')
141
- out={
142
- "securityInfo": pd.DataFrame([df["securityInfo"]]),
143
- "priceInfo": pd.DataFrame([flatten_dict(df["priceInfo"])]),
144
- "industryInfo": pd.DataFrame([df["industryInfo"]]),
145
- "pdSectorIndAll": pd.DataFrame([df["metadata"].pop("pdSectorIndAll")]),
146
- "metadata": pd.DataFrame([df["metadata"]]),
147
- "info": pd.DataFrame([df["info"]]),
148
- "preOpen": pd.DataFrame(pre.pop('preopen')),
149
- "preOpenMarket": pd.DataFrame([pre])
150
- }
151
- return out
152
-
153
- def eq_fno(): return nsefetch('https://www.nseindia.com/api/equity-stockIndices?index=SECURITIES%20IN%20F%26O')
154
- def eq_der(symbol): return nsefetch('https://www.nseindia.com/api/quote-derivative?symbol='+nsesymbolpurify(symbol))
155
- def index_chain(symbol): return nsefetch('https://www.nseindia.com/api/option-chain-indices?symbol='+nsesymbolpurify(symbol))
156
- def eq_chain(symbol): return nsefetch('https://www.nseindia.com/api/option-chain-equities?symbol='+nsesymbolpurify(symbol))
157
- def nse_holidays(t="trading"): return nsefetch('https://www.nseindia.com/api/holiday-master?type='+t)
158
-
159
- def nse_results(index="equities",period="Quarterly"):
160
- if index in ["equities","debt","sme"] and period in ["Quarterly","Annual","Half-Yearly","Others"]:
161
- return pd.json_normalize(nsefetch(f'https://www.nseindia.com/api/corporates-financial-results?index={index}&period={period}'))
162
- print("Invalid Input")
163
-
164
- def nse_events(): return pd.json_normalize(nsefetch('https://www.nseindia.com/api/event-calendar'))
165
- def nse_past_results(symbol): return nsefetch('https://www.nseindia.com/api/results-comparision?symbol='+nsesymbolpurify(symbol))
166
- def nse_blockdeal(): return nsefetch('https://nseindia.com/api/block-deal')
167
- def nse_marketStatus(): return nsefetch('https://nseindia.com/api/marketStatus')
168
- def nse_circular(mode="latest"):
169
- return nsefetch('https://www.nseindia.com/api/latest-circular' if mode=="latest" else 'https://www.nseindia.com/api/circulars')
170
-
171
- def nse_fiidii(mode="pandas"):
172
-
173
- p=nsefetch('https://www.nseindia.com/api/fiidiiTradeReact')
174
- return pd.DataFrame(p)
175
-
176
- def nsetools_get_quote(symbol):
177
- p=nsefetch('https://www.nseindia.com/api/equity-stockIndices?index=SECURITIES%20IN%20F%26O')
178
- for x in p['data']:
179
- if x['symbol']==symbol.upper(): return x
180
-
181
- def nse_index():
182
- p=nsefetch('https://iislliveblob.niftyindices.com/jsonfiles/LiveIndicesWatch.json')
183
- return pd.DataFrame(p['data'])
184
- import requests
185
- import json
186
- import pandas as pd
187
-
188
- # ==============================
189
- # Headers (DO NOT MODIFY)
190
- # ==============================
191
- niftyindices_headers = {
192
- 'Connection': 'keep-alive',
193
- 'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
194
- 'Accept': 'application/json, text/javascript, */*; q=0.01',
195
- 'DNT': '1',
196
- 'X-Requested-With': 'XMLHttpRequest',
197
- 'sec-ch-ua-mobile': '?0',
198
- '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',
199
- 'Content-Type': 'application/json; charset=UTF-8',
200
- 'Origin': 'https://niftyindices.com',
201
- 'Sec-Fetch-Site': 'same-origin',
202
- 'Sec-Fetch-Mode': 'cors',
203
- 'Sec-Fetch-Dest': 'empty',
204
- 'Referer': 'https://niftyindices.com/reports/historical-data',
205
- 'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8',
206
- }
207
-
208
- # ==============================
209
- # Internal helper (SAFE)
210
- # ==============================
211
- def _fmt_date(d):
212
- """
213
- Accepts: dd-mm-yyyy or ddmmyyyy
214
- Returns: ddmmyyyy
215
- """
216
- return d.replace("-", "")
217
-
218
- # ==============================
219
- # Index APIs
220
- # ==============================
221
- def index_history(symbol, start_date, end_date):
222
- start_date = _fmt_date(start_date)
223
- end_date = _fmt_date(end_date)
224
-
225
- data = {
226
- 'cinfo': "{'name':'" + symbol +
227
- "','startDate':'" + start_date +
228
- "','endDate':'" + end_date +
229
- "','indexName':'" + symbol + "'}"
230
- }
231
-
232
- payload = requests.post(
233
- 'https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString',
234
- headers=niftyindices_headers,
235
- json=data
236
- ).json()
237
-
238
- payload = json.loads(payload["d"])
239
- return pd.DataFrame.from_records(payload)
240
-
241
-
242
- def index_pe_pb_div(symbol, start_date, end_date):
243
- start_date = _fmt_date(start_date)
244
- end_date = _fmt_date(end_date)
245
-
246
- data = {
247
- 'cinfo': "{'name':'" + symbol +
248
- "','startDate':'" + start_date +
249
- "','endDate':'" + end_date +
250
- "','indexName':'" + symbol + "'}"
251
- }
252
-
253
- payload = requests.post(
254
- 'https://niftyindices.com/Backpage.aspx/getpepbHistoricaldataDBtoString',
255
- headers=niftyindices_headers,
256
- json=data
257
- ).json()
258
-
259
- payload = json.loads(payload["d"])
260
- return pd.DataFrame.from_records(payload)
261
-
262
-
263
- def index_total_returns(symbol, start_date, end_date):
264
- start_date = _fmt_date(start_date)
265
- end_date = _fmt_date(end_date)
266
-
267
- data = {
268
- 'cinfo': "{'name':'" + symbol +
269
- "','startDate':'" + start_date +
270
- "','endDate':'" + end_date +
271
- "','indexName':'" + symbol + "'}"
272
- }
273
-
274
- payload = requests.post(
275
- 'https://niftyindices.com/Backpage.aspx/getTotalReturnIndexString',
276
- headers=niftyindices_headers,
277
- json=data
278
- ).json()
279
-
280
- payload = json.loads(payload["d"])
281
- return pd.DataFrame.from_records(payload)
282
-
283
- def nse_bhavcopy(d): return pd.read_csv("https://archives.nseindia.com/products/content/sec_bhavdata_full_"+d.replace("-","")+".csv")
284
- def nse_bulkdeals(): return pd.read_csv("https://archives.nseindia.com/content/equities/bulk.csv")
285
- def nse_blockdeals(): return pd.read_csv("https://archives.nseindia.com/content/equities/block.csv")
286
-
287
- def nse_preopen(key="NIFTY"):
288
- p=nsefetch("https://www.nseindia.com/api/market-data-pre-open?key="+key)
289
- return {"data":df_from_data(p.pop("data")), "rem":df_from_data([p])}
290
-
291
- def nse_most_active(t="securities",s="value"):
292
- return pd.DataFrame(nsefetch(f"https://www.nseindia.com/api/live-analysis-most-active-{t}?index={s}")["data"])
293
-
294
- def nse_eq_symbols():
295
- return pd.read_csv('https://archives.nseindia.com/content/equities/EQUITY_L.csv')['SYMBOL'].tolist()
296
-
297
- def nse_price_band_hitters(b="both",v="AllSec"):
298
- p=nsefetch("https://www.nseindia.com/api/live-analysis-price-band-hitter")
299
- return {"data":pd.DataFrame(p[b][v]["data"]), "count":pd.DataFrame([p['count']])}
300
-
301
- def nse_largedeals(mode="bulk_deals"):
302
- p=nsefetch('https://www.nseindia.com/api/snapshot-capital-market-largedeal')
303
- return pd.DataFrame(p["BULK_DEALS_DATA" if mode=="bulk_deals" else "SHORT_DEALS_DATA" if mode=="short_deals" else "BLOCK_DEALS_DATA"])
304
-
305
- def nse_largedeals_historical(f,t,mode="bulk_deals"):
306
- m = "bulk-deals" if mode=="bulk_deals" else "short-selling" if mode=="short_deals" else "block-deals"
307
- p=nsefetch(f'https://www.nseindia.com/api/historical/{m}?from={f}&to={t}')
308
- return pd.DataFrame(p["data"])
309
-
310
- def nse_stock_hist(f,t,symbol,series="ALL"):
311
- url=f"https://www.nseindia.com/api/historical/securityArchives?from={f}&to={t}&symbol={symbol.upper()}&dataType=priceVolumeDeliverable&series={series}"
312
- return pd.DataFrame(nsefetch(url)['data'])
313
-
314
- def nse_index_live(name="NIFTY 50"):
315
- p=nsefetch(f"https://www.nseindia.com/api/equity-stockIndices?index={name.replace(' ','%20')}")
316
- return {"data":df_from_data(p.pop("data")) if "data" in p else pd.DataFrame(), "rem":df_from_data([p])}
317
-
318
- import zipfile
319
- from io import BytesIO, StringIO
320
- import pandas as pd
321
- import datetime
322
- import requests
323
-
324
- # ------------------------- RAW CSV FETCH -------------------------
325
- def nse_csv_fetch(url):
326
- """
327
- Fetch RAW CSV text from NSE using existing headers/session logic
328
- """
329
- try:
330
- s = requests.Session()
331
- s.get("https://www.nseindia.com", headers=headers, timeout=10)
332
- s.get("https://www.nseindia.com/option-chain", headers=headers, timeout=10)
333
- r = s.get(url, headers=headers, timeout=10)
334
- r.raise_for_status()
335
- return r.text
336
- except Exception:
337
- return ""
338
-
339
- # ------------------------- INDEX HIGH-LOW CSV -------------------------
340
- def nse_highlow(date_str):
341
- date_str = date_str.replace("-", "")
342
- url="https://archives.nseindia.com/content/indices/" f"ind_close_all_{date_str}.csv"
343
-
344
- return pd.read_csv(url, header=0)
345
-
346
- # ------------------------- STOCK 52-WEEK HIGH-LOW CSV -------------------------
347
- def stock_highlow(date_str):
348
- date_str = date_str.replace("-", "")
349
- url="https://archives.nseindia.com/content/"f"CM_52_wk_High_low_{date_str}.csv"
350
-
351
- return pd.read_csv(url, header=2)
352
- # ------------------------- ZIP CSV FETCH -------------------------
353
- def nse_zip_csv_fetch(url):
354
- """
355
- Fetch ZIP file from NSE, return list of DataFrames (one per CSV inside)
356
- """
357
- try:
358
- s = requests.Session()
359
- s.get("https://www.nseindia.com", headers=headers, timeout=10)
360
- s.get("https://www.nseindia.com/option-chain", headers=headers, timeout=10)
361
-
362
- r = s.get(url, headers=headers, timeout=10)
363
- r.raise_for_status()
364
-
365
- z = zipfile.ZipFile(BytesIO(r.content))
366
- dfs = []
367
- for name in z.namelist():
368
- if name.lower().endswith(".csv"):
369
- with z.open(name) as f:
370
- df = pd.read_csv(f)
371
- dfs.append(df)
372
- return dfs
373
- except Exception:
374
- return []