From fcad3389212d3978dabdafcdc831b6de8ac93633 Mon Sep 17 00:00:00 2001 From: Martin Berka Date: Wed, 3 Jan 2024 14:01:45 +0100 Subject: [PATCH] =?UTF-8?q?Oprava=20parsov=C3=A1n=C3=AD=20U=20Motl=C3=ADk?= =?UTF-8?q?=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nyní je počítáno s případy, kdy neexistuje nabídka pro první den v daném týdnu. --- client/src/App.tsx | 2 +- server/src/restaurants.ts | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 4449b83..e5224c2 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -360,7 +360,7 @@ function App() { Poslední změny: {dayIndex != null && diff --git a/server/src/restaurants.ts b/server/src/restaurants.ts index 2af0708..d1700c2 100644 --- a/server/src/restaurants.ts +++ b/server/src/restaurants.ts @@ -157,23 +157,30 @@ export const getMenuUMotliku = async (firstDayOfWeek: Date, mock: boolean = fals const html = await getHtml(U_MOTLIKU_URL); const $ = load(html); - // Najdeme první tabulku, nad kterou je v H3 datum začínající prvním dnem aktuálního týdne - // To může selhat mnoha způsoby, ale ty nemá cenu řešit dokud nenastanou + // Najdeme první tabulku, nad kterou je v H3 datum začínající co nejdřívějším dnem v aktuálním týdnu const tables = $('table.table.table-hover.Xtable-striped'); let usedTable; - const firstDayOfWeekString = `${firstDayOfWeek.getDate()}.${firstDayOfWeek.getMonth() + 1}.`; - for (const tableNode of tables) { - const table = $(tableNode); - const h3 = table.parent().prev(); - const s1 = h3.text().split("-")[0].split("."); - const foundFirstDayString = `${s1[0]}.${s1[1]}.`; - if (foundFirstDayString === firstDayOfWeekString) { - usedTable = table; + let usedDate = new Date(firstDayOfWeek.getTime()); + for (let i = 0; i < 4; i++) { + const dayOfWeekString = `${usedDate.getDate()}.${usedDate.getMonth() + 1}.`; + for (const tableNode of tables) { + const table = $(tableNode); + const h3 = table.parent().prev(); + const s1 = h3.text().split("-")[0].split("."); + const foundFirstDayString = `${s1[0]}.${s1[1]}.`; + if (foundFirstDayString === dayOfWeekString) { + usedTable = table; + } } + if (usedTable != null) { + break; + } + usedDate.setDate(usedDate.getDate() + 1); } if (usedTable == null) { - throw Error(`Nepodařilo se najít tabulku pro datum ${firstDayOfWeekString}`); + const firstDayOfWeekString = `${firstDayOfWeek.getDate()}.${firstDayOfWeek.getMonth() + 1}.`; + throw Error(`Nepodařilo se najít tabulku pro týden začínající ${firstDayOfWeekString}`); } const body = usedTable.children().first();