File size: 8,916 Bytes
ac877b5
 
 
a106796
 
 
 
ac877b5
 
 
 
 
 
 
 
 
 
 
 
 
 
a106796
ac877b5
a106796
ac877b5
 
 
 
 
 
 
a106796
 
 
 
ac877b5
 
 
a106796
 
 
ac877b5
 
 
a106796
 
 
ac877b5
 
 
 
 
 
a106796
 
ac877b5
 
 
a106796
 
 
 
 
 
 
 
ac877b5
 
 
 
 
a106796
ac877b5
 
a106796
ac877b5
a106796
 
 
ac877b5
 
 
 
 
a106796
 
ac877b5
 
 
 
 
 
 
 
a106796
ac877b5
 
 
a106796
ac877b5
 
 
 
 
 
 
 
 
 
a106796
 
 
ac877b5
 
 
a106796
ac877b5
 
 
 
a106796
 
ac877b5
a106796
ac877b5
 
 
 
a106796
 
 
 
ac877b5
 
 
 
 
 
a106796
 
ac877b5
a106796
ac877b5
 
 
 
 
 
 
a106796
ac877b5
 
a106796
 
ac877b5
a106796
ac877b5
 
 
 
a106796
 
ac877b5
 
 
 
 
a106796
ac877b5
 
 
 
 
 
 
a106796
 
ac877b5
 
 
 
 
a106796
 
 
 
 
 
ac877b5
a106796
 
 
 
ac877b5
 
a106796
 
ac877b5
 
a106796
 
ac877b5
 
a106796
 
ac877b5
 
a106796
 
ac877b5
 
beb787e
efa78ba
ac877b5
 
 
 
 
 
efa78ba
 
ac877b5
 
beb787e
 
ac877b5
 
beb787e
a106796
ac877b5
 
a106796
 
ac877b5
 
a106796
 
ac877b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a106796
ac877b5
 
 
a106796
 
 
ac877b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# ===============================================================
# Core imports (kept exactly as requested)
# ===============================================================
import os, sys, requests, pandas as pd, json, random, datetime, time, logging, re, urllib.parse
from collections import Counter


# ===============================================================
# Runtime mode
#   - "local": direct requests.Session
#   - "vpn"  : curl + cookie based access
# ===============================================================
mode = "local"


# ===============================================================
# NSE FETCH HANDLER
#   Single unified fetch function
#   Auto-handles cookies, headers, and retries
# ===============================================================

if mode == "vpn":

    def nsefetch(payload):
        """
        NSE fetch using curl + cookies (VPN-safe mode)
        """

        def encode(url):
            return url if "%26" in url or "%20" in url else urllib.parse.quote(url, safe=":/?&=")

        def refresh_cookies():
            os.popen(f'curl -c cookies.txt "https://www.nseindia.com" {curl_headers}').read()
            os.popen(f'curl -b cookies.txt -c cookies.txt "https://www.nseindia.com/option-chain" {curl_headers}').read()

        if not os.path.exists("cookies.txt"):
            refresh_cookies()

        encoded = encode(payload)
        cmd = f'curl -b cookies.txt "{encoded}" {curl_headers}'
        raw = os.popen(cmd).read()

        try:
            return json.loads(raw)
        except:
            refresh_cookies()
            raw = os.popen(cmd).read()
            try:
                return json.loads(raw)
            except:
                return {}

else:

    def nsefetch(payload):
        """
        NSE fetch using requests.Session (local / HF safe)
        """
        try:
            s = requests.Session()
            s.get("https://www.nseindia.com", headers=headers, timeout=10)
            s.get("https://www.nseindia.com/option-chain", headers=headers, timeout=10)
            return s.get(payload, headers=headers, timeout=10).json()
        except:
            return {}


# ===============================================================
# HTTP HEADERS
# ===============================================================

headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
    "accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
    "cache-control": "max-age=0",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}

niftyindices_headers = {
    "Accept": "application/json,text/javascript,*/*;q=0.01",
    "Content-Type": "application/json; charset=UTF-8",
    "Origin": "https://niftyindices.com",
    "Referer": "https://niftyindices.com/reports/historical-data",
    "User-Agent": "Mozilla/5.0"
}

curl_headers = '''
-H "authority: beta.nseindia.com"
-H "cache-control: max-age=0"
-H "user-agent: Mozilla/5.0"
-H "accept: */*"
--compressed
'''


# ===============================================================
# Runtime metadata
# ===============================================================
run_time = datetime.datetime.now()
indices = ["NIFTY", "FINNIFTY", "BANKNIFTY"]


# ===============================================================
# Helper utilities
# ===============================================================

def nsesymbolpurify(s):
    """Encode special NSE symbols"""
    return s.replace("&", "%26")


def flatten_dict(d, parent="", sep="."):
    """Flatten nested dictionaries"""
    items = {}
    for k, v in d.items():
        nk = f"{parent}{sep}{k}" if parent else k
        if isinstance(v, dict):
            items.update(flatten_dict(v, nk, sep))
        else:
            items[nk] = v
    return items


def flatten_nested(d, prefix=""):
    """Flatten dicts + lists (deep NSE JSON support)"""
    flat = {}
    for k, v in d.items():
        nk = f"{prefix}{k}" if prefix == "" else f"{prefix}.{k}"
        if isinstance(v, dict):
            flat.update(flatten_nested(v, nk))
        elif isinstance(v, list):
            if v and isinstance(v[0], dict):
                for i, x in enumerate(v):
                    flat.update(flatten_nested(x, f"{nk}.{i}"))
            else:
                flat[nk] = v
        else:
            flat[nk] = v
    return flat


def rename_col(cols):
    """Resolve duplicate column names after flattening"""
    child = [c.split(".")[-1] for c in cols]
    cnt = Counter(child)
    new = []
    for c, ch in zip(cols, child):
        if cnt[ch] == 1:
            new.append(ch)
        else:
            p = c.split(".")
            new.append(f"{p[-1]}_{p[-2]}" if len(p) >= 2 else p[-1])
    return new


def df_from_data(data):
    """Convert NSE JSON array into clean DataFrame"""
    rows = [flatten_nested(x) if isinstance(x, dict) else {"value": x} for x in data]
    df = pd.DataFrame(rows)
    df.columns = rename_col(df.columns)
    return df


# ===============================================================
# API WRAPPERS (No name changes)
# ===============================================================

def indices():
    p = nsefetch("https://www.nseindia.com/api/allIndices")
    return {
        "data": pd.DataFrame(p.pop("data")),
        "dates": pd.DataFrame([p.pop("dates")]),
        "indices": pd.DataFrame([p])
    }


def eq(symbol):
    symbol = nsesymbolpurify(symbol)
    df = nsefetch(f"https://www.nseindia.com/api/quote-equity?symbol={symbol}")
    pre = df.pop("preOpenMarket")

    return {
        "securityInfo": pd.DataFrame([df["securityInfo"]]),
        "priceInfo": pd.DataFrame([flatten_dict(df["priceInfo"])]),
        "industryInfo": pd.DataFrame([df["industryInfo"]]),
        "pdSectorIndAll": pd.DataFrame([df["metadata"].pop("pdSectorIndAll")]),
        "metadata": pd.DataFrame([df["metadata"]]),
        "info": pd.DataFrame([df["info"]]),
        "preOpen": pd.DataFrame(pre.pop("preopen")),
        "preOpenMarket": pd.DataFrame([pre])
    }


def eq_fno():
    return nsefetch("https://www.nseindia.com/api/equity-stockIndices?index=SECURITIES%20IN%20F%26O")


def eq_der(symbol):
    return nsefetch("https://www.nseindia.com/api/quote-derivative?symbol=" + nsesymbolpurify(symbol))


def index_chain(symbol):
    return nsefetch("https://www.nseindia.com/api/option-chain-indices?symbol=" + nsesymbolpurify(symbol))


def eq_chain(symbol):
    return nsefetch("https://www.nseindia.com/api/option-chain-equities?symbol=" + nsesymbolpurify(symbol))


def nse_holidays(t="trading"):
    return nsefetch("https://www.nseindia.com/api/holiday-master?type=" + t)


def nse_results(index="equities", period="Quarterly"):
    if index in ["equities", "debt", "sme"] and period in ["Quarterly", "Annual", "Half-Yearly", "Others"]:
        return pd.json_normalize(
            nsefetch(f"https://www.nseindia.com/api/corporates-financial-results?index={index}&period={period}")
        )
    print("Invalid Input")


def nse_events():
    return pd.json_normalize(nsefetch("https://www.nseindia.com/api/event-calendar"))


def nse_past_results(symbol):
    return nsefetch("https://www.nseindia.com/api/results-comparision?symbol=" + nsesymbolpurify(symbol))


def nse_blockdeal():
    return nsefetch("https://nseindia.com/api/block-deal")


def nse_marketStatus():
    return nsefetch("https://nseindia.com/api/marketStatus")


def nse_circular(mode="latest"):
    return nsefetch(
        "https://www.nseindia.com/api/latest-circular"
        if mode == "latest"
        else "https://www.nseindia.com/api/circulars"
    )


def nse_fiidii(mode="pandas"):
    return pd.DataFrame(nsefetch("https://www.nseindia.com/api/fiidiiTradeReact"))


def nsetools_get_quote(symbol):
    p = nsefetch("https://www.nseindia.com/api/equity-stockIndices?index=SECURITIES%20IN%20F%26O")
    for x in p["data"]:
        if x["symbol"] == symbol.upper():
            return x


def nse_index():
    p = nsefetch("https://iislliveblob.niftyindices.com/jsonfiles/LiveIndicesWatch.json")
    return pd.DataFrame(p["data"])


# ===============================================================
# Historical / CSV endpoints
# ===============================================================

def nse_bhavcopy(d):
    return pd.read_csv(
        "https://archives.nseindia.com/products/content/sec_bhavdata_full_" + d.replace("-", "") + ".csv"
    )


def nse_highlow(d: str) -> pd.DataFrame:
    date_str = d.replace("-", "")
    url = f"https://archives.nseindia.com/content/CM_52_wk_High_low_{date_str}.csv"
    df = pd.read_csv(url, skiprows=2, engine="python")
    df.columns = df.columns.str.strip()
    return df


def nse_bulkdeals():
    return pd.read_csv("https://archives.nseindia.com/content/equities/bulk.csv")


def nse_blockdeals():
    return pd.read_csv("https://archives.nseindia.com/content/equities/block.csv")