SalexAI commited on
Commit
6fbb91a
·
verified ·
1 Parent(s): 70f57d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -6
app.py CHANGED
@@ -26,6 +26,31 @@ app.add_middleware(
26
  # ==================================================
27
  # CONFIG
28
  # ==================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  ADMIN_TOKEN = os.environ.get("ADMIN_TOKEN")
30
  GOOGLE_KEY = os.environ.get("GOOGLE_KEY")
31
  HF_TOKEN = os.environ.get("HF_TOKEN")
@@ -91,6 +116,41 @@ scheduler = CommitScheduler(
91
  # ==================================================
92
  # HELPERS
93
  # ==================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  def empty_structure():
95
  return {
96
  "images": {
@@ -123,18 +183,35 @@ def require_admin(token: str):
123
  @app.get("/imageget/{tour}.json")
124
  async def get_images(tour: str):
125
  tour = normalize_tour(tour)
126
- path = tour_path(tour)
127
 
 
 
128
  if path.exists():
129
- return load_json(path)
 
 
130
 
131
  data = await fetch_from_hf(tour)
 
 
 
132
 
133
- if data is None:
134
- return empty_structure()
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- save_json(path, data)
137
- return data
138
 
139
  @app.get("/")
140
  async def root_status():
 
26
  # ==================================================
27
  # CONFIG
28
  # ==================================================
29
+ ALL_TOURS = [
30
+ "Benelux",
31
+ "Epic Rail 1", "Epic Rail 2", "Epic Rail 3",
32
+ "Haida Gwaii 1", "Haida Gwaii 2", "Haida Gwaii 3",
33
+ "Haida Gwaii 4", "Haida Gwaii 5", "Haida Gwaii 6", "Haida Gwaii 7",
34
+ "Harrison",
35
+ "Ireland",
36
+ "Island Hopping", "Island Hopping 2",
37
+ "Kootenays",
38
+ "Maritimes 1", "Maritimes 2",
39
+ "New Zealand",
40
+ "Newfoundland 1", "Newfoundland 2", "Newfoundland 3", "Newfoundland 4",
41
+ "Okanagan",
42
+ "Portugal",
43
+ "Quebec",
44
+ "Quebec Holiday",
45
+ "Scotland",
46
+ "Sea to Sky",
47
+ "Skeena",
48
+ "Tofino 1", "Tofino 2",
49
+ "Van Isle",
50
+ "Yukon 1", "Yukon 2", "Yukon 3", "Yukon 4",
51
+ "Yukon Winter",
52
+ ]
53
+
54
  ADMIN_TOKEN = os.environ.get("ADMIN_TOKEN")
55
  GOOGLE_KEY = os.environ.get("GOOGLE_KEY")
56
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
116
  # ==================================================
117
  # HELPERS
118
  # ==================================================
119
+ import re
120
+
121
+ def has_images(data: dict) -> bool:
122
+ imgs = data.get("images", {})
123
+ return bool(
124
+ imgs.get("banner") or
125
+ imgs.get("cover") or
126
+ imgs.get("carousel")
127
+ )
128
+
129
+
130
+ def get_fallback_tours(requested: str) -> list[str]:
131
+ """
132
+ If 'Maritimes' is requested → returns ['Maritimes 1', 'Maritimes 2']
133
+ If 'Haida Gwaii' → ['Haida Gwaii 1' ...]
134
+ Otherwise empty list
135
+ """
136
+ base = requested.strip()
137
+
138
+ # If request already ends in a number, don't fallback
139
+ if re.search(r"\s\d+$", base):
140
+ return []
141
+
142
+ matches = []
143
+ for t in ALL_TOURS:
144
+ if t.startswith(base + " "):
145
+ matches.append(t)
146
+
147
+ # Sort numerically (1,2,3 not 1,10,2)
148
+ def tour_num(name):
149
+ m = re.search(r"(\d+)$", name)
150
+ return int(m.group(1)) if m else 0
151
+
152
+ return sorted(matches, key=tour_num)
153
+
154
  def empty_structure():
155
  return {
156
  "images": {
 
183
  @app.get("/imageget/{tour}.json")
184
  async def get_images(tour: str):
185
  tour = normalize_tour(tour)
 
186
 
187
+ # 1️⃣ Try exact match first (current behavior)
188
+ path = tour_path(tour)
189
  if path.exists():
190
+ data = load_json(path)
191
+ if has_images(data):
192
+ return data
193
 
194
  data = await fetch_from_hf(tour)
195
+ if data and has_images(data):
196
+ save_json(path, data)
197
+ return data
198
 
199
+ # 2️⃣ Fallback to numbered tours (NEW)
200
+ for alt in get_fallback_tours(tour):
201
+ alt_path = tour_path(alt)
202
+
203
+ if alt_path.exists():
204
+ alt_data = load_json(alt_path)
205
+ if has_images(alt_data):
206
+ return alt_data
207
+
208
+ alt_data = await fetch_from_hf(alt)
209
+ if alt_data and has_images(alt_data):
210
+ save_json(alt_path, alt_data)
211
+ return alt_data
212
 
213
+ # 3️⃣ Nothing found
214
+ return empty_structure()
215
 
216
  @app.get("/")
217
  async def root_status():