Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,30 +9,12 @@ from bhavcopy_html import *
|
|
| 9 |
from nsepython import *
|
| 10 |
from yahooinfo import fetch_info
|
| 11 |
import datetime
|
| 12 |
-
from io import BytesIO
|
| 13 |
from datetime import datetime
|
| 14 |
-
import
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# Backblaze B2 Setup
|
| 18 |
-
# ======================================================
|
| 19 |
-
S3_ENDPOINT = "https://s3.us-east-005.backblazeb2.com"
|
| 20 |
-
BUCKET_NAME = "eshanhf"
|
| 21 |
-
AWS_KEY_ID = "005239ca03b31af0000000001"
|
| 22 |
-
AWS_SECRET_KEY = "K005uGFZkrtYa4Hg1GliFUQohs/BTk4"
|
| 23 |
-
|
| 24 |
-
s3 = boto3.client(
|
| 25 |
-
"s3",
|
| 26 |
-
endpoint_url=S3_ENDPOINT,
|
| 27 |
-
aws_access_key_id=AWS_KEY_ID,
|
| 28 |
-
aws_secret_access_key=AWS_SECRET_KEY,
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
LOG_FILE = "fetch_log.csv"
|
| 32 |
-
|
| 33 |
-
# ======================================================
|
| 34 |
# Scrollable HTML wrapper
|
| 35 |
-
#
|
| 36 |
SCROLL_WRAP = """
|
| 37 |
<div style="
|
| 38 |
max-height: 80vh;
|
|
@@ -46,9 +28,9 @@ SCROLL_WRAP = """
|
|
| 46 |
</div>
|
| 47 |
"""
|
| 48 |
|
| 49 |
-
#
|
| 50 |
# Date helpers
|
| 51 |
-
#
|
| 52 |
def today_str():
|
| 53 |
return datetime.date.today().strftime("%d-%m-%Y")
|
| 54 |
|
|
@@ -60,17 +42,17 @@ def last_year_date(d):
|
|
| 60 |
new_dt = dt.replace(year=dt.year - 1)
|
| 61 |
return new_dt.strftime("%d-%m-%Y")
|
| 62 |
|
| 63 |
-
#
|
| 64 |
# HTML wrapper
|
| 65 |
-
#
|
| 66 |
def wrap(html):
|
| 67 |
if html is None:
|
| 68 |
return "<h3>No Data</h3>"
|
| 69 |
return SCROLL_WRAP.replace("{{HTML}}", html)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
# Request Type Options
|
| 73 |
-
#
|
| 74 |
STOCK_REQ = [
|
| 75 |
"info", "intraday", "daily", "nse_eq", "qresult", "result",
|
| 76 |
"balance", "cashflow", "dividend", "split", "other", "stock_hist"
|
|
@@ -84,9 +66,9 @@ INDEX_REQ = [
|
|
| 84 |
"index_pe_pb_div", "index_total_returns"
|
| 85 |
]
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
# Update UI
|
| 89 |
-
#
|
| 90 |
def update_on_mode(mode):
|
| 91 |
if mode == "stock":
|
| 92 |
return (
|
|
@@ -107,9 +89,9 @@ def update_on_mode(mode):
|
|
| 107 |
gr.update(value="")
|
| 108 |
)
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
-
#
|
| 113 |
def fetch_data(mode, req_type, name, date_str):
|
| 114 |
req_type = req_type.lower()
|
| 115 |
name = name.strip()
|
|
@@ -118,9 +100,7 @@ def fetch_data(mode, req_type, name, date_str):
|
|
| 118 |
date_str = yesterday_str()
|
| 119 |
date_start = last_year_date(date_str)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
# Fetch the requested data
|
| 123 |
-
# ==========================
|
| 124 |
if mode == "index":
|
| 125 |
if req_type == "indices":
|
| 126 |
result = build_indices_html()
|
|
@@ -159,6 +139,7 @@ def fetch_data(mode, req_type, name, date_str):
|
|
| 159 |
else:
|
| 160 |
result = wrap(f"<h3>No handler for {req_type}</h3>")
|
| 161 |
|
|
|
|
| 162 |
elif mode == "stock":
|
| 163 |
if req_type == "daily":
|
| 164 |
result = wrap(fetch_daily(name))
|
|
@@ -190,44 +171,11 @@ def fetch_data(mode, req_type, name, date_str):
|
|
| 190 |
else:
|
| 191 |
result = wrap(f"<h3>No valid mode: {mode}</h3>")
|
| 192 |
|
| 193 |
-
# ==========================
|
| 194 |
-
# Update fetch log at the END
|
| 195 |
-
# ==========================
|
| 196 |
-
try:
|
| 197 |
-
# Read existing log from B2
|
| 198 |
-
try:
|
| 199 |
-
obj = s3.get_object(Bucket=BUCKET_NAME, Key=LOG_FILE)
|
| 200 |
-
data = obj['Body'].read()
|
| 201 |
-
df = pd.read_csv(BytesIO(data))
|
| 202 |
-
except Exception:
|
| 203 |
-
df = pd.DataFrame(columns=["Sr","Datetime","Client","Mode","Req_Type","Name","Date"])
|
| 204 |
-
|
| 205 |
-
# Append new row
|
| 206 |
-
df = df.append({
|
| 207 |
-
"Sr": len(df)+1,
|
| 208 |
-
"Datetime": datetime.now().strftime("%d-%m-%Y %H:%M:%S"),
|
| 209 |
-
"Client": "gradio",
|
| 210 |
-
"Mode": mode,
|
| 211 |
-
"Req_Type": req_type,
|
| 212 |
-
"Name": name,
|
| 213 |
-
"Date": date_str
|
| 214 |
-
}, ignore_index=True)
|
| 215 |
-
|
| 216 |
-
# Save back to B2
|
| 217 |
-
csv_buffer = BytesIO()
|
| 218 |
-
df.to_csv(csv_buffer, index=False)
|
| 219 |
-
csv_buffer.seek(0)
|
| 220 |
-
s3.put_object(Bucket=BUCKET_NAME, Key=LOG_FILE, Body=csv_buffer.getvalue())
|
| 221 |
-
|
| 222 |
-
except Exception as e:
|
| 223 |
-
print(f"Error logging fetch: {e}")
|
| 224 |
-
|
| 225 |
-
# Return the fetched result to Gradio
|
| 226 |
return result
|
| 227 |
|
| 228 |
-
#
|
| 229 |
# UI
|
| 230 |
-
#
|
| 231 |
with gr.Blocks(title="Stock / Index App") as iface:
|
| 232 |
|
| 233 |
gr.Markdown("### **Stock / Index Data Fetcher**")
|
|
@@ -250,8 +198,8 @@ with gr.Blocks(title="Stock / Index App") as iface:
|
|
| 250 |
# Fetch
|
| 251 |
fetch_btn.click(fetch_data, inputs=[mode_input, req_type_input, name_input, date_input], outputs=output)
|
| 252 |
|
| 253 |
-
#
|
| 254 |
# Launch
|
| 255 |
-
#
|
| 256 |
if __name__ == "__main__":
|
| 257 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 9 |
from nsepython import *
|
| 10 |
from yahooinfo import fetch_info
|
| 11 |
import datetime
|
|
|
|
| 12 |
from datetime import datetime
|
| 13 |
+
from backblaze import upload_file, read_file # for optional file writing
|
| 14 |
|
| 15 |
+
# ===========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Scrollable HTML wrapper
|
| 17 |
+
# ===========================
|
| 18 |
SCROLL_WRAP = """
|
| 19 |
<div style="
|
| 20 |
max-height: 80vh;
|
|
|
|
| 28 |
</div>
|
| 29 |
"""
|
| 30 |
|
| 31 |
+
# ===========================
|
| 32 |
# Date helpers
|
| 33 |
+
# ===========================
|
| 34 |
def today_str():
|
| 35 |
return datetime.date.today().strftime("%d-%m-%Y")
|
| 36 |
|
|
|
|
| 42 |
new_dt = dt.replace(year=dt.year - 1)
|
| 43 |
return new_dt.strftime("%d-%m-%Y")
|
| 44 |
|
| 45 |
+
# ===========================
|
| 46 |
# HTML wrapper
|
| 47 |
+
# ===========================
|
| 48 |
def wrap(html):
|
| 49 |
if html is None:
|
| 50 |
return "<h3>No Data</h3>"
|
| 51 |
return SCROLL_WRAP.replace("{{HTML}}", html)
|
| 52 |
|
| 53 |
+
# ===========================
|
| 54 |
# Request Type Options
|
| 55 |
+
# ===========================
|
| 56 |
STOCK_REQ = [
|
| 57 |
"info", "intraday", "daily", "nse_eq", "qresult", "result",
|
| 58 |
"balance", "cashflow", "dividend", "split", "other", "stock_hist"
|
|
|
|
| 66 |
"index_pe_pb_div", "index_total_returns"
|
| 67 |
]
|
| 68 |
|
| 69 |
+
# ===========================
|
| 70 |
+
# Update UI defaults
|
| 71 |
+
# ===========================
|
| 72 |
def update_on_mode(mode):
|
| 73 |
if mode == "stock":
|
| 74 |
return (
|
|
|
|
| 89 |
gr.update(value="")
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# ===========================
|
| 93 |
+
# Fetch data
|
| 94 |
+
# ===========================
|
| 95 |
def fetch_data(mode, req_type, name, date_str):
|
| 96 |
req_type = req_type.lower()
|
| 97 |
name = name.strip()
|
|
|
|
| 100 |
date_str = yesterday_str()
|
| 101 |
date_start = last_year_date(date_str)
|
| 102 |
|
| 103 |
+
# ======= Index Mode =======
|
|
|
|
|
|
|
| 104 |
if mode == "index":
|
| 105 |
if req_type == "indices":
|
| 106 |
result = build_indices_html()
|
|
|
|
| 139 |
else:
|
| 140 |
result = wrap(f"<h3>No handler for {req_type}</h3>")
|
| 141 |
|
| 142 |
+
# ======= Stock Mode =======
|
| 143 |
elif mode == "stock":
|
| 144 |
if req_type == "daily":
|
| 145 |
result = wrap(fetch_daily(name))
|
|
|
|
| 171 |
else:
|
| 172 |
result = wrap(f"<h3>No valid mode: {mode}</h3>")
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
return result
|
| 175 |
|
| 176 |
+
# ===========================
|
| 177 |
# UI
|
| 178 |
+
# ===========================
|
| 179 |
with gr.Blocks(title="Stock / Index App") as iface:
|
| 180 |
|
| 181 |
gr.Markdown("### **Stock / Index Data Fetcher**")
|
|
|
|
| 198 |
# Fetch
|
| 199 |
fetch_btn.click(fetch_data, inputs=[mode_input, req_type_input, name_input, date_input], outputs=output)
|
| 200 |
|
| 201 |
+
# ===========================
|
| 202 |
# Launch
|
| 203 |
+
# ===========================
|
| 204 |
if __name__ == "__main__":
|
| 205 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|