Spaces:
Sleeping
Sleeping
Update app/daily.py
Browse files- app/daily.py +58 -20
app/daily.py
CHANGED
|
@@ -6,6 +6,7 @@ import matplotlib.pyplot as plt
|
|
| 6 |
from datetime import datetime as dt
|
| 7 |
import traceback
|
| 8 |
import io
|
|
|
|
| 9 |
|
| 10 |
from . import persist
|
| 11 |
from . import backblaze as b2
|
|
@@ -17,6 +18,11 @@ from . import backblaze as b2
|
|
| 17 |
IMAGE_FORMAT = "png"
|
| 18 |
IMAGE_EXT = "png"
|
| 19 |
DPI = 150
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# ============================================================
|
|
@@ -41,7 +47,7 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 41 |
end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 42 |
|
| 43 |
# ----------------------------------------------------
|
| 44 |
-
# Fetch
|
| 45 |
# ----------------------------------------------------
|
| 46 |
df = yf.download(symbol + ".NS", start=start, end=end)
|
| 47 |
|
|
@@ -86,18 +92,18 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 86 |
plt.grid(True)
|
| 87 |
plt.tight_layout()
|
| 88 |
|
| 89 |
-
plt.savefig(
|
| 90 |
-
buf,
|
| 91 |
-
format=IMAGE_FORMAT,
|
| 92 |
-
dpi=DPI,
|
| 93 |
-
bbox_inches="tight"
|
| 94 |
-
)
|
| 95 |
plt.close()
|
| 96 |
|
| 97 |
buf.seek(0)
|
| 98 |
price_key = f"daily/{symbol}_price_volume.{IMAGE_EXT}"
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# ====================================================
|
| 103 |
# MOVING AVERAGE CHART
|
|
@@ -114,18 +120,25 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 114 |
plt.grid(True)
|
| 115 |
plt.tight_layout()
|
| 116 |
|
| 117 |
-
plt.savefig(
|
| 118 |
-
buf,
|
| 119 |
-
format=IMAGE_FORMAT,
|
| 120 |
-
dpi=DPI,
|
| 121 |
-
bbox_inches="tight"
|
| 122 |
-
)
|
| 123 |
plt.close()
|
| 124 |
|
| 125 |
buf.seek(0)
|
| 126 |
ma_key = f"daily/{symbol}_ma.{IMAGE_EXT}"
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# ====================================================
|
| 131 |
# TABLE (Last 100 days)
|
|
@@ -144,7 +157,7 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 144 |
"""
|
| 145 |
|
| 146 |
# ====================================================
|
| 147 |
-
# FINAL HTML
|
| 148 |
# ====================================================
|
| 149 |
html = f"""
|
| 150 |
<div id="daily_dashboard">
|
|
@@ -165,10 +178,35 @@ def fetch_daily(symbol, date_end, date_start):
|
|
| 165 |
</table>
|
| 166 |
|
| 167 |
<h3>Price & Volume</h3>
|
| 168 |
-
<img src="{price_url}" style="width:100%;max-width:1200px;">
|
| 169 |
|
| 170 |
<h3>Moving Averages</h3>
|
| 171 |
-
<img src="{ma_url}" style="width:100%;max-width:1200px;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
</div>
|
| 174 |
"""
|
|
|
|
| 6 |
from datetime import datetime as dt
|
| 7 |
import traceback
|
| 8 |
import io
|
| 9 |
+
import time
|
| 10 |
|
| 11 |
from . import persist
|
| 12 |
from . import backblaze as b2
|
|
|
|
| 18 |
IMAGE_FORMAT = "png"
|
| 19 |
IMAGE_EXT = "png"
|
| 20 |
DPI = 150
|
| 21 |
+
IMAGE_QUALITY = 85 # compression quality (PNG is lossless)
|
| 22 |
+
|
| 23 |
+
# Public download base (DISPLAY ONLY, no change to backblaze.py)
|
| 24 |
+
B2_PUBLIC_BASE = "https://f005.backblazeb2.com/file/eshanhf"
|
| 25 |
+
# ⚠️ replace f005 if your Backblaze console shows different
|
| 26 |
|
| 27 |
|
| 28 |
# ============================================================
|
|
|
|
| 47 |
end = dt.strptime(date_end, "%d-%m-%Y").strftime("%Y-%m-%d")
|
| 48 |
|
| 49 |
# ----------------------------------------------------
|
| 50 |
+
# Fetch data
|
| 51 |
# ----------------------------------------------------
|
| 52 |
df = yf.download(symbol + ".NS", start=start, end=end)
|
| 53 |
|
|
|
|
| 92 |
plt.grid(True)
|
| 93 |
plt.tight_layout()
|
| 94 |
|
| 95 |
+
plt.savefig(buf, format=IMAGE_FORMAT, dpi=DPI, bbox_inches="tight")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
plt.close()
|
| 97 |
|
| 98 |
buf.seek(0)
|
| 99 |
price_key = f"daily/{symbol}_price_volume.{IMAGE_EXT}"
|
| 100 |
+
|
| 101 |
+
b2.upload_image_compressed(
|
| 102 |
+
"eshanhf",
|
| 103 |
+
price_key,
|
| 104 |
+
buf.getvalue(),
|
| 105 |
+
quality=IMAGE_QUALITY
|
| 106 |
+
)
|
| 107 |
|
| 108 |
# ====================================================
|
| 109 |
# MOVING AVERAGE CHART
|
|
|
|
| 120 |
plt.grid(True)
|
| 121 |
plt.tight_layout()
|
| 122 |
|
| 123 |
+
plt.savefig(buf, format=IMAGE_FORMAT, dpi=DPI, bbox_inches="tight")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
plt.close()
|
| 125 |
|
| 126 |
buf.seek(0)
|
| 127 |
ma_key = f"daily/{symbol}_ma.{IMAGE_EXT}"
|
| 128 |
+
|
| 129 |
+
b2.upload_image_compressed(
|
| 130 |
+
"eshanhf",
|
| 131 |
+
ma_key,
|
| 132 |
+
buf.getvalue(),
|
| 133 |
+
quality=IMAGE_QUALITY
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# ----------------------------------------------------
|
| 137 |
+
# Cache-busting timestamp
|
| 138 |
+
# ----------------------------------------------------
|
| 139 |
+
ts = int(time.time())
|
| 140 |
+
price_url = f"{B2_PUBLIC_BASE}/{price_key}?v={ts}"
|
| 141 |
+
ma_url = f"{B2_PUBLIC_BASE}/{ma_key}?v={ts}"
|
| 142 |
|
| 143 |
# ====================================================
|
| 144 |
# TABLE (Last 100 days)
|
|
|
|
| 157 |
"""
|
| 158 |
|
| 159 |
# ====================================================
|
| 160 |
+
# FINAL HTML (IMAGE WAIT + RETRY)
|
| 161 |
# ====================================================
|
| 162 |
html = f"""
|
| 163 |
<div id="daily_dashboard">
|
|
|
|
| 178 |
</table>
|
| 179 |
|
| 180 |
<h3>Price & Volume</h3>
|
| 181 |
+
<img data-src="{price_url}" style="width:100%;max-width:1200px;">
|
| 182 |
|
| 183 |
<h3>Moving Averages</h3>
|
| 184 |
+
<img data-src="{ma_url}" style="width:100%;max-width:1200px;">
|
| 185 |
+
|
| 186 |
+
<script>
|
| 187 |
+
function loadWithRetry(img, retries = 6, delay = 800) {{
|
| 188 |
+
let attempt = 0;
|
| 189 |
+
|
| 190 |
+
function tryLoad() {{
|
| 191 |
+
attempt++;
|
| 192 |
+
img.src = img.dataset.src + "&retry=" + attempt;
|
| 193 |
+
}}
|
| 194 |
+
|
| 195 |
+
img.onerror = function() {{
|
| 196 |
+
if (attempt < retries) {{
|
| 197 |
+
setTimeout(tryLoad, delay);
|
| 198 |
+
}} else {{
|
| 199 |
+
img.alt = "Image failed to load";
|
| 200 |
+
}}
|
| 201 |
+
}};
|
| 202 |
+
|
| 203 |
+
tryLoad();
|
| 204 |
+
}}
|
| 205 |
+
|
| 206 |
+
document.querySelectorAll("img[data-src]").forEach(img => {{
|
| 207 |
+
loadWithRetry(img);
|
| 208 |
+
}});
|
| 209 |
+
</script>
|
| 210 |
|
| 211 |
</div>
|
| 212 |
"""
|