Spaces:
Running
Running
Update nse.py
Browse files
nse.py
CHANGED
|
@@ -119,52 +119,42 @@ def nse_high_low():
|
|
| 119 |
# ======================================================================
|
| 120 |
# BHAVCOPY
|
| 121 |
# ======================================================================
|
|
|
|
| 122 |
def nse_bhav(date_str):
|
| 123 |
"""
|
| 124 |
-
|
| 125 |
-
- DDMMYYYY
|
| 126 |
-
- DD-MM-YYYY
|
| 127 |
-
- DD/MM/YYYY
|
| 128 |
-
Converts automatically to DDMMYYYY for NSE API.
|
| 129 |
"""
|
| 130 |
-
|
| 131 |
-
# ---------------------------
|
| 132 |
# Normalize date
|
| 133 |
-
# ---------------------------
|
| 134 |
try:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
except Exception:
|
| 146 |
-
return "<p>Error: Unable to parse date.</p>"
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# ---------------------------
|
| 151 |
-
url = (
|
| 152 |
-
"https://www.nseindia.com/api/reports"
|
| 153 |
-
f"?archives=true&date={api_date}&type=equities&mode=single"
|
| 154 |
-
)
|
| 155 |
|
| 156 |
try:
|
| 157 |
-
r =
|
|
|
|
| 158 |
data = r.json()
|
| 159 |
-
|
| 160 |
df = pd.DataFrame(data.get("data", []))
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
return f"<p>Error fetching bhavcopy: {e}</p>"
|
| 166 |
|
| 167 |
-
|
| 168 |
# ======================================================================
|
| 169 |
# ALL NSE INDICES LIST
|
| 170 |
# ======================================================================
|
|
|
|
| 119 |
# ======================================================================
|
| 120 |
# BHAVCOPY
|
| 121 |
# ======================================================================
|
| 122 |
+
|
| 123 |
def nse_bhav(date_str):
|
| 124 |
"""
|
| 125 |
+
Fetch NSE Bhavcopy for given date.
|
| 126 |
+
date_str: "DD-MM-YYYY" or "DD/MM/YYYY" or "DDMMYYYY"
|
|
|
|
|
|
|
|
|
|
| 127 |
"""
|
|
|
|
|
|
|
| 128 |
# Normalize date
|
|
|
|
| 129 |
try:
|
| 130 |
+
if "-" in date_str:
|
| 131 |
+
dt = datetime.strptime(date_str, "%d-%m-%Y")
|
| 132 |
+
elif "/" in date_str:
|
| 133 |
+
dt = datetime.strptime(date_str, "%d/%m/%Y")
|
| 134 |
+
elif len(date_str) == 8:
|
| 135 |
+
dt = datetime.strptime(date_str, "%d%m%Y")
|
| 136 |
+
else:
|
| 137 |
+
return f"<p>Invalid date format: {date_str}</p>"
|
| 138 |
+
except Exception as e:
|
| 139 |
+
return f"<p>Error parsing date: {e}</p>"
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
date_api = dt.strftime("%d-%m-%Y")
|
| 142 |
+
url = f"https://www.nseindia.com/api/reports?archives=true&date={date_api}&type=equities&mode=single"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
try:
|
| 145 |
+
r = session.get(url, headers=NSE_HEADERS, timeout=5)
|
| 146 |
+
r.raise_for_status()
|
| 147 |
data = r.json()
|
|
|
|
| 148 |
df = pd.DataFrame(data.get("data", []))
|
| 149 |
|
| 150 |
+
if df.empty:
|
| 151 |
+
return f"<h3>Bhavcopy {date_api}</h3><p>No data available</p>"
|
| 152 |
+
|
| 153 |
+
return _to_html(f"Bhavcopy {date_api}", df)
|
| 154 |
|
| 155 |
except Exception as e:
|
| 156 |
return f"<p>Error fetching bhavcopy: {e}</p>"
|
| 157 |
|
|
|
|
| 158 |
# ======================================================================
|
| 159 |
# ALL NSE INDICES LIST
|
| 160 |
# ======================================================================
|