Hemann commited on
Commit
f96d2fa
·
verified ·
1 Parent(s): e29c5b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -7,6 +7,7 @@ import math
7
  import time
8
  import sqlite3
9
  import os
 
10
  from bs4 import BeautifulSoup
11
 
12
  ORS_API_KEY=os.getenv('ORS_API_KEY')
@@ -20,6 +21,21 @@ IST = timezone(timedelta(hours=5, minutes=30))
20
  def now_ist_naive():
21
  return datetime.now(IST).replace(tzinfo=None)
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  APSRTC_HEADERS = {
24
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
25
  "Accept": "*/*",
@@ -228,8 +244,11 @@ class BusCache:
228
  if deleted > 0:
229
  print(f"Cleaned up {deleted} old cache entries")
230
  return deleted
231
- bus_cache = BusCache()
232
- bus_cache.cleanup_old_cache(days_old=100)
 
 
 
233
 
234
  tnstc_manager = TNSTCSessionManager()
235
  TNSTC_PLACE_CODES = {}
 
7
  import time
8
  import sqlite3
9
  import os
10
+ import tempfile
11
  from bs4 import BeautifulSoup
12
 
13
  ORS_API_KEY=os.getenv('ORS_API_KEY')
 
21
  def now_ist_naive():
22
  return datetime.now(IST).replace(tzinfo=None)
23
 
24
+
25
+ def resolve_cache_db_path():
26
+ env_path = os.getenv("BUS_CACHE_DB_PATH")
27
+ if env_path:
28
+ return env_path
29
+
30
+ for candidate_dir in ("/data", tempfile.gettempdir(), "."):
31
+ try:
32
+ if os.path.isdir(candidate_dir) and os.access(candidate_dir, os.W_OK):
33
+ return os.path.join(candidate_dir, "bus_cache.db")
34
+ except Exception:
35
+ continue
36
+
37
+ return "bus_cache.db"
38
+
39
  APSRTC_HEADERS = {
40
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
41
  "Accept": "*/*",
 
244
  if deleted > 0:
245
  print(f"Cleaned up {deleted} old cache entries")
246
  return deleted
247
+ bus_cache = BusCache(resolve_cache_db_path())
248
+ try:
249
+ bus_cache.cleanup_old_cache(days_old=100)
250
+ except sqlite3.OperationalError as e:
251
+ print(f"Cache cleanup skipped (read-only DB): {e}")
252
 
253
  tnstc_manager = TNSTCSessionManager()
254
  TNSTC_PLACE_CODES = {}