Spaces:
Running
Running
Update app/build_nse_fno.py
Browse files- app/build_nse_fno.py +14 -27
app/build_nse_fno.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 1 |
-
#
|
| 2 |
import os
|
| 3 |
-
import subprocess
|
| 4 |
import zipfile
|
| 5 |
-
import
|
| 6 |
import pandas as pd
|
|
|
|
| 7 |
from datetime import datetime as dt
|
| 8 |
|
| 9 |
NSE_FO_BASE = "https://archives.nseindia.com/content/fo"
|
| 10 |
|
| 11 |
|
| 12 |
# ============================================================
|
| 13 |
-
# FETCH FO BHAVCOPY (RAW DF)
|
| 14 |
# ============================================================
|
| 15 |
def fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
|
| 16 |
"""
|
|
@@ -23,28 +23,17 @@ def fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
|
|
| 23 |
zip_name = f"{file_name}.zip"
|
| 24 |
url = f"{NSE_FO_BASE}/{zip_name}"
|
| 25 |
|
| 26 |
-
|
| 27 |
-
zip_path = os.path.join(tmp, zip_name)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"--tlsv1.2",
|
| 33 |
-
"--compressed",
|
| 34 |
-
"-o", zip_path,
|
| 35 |
-
url
|
| 36 |
-
]
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
with zipfile.ZipFile(zip_path) as z:
|
| 44 |
-
if file_name not in z.namelist():
|
| 45 |
-
raise RuntimeError("FO bhavcopy csv missing inside zip")
|
| 46 |
-
with z.open(file_name) as f:
|
| 47 |
-
return pd.read_csv(f)
|
| 48 |
|
| 49 |
|
| 50 |
# ============================================================
|
|
@@ -94,7 +83,6 @@ def nse_fno_html(fo_date: str, symbol: str) -> str:
|
|
| 94 |
"""
|
| 95 |
Returns LIVE HTML for NSE F&O for the given date and symbol.
|
| 96 |
"""
|
| 97 |
-
|
| 98 |
fo_df = fetch_fo_bhavcopy(fo_date)
|
| 99 |
if fo_df.empty:
|
| 100 |
return "<h3>FO Bhavcopy empty</h3>"
|
|
@@ -146,5 +134,4 @@ h2,h3,h4 {{ margin:6px 0; }}
|
|
| 146 |
</body>
|
| 147 |
</html>
|
| 148 |
"""
|
| 149 |
-
|
| 150 |
-
return html
|
|
|
|
| 1 |
+
# build_nse_fno_hf.py
|
| 2 |
import os
|
|
|
|
| 3 |
import zipfile
|
| 4 |
+
import io
|
| 5 |
import pandas as pd
|
| 6 |
+
import requests
|
| 7 |
from datetime import datetime as dt
|
| 8 |
|
| 9 |
NSE_FO_BASE = "https://archives.nseindia.com/content/fo"
|
| 10 |
|
| 11 |
|
| 12 |
# ============================================================
|
| 13 |
+
# FETCH FO BHAVCOPY (RAW DF) - HF-friendly
|
| 14 |
# ============================================================
|
| 15 |
def fetch_fo_bhavcopy(fo_date: str) -> pd.DataFrame:
|
| 16 |
"""
|
|
|
|
| 23 |
zip_name = f"{file_name}.zip"
|
| 24 |
url = f"{NSE_FO_BASE}/{zip_name}"
|
| 25 |
|
| 26 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
|
|
|
| 27 |
|
| 28 |
+
r = requests.get(url, headers=headers, timeout=10)
|
| 29 |
+
if r.status_code != 200:
|
| 30 |
+
raise RuntimeError(f"FO bhavcopy download failed ({r.status_code})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
with zipfile.ZipFile(io.BytesIO(r.content)) as z:
|
| 33 |
+
if file_name not in z.namelist():
|
| 34 |
+
raise RuntimeError("FO bhavcopy CSV missing inside zip")
|
| 35 |
+
with z.open(file_name) as f:
|
| 36 |
+
return pd.read_csv(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
# ============================================================
|
|
|
|
| 83 |
"""
|
| 84 |
Returns LIVE HTML for NSE F&O for the given date and symbol.
|
| 85 |
"""
|
|
|
|
| 86 |
fo_df = fetch_fo_bhavcopy(fo_date)
|
| 87 |
if fo_df.empty:
|
| 88 |
return "<h3>FO Bhavcopy empty</h3>"
|
|
|
|
| 134 |
</body>
|
| 135 |
</html>
|
| 136 |
"""
|
| 137 |
+
return html
|
|
|