daafa999 commited on
Commit
9d7dd5f
·
verified ·
1 Parent(s): 2f70065

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -2
app.py CHANGED
@@ -139,10 +139,45 @@ def clean_columns(df):
139
  # DOWNLOAD DATA
140
  # ============================================================
141
 
 
 
 
 
 
 
 
 
 
142
  def safe_download(symbol):
143
 
144
  try:
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  df = yf.download(
147
  symbol,
148
  period="90d",
@@ -156,10 +191,38 @@ def safe_download(symbol):
156
 
157
  df = clean_columns(df)
158
 
159
- df.to_csv(data_path(symbol))
 
 
160
 
161
  return df, True
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  except Exception as e:
164
 
165
  logger.warning(e)
@@ -698,6 +761,7 @@ def realtime_scan():
698
 
699
  for symbol in SYMBOLS:
700
 
 
701
  try:
702
 
703
  result = run_predict(symbol)
@@ -738,7 +802,7 @@ with gr.Blocks(
738
  interactive=False
739
  )
740
 
741
- timer = gr.Timer(20)
742
 
743
  timer.tick(
744
  fn=realtime_scan,
 
139
  # DOWNLOAD DATA
140
  # ============================================================
141
 
142
+ # ============================================================
143
+ # DOWNLOAD DATA WITH CACHE + RETRY
144
+ # ============================================================
145
+
146
+ LAST_DOWNLOAD = {}
147
+
148
+ CACHE_SECONDS = 300
149
+
150
+
151
  def safe_download(symbol):
152
 
153
  try:
154
 
155
+ # ============================================
156
+ # CACHE
157
+ # ============================================
158
+
159
+ now = time.time()
160
+
161
+ csv_file = data_path(symbol)
162
+
163
+ if os.path.exists(csv_file):
164
+
165
+ last_time = LAST_DOWNLOAD.get(symbol, 0)
166
+
167
+ if now - last_time < CACHE_SECONDS:
168
+
169
+ df = pd.read_csv(
170
+ csv_file,
171
+ index_col=0,
172
+ parse_dates=True
173
+ )
174
+
175
+ return df, True
176
+
177
+ # ============================================
178
+ # DOWNLOAD
179
+ # ============================================
180
+
181
  df = yf.download(
182
  symbol,
183
  period="90d",
 
191
 
192
  df = clean_columns(df)
193
 
194
+ df.to_csv(csv_file)
195
+
196
+ LAST_DOWNLOAD[symbol] = now
197
 
198
  return df, True
199
 
200
+ except Exception as e:
201
+
202
+ logger.warning(
203
+ f"{symbol} download failed: {e}"
204
+ )
205
+
206
+ # ============================================
207
+ # FALLBACK TO CACHE
208
+ # ============================================
209
+
210
+ try:
211
+
212
+ if os.path.exists(csv_file):
213
+
214
+ logger.info(
215
+ f"Using cached data: {symbol}"
216
+ )
217
+
218
+ df = pd.read_csv(
219
+ csv_file,
220
+ index_col=0,
221
+ parse_dates=True
222
+ )
223
+
224
+ return df, False
225
+
226
  except Exception as e:
227
 
228
  logger.warning(e)
 
761
 
762
  for symbol in SYMBOLS:
763
 
764
+ time.sleep(1)
765
  try:
766
 
767
  result = run_predict(symbol)
 
802
  interactive=False
803
  )
804
 
805
+ timer = gr.Timer(120)
806
 
807
  timer.tick(
808
  fn=realtime_scan,