added notification when menu is unavailable

This commit is contained in:
Louis Heredero 2024-09-15 00:58:50 +02:00
parent 2cb5186258
commit c9c95350a8
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 23 additions and 7 deletions

View File

@ -215,9 +215,14 @@ class BeeBot:
async with self.fetch_lock: async with self.fetch_lock:
if not os.path.exists(menu_path) or self.is_outdated(menu_id, today_only): if not os.path.exists(menu_path) or self.is_outdated(menu_id, today_only):
if today_only: if today_only:
await self.fetch_today_menu() available = await self.fetch_today_menu()
else: else:
await self.fetch_week_menu() available = await self.fetch_week_menu()
if not available:
await msg.edit_text(self.i18n(prefs["lang"], "notif.unavailable"))
return
await msg.delete() await msg.delete()
# If image needs to be (re)generated # If image needs to be (re)generated
@ -338,33 +343,41 @@ class BeeBot:
self.cache[menu_id] = datetime.datetime.today().strftime("%Y-%m-%d") self.cache[menu_id] = datetime.datetime.today().strftime("%Y-%m-%d")
self.save_cache() self.save_cache()
async def fetch_week_menu(self) -> None: async def fetch_week_menu(self) -> bool:
logger.info("Fetching week menu") logger.info("Fetching week menu")
today = datetime.datetime.today() today = datetime.datetime.today()
delta = datetime.timedelta(days=today.weekday()) delta = datetime.timedelta(days=today.weekday())
monday = today - delta monday = today - delta
days = [] days = []
menus_count = 0
for i in range(5): for i in range(5):
dt = datetime.timedelta(days=i) dt = datetime.timedelta(days=i)
date = monday + dt date = monday + dt
menus = await self.fetch_menu(date) menus = await self.fetch_menu(date)
menus_count += len(menus)
days.append({ days.append({
"date": date.strftime("%Y-%m-%d"), "date": date.strftime("%Y-%m-%d"),
"menus": menus "menus": menus
}) })
if menus_count == 0:
return False
self.save_menu(days, "week_menu", "menus_week.json") self.save_menu(days, "week_menu", "menus_week.json")
return True
async def fetch_today_menu(self) -> None: async def fetch_today_menu(self) -> bool:
logger.info("Fetching today menu") logger.info("Fetching today menu")
today = datetime.datetime.today() today = datetime.datetime.today()
menus = await self.fetch_menu(today) menus = await self.fetch_menu(today)
if len(menus) == 0:
return False
days = [{ days = [{
"date": today.strftime("%Y-%m-%d"), "date": today.strftime("%Y-%m-%d"),
"menus": menus "menus": menus
}] }]
self.save_menu(days, "today_menu", "menus_today.json") self.save_menu(days, "today_menu", "menus_today.json")
return True
async def fetch_menu(self, date: datetime.date) -> list: async def fetch_menu(self, date: datetime.date) -> list:
url = self.MENU_URL.format(date=date.strftime("%Y-%m-%d")) url = self.MENU_URL.format(date=date.strftime("%Y-%m-%d"))

View File

@ -10,7 +10,8 @@
"menu.back_to_settings": "Retour aux paramètres", "menu.back_to_settings": "Retour aux paramètres",
"setting.language": "Langue: {}", "setting.language": "Langue: {}",
"setting.categories": "Catégories: {}", "setting.categories": "Catégories: {}",
"notif.wait_updating": "Le menu est en train d'être mis à jour, veuillez patienter..." "notif.wait_updating": "Le menu est en train d'être mis à jour, veuillez patienter...",
"notif.unavailable": "Il semblerait que le menu ne soit actuellement pas disponible"
}, },
"en": { "en": {
"category.student": "Student", "category.student": "Student",
@ -23,7 +24,8 @@
"menu.back_to_settings": "Back to settings", "menu.back_to_settings": "Back to settings",
"setting.language": "Language: {}", "setting.language": "Language: {}",
"setting.categories": "Categories: {}", "setting.categories": "Categories: {}",
"notif.wait_updating": "The menu is being updated, please wait..." "notif.wait_updating": "The menu is being updated, please wait...",
"notif.unavailable": "It seems that the menu is currently unavailable"
}, },
"de": { "de": {
"category.student": "Student", "category.student": "Student",
@ -36,6 +38,7 @@
"menu.back_to_settings": "Zurück zu Einstellungen", "menu.back_to_settings": "Zurück zu Einstellungen",
"setting.language": "Sprache: {}", "setting.language": "Sprache: {}",
"setting.categories": "Kategorien: {}", "setting.categories": "Kategorien: {}",
"notif.wait_updating": "Das Menü wird gerade aktualisiert, bitte warten Sie..." "notif.wait_updating": "Das Menü wird gerade aktualisiert, bitte warten Sie...",
"notif.unavailable": "Es scheint, dass das Menü derzeit nicht verfügbar ist"
} }
} }